-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMain.java
More file actions
39 lines (35 loc) · 841 Bytes
/
Main.java
File metadata and controls
39 lines (35 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int group;
int mountain;
int[] heights;
String temp;
int a, b;
group = Integer.parseInt(cin.nextLine());
for(int x=0; x < group; x++){
mountain = Integer.parseInt(cin.nextLine());
heights = new int[mountain];
int sum = 0;
if (mountain > 1) {
temp = cin.nextLine();
String[] res = temp.split(" ");
for (int i = 0; i < res.length; i++)
heights[i] = Integer.parseInt(res[i]);
int h = 0;
int i = 0;
for (i = 0; i < heights.length; i++) {
sum += Math.abs(heights[i] - h) * 2;
h = heights[i];
}
sum += heights[i - 1] * 2;
}else if(mountain==1){
sum = heights[0] * 2;
}else{
sum = -1;
}
System.out.println(sum);
}
}
}