-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusVideoSystem.java
More file actions
60 lines (53 loc) · 1.79 KB
/
BusVideoSystem.java
File metadata and controls
60 lines (53 loc) · 1.79 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Created by MalhotR1 on 05/13/2018.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BusVideoSystem {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String[] in = br.readLine().trim().split(" ");
int N = Integer.parseInt(in[0]);
long w = Long.parseLong(in[1]);
in = br.readLine().trim().split(" ");
int[] arr = new int[N];
long sum = 0;
long temp = 0;
boolean isPossible = true;
for (int i = 0; i < in.length; i++) {
arr[i] = Integer.parseInt(in[i]);
sum += arr[i];
temp = Math.abs(sum);
if (temp > w) {
isPossible = false;
break;
}
}
if (!isPossible) System.out.println(0);
else {
long present = 0;
temp = 0;
long tempn = 0;
long max = 0;
long min = 0;
long maxPb = w + 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
tempn = 0;
temp += arr[i];
max = (max > temp) ? max : temp;
} else {
temp = 0;
tempn += arr[i];
min = (min < temp) ? min : temp;
}
}
min = Math.abs(min);
max = (max > min) ? max : min;
maxPb -= max;
System.out.println(maxPb);
}
}
}
}