-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP787.java
executable file
·52 lines (47 loc) · 1.32 KB
/
P787.java
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
import java.math.BigInteger;
import java.util.*;
public class P787 {
private static BigInteger update(BigInteger max, ArrayList<Integer> a) {
if(a.isEmpty())
return max;
BigInteger ret = max;
BigInteger left = BigInteger.ONE;
for(int i = 0; i < a.size(); ++i) {
left = left.multiply(BigInteger.valueOf(a.get(i)));
if(ret == null || left.compareTo(ret) > 0)
ret = left;
}
BigInteger right = BigInteger.ONE;
for(int i = a.size()-1; i >= 0; --i) {
right = right.multiply(BigInteger.valueOf(a.get(i)));
if(ret == null || right.compareTo(ret) > 0)
ret = right;
}
a.clear();
return ret;
}
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
while(s.hasNextInt()) {
BigInteger max = null;
ArrayList<Integer> a = new ArrayList<Integer>();
while(s.hasNextInt()) {
int n = s.nextInt();
if(n == -999999) {
max = update(max, a);
break;
}
else if(n == 0) {
max = update(max, a);
if(max == null || max.compareTo(BigInteger.ZERO) < 0)
max = BigInteger.ZERO;
}
else {
a.add(n);
}
}
max = update(max, a);
System.out.println(max.toString()); // Fails if n is null... which we want.
} // while s.hasNextInt()
} // int main
} // class