-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00787.java
64 lines (61 loc) · 2.04 KB
/
UVa00787.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
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Main
{
static PrintStream out = System.out;
static PrintStream err = System.err;
static final int INF = 1000000000;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// if necessary
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
try
{
while (br.ready())
{
String[] list = br.readLine().split(" ");
BigInteger maxProd = null;
BigInteger maxProdPos = BigInteger.ZERO;
BigInteger maxProdNeg = BigInteger.ZERO;
// pr.printf("next:%5s max:%10s pos:%10s neg:%10s%n", "null", "null", maxProdPos.toString(), maxProdNeg.toString());
for (int i = 0; i < list.length - 1; i++)
{
BigInteger oldPos = (maxProdPos.compareTo(BigInteger.ZERO) == 0) ? BigInteger.ONE : maxProdPos;
BigInteger oldNeg = (maxProdNeg.compareTo(BigInteger.ZERO) == 0) ? BigInteger.ONE : maxProdNeg;
int next = Integer.parseInt(list[i]);
if (next < 0)
{
if (oldNeg.compareTo(BigInteger.ZERO) == -1)
maxProdPos = oldNeg.multiply(BigInteger.valueOf(next));
else
maxProdPos = BigInteger.ZERO;
maxProdNeg = oldPos.multiply(BigInteger.valueOf(next));
}
else if (next > 0)
{
maxProdPos = oldPos.multiply(BigInteger.valueOf(next));
if (maxProdNeg.compareTo(BigInteger.ZERO) == -1)
maxProdNeg = oldNeg.multiply(BigInteger.valueOf(next));
}
else
{
maxProdPos = BigInteger.ZERO;
maxProdNeg = BigInteger.ZERO;
}
if (maxProd == null)
maxProd = (maxProdPos == BigInteger.ZERO) ? maxProdNeg : maxProdPos;
else
maxProd = maxProd.max(maxProdPos.max(maxProdNeg));
// pr.printf("next:%5d max:%10s pos:%10s neg:%10s%n", next, maxProd.toString(), maxProdPos.toString(), maxProdNeg.toString());
}
pr.println(maxProd);
}
} catch (IOException ex) {}
pr.close();
}
}