-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterMaximalSum.java
More file actions
81 lines (60 loc) · 1.85 KB
/
BetterMaximalSum.java
File metadata and controls
81 lines (60 loc) · 1.85 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by arpit on 13/11/16.
*/
public class BetterMaximalSum {
static final int MAX=100000;
static int[] a=new int[MAX];
static long e[]=new long[MAX];
static long s[]=new long[MAX];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n;
String s[];
t=Integer.parseInt(br.readLine());
while (t-->0){
n=Integer.parseInt(br.readLine());
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i]=Integer.parseInt(s[i]);
}
System.out.println(solve(a,n));
}
}
private static long solve(int[] a, int n) {
for (int i = 0; i < n; i++)
e[i]=s[i]=0;
long prev=0,ans=Long.MIN_VALUE;
e[0]=ans=a[0];
s[n-1]=a[n-1];
for (int i = 1; i <n ; i++) {
e[i]=Long.max(a[i],e[i-1]+a[i]);
ans=Long.max(ans,e[i]);
}
for (int i=n-2;i>=0;i--){
s[i]=Long.max(a[i],s[i+1]+a[i]);
//System.out.println("xenny[i]= "+xenny[i]+"prev"+prev);
}
for (int i = 1; i <(n-1) ; i++) {
ans=Long.max(ans,e[i-1]+s[i+1]);
//System.out.println("ans = "+ans);
}
/*for (int i = 0; i < n; i++) {
System.out.print(xenny[i] + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(e[i] + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(s[i] + " ");
}
System.out.println();*/
return ans;
}
}