#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <deque>
#include <set>
using namespace std;
#define speedup ios::sync_with_stdio(0),cin.tie(0);
long long int power(long long int n,long long int p)
{
long long int x=1;
for(int i=1;i<=p;i++)
{
x*=n;
}
return x;
}
template<typename t>
t gcd(t a,t b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
/*
long long int maxSubArraySum(vector<long long int>&a)
{
int max_so_far = -100000000, max_ending_here = 0;
for (int i = 0; i < a.size(); i++)
{
max_ending_here = max_ending_here + a[i];
max_so_far=max(max_so_far,max_ending_here);
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
*/
long long int maxSubarraySum(vector<long long int>&a)
{
int size=a.size();
int max_ending_here = 0, max_so_far =-1000000000;
for (int i = 0; i < size; i++) {
if (a[i] <= max_ending_here + a[i]) {
max_ending_here += a[i];
}
else {
max_ending_here = a[i];
}
if (max_ending_here > max_so_far)
max_so_far = max_ending_here;
}
return max_so_far;
}
int main()
{
speedup;
int n;
long long int mx=(-10000000);
cin>>n;
vector<long long int>a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
//cout<<maxSubArraySum(a)<<endl;
cout<<maxSubarraySum(a)<<endl;
return 0;
}
Copy