/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <limits.h>
using namespace std;
long long n, arr[1000000];
int main()
{
cin >> n;
for(int i=0; i<n; i++){
cin >> arr[i] ;
}
long long sum = arr[0];
long long maxSoFar = arr[0];
for(int i=1; i<n; i++){
sum = sum + arr[i];
if (sum > maxSoFar) {
maxSoFar = sum;
}
if (sum < 0) sum = 0;
}
cout << maxSoFar;
return 0;
}
Copy