```
int max=A[0], i=0;
while (A[i]<=0 && i<n) {
if (A[i]>max) max=A[i];
++i;
}
if (i==n) return max;
int b=0, maxsum=0;
while (b<n ) {
int i=b, sum=0, runningsum=0;
while (A[i]<=0 && i<n) ++i;
b=i;
int e=b;
while (A[e]>0) {
sum+=A[e];
++e;
}
i=e;
runningsum=sum;
while (runningsum>0 && i<n) {
runningsum+=A[i];
if (runningsum>sum) {
sum=runningsum;
}
++i;
}
if (maxsum<sum) {
maxsum=sum;
}
b=i;
}
return maxsum;
```