-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathC.java
More file actions
39 lines (36 loc) · 1015 Bytes
/
Copy pathC.java
File metadata and controls
39 lines (36 loc) · 1015 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
package atcoder.nomura;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public final class C {
public static void main(String[] args) {
final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
final int n = in.nextInt() + 1;
in.nextLine();
final int[] leaves = new int[n];
long branch = 0;
for (int i = 0; i < n; i++) {
leaves[i] = in.nextInt();
branch += leaves[i];
}
branch--;
long res = 0;
long curr = 1;
for (int i = 0; i < n; i++) {
res += curr;
curr -= leaves[i];
if (curr < 0) {
res = -1;
break;
}
if (curr <= branch) {
branch -= curr;
curr *= 2;
} else {
curr += branch;
branch = 0;
}
}
System.out.println(res);
}
}