package com.yimu.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class D {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine());
String[] sp = bufferedReader.readLine().split(" ");
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(sp[i]);
}
long[] maxs = new long[n+1];
long max ;
max = Arrays.stream(nums).max().getAsLong();
if (max <= 0) {
System.out.println(max);
return;
}
int j = 0;
maxs[j] = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
maxs[j] += nums[i];
}else{
if (maxs[j] > (-nums[i])) {
maxs[j] += nums[i];
}else{
j++;
maxs[j] = 0;
}
}
maxs[i] = maxs[j];
}
max = Arrays.stream(maxs).max().getAsLong();
System.out.println(max);
}
}
package com.yimu.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class D {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine());
String[] sp = bufferedReader.readLine().split(" ");
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(sp[i]);
}
long max ;
max = Arrays.stream(nums).max().getAsLong();
if (max <= 0) {
System.out.println(max);
return;
}
if (nums[0] < 0) nums[0] = 0;
for (int i = 1; i < nums.length; i++) {
long l = nums[i - 1] + nums[i];
if (nums[i] > 0) nums[i] = l;
else{
if (nums[i-1] > (-nums[i])) nums[i] = l;
else{
nums[i] = 0;
}
}
}
max = Arrays.stream(nums).max().getAsLong();
System.out.println(max);
}
}