Source Code
#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, flag = 0;
    cin>>n;
    deque<long long int>a(n);
    long long int mn=-10000000;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mn=max(mn,a[i]);
    }

    for(int i=0;i<a.size();)
    {
        if(a[i]<=0)
        {
            a.pop_front();
        }
        else
        {
            break;
        }
    }

    if(a.size()==0)
    {
        cout<<mn;
        return 0;
    }

    for(int i=a.size()-1;i>=0;i--)
    {
        if(a[i]<=0)
        {
            a.pop_back();
        }
        else
        {
            break;
        }
    }




    int max = 0 , end;

    for(int i =0; i<a.size(); i++)
        max += a[i];

    for (int i = 0; i < a.size(); i++){

        int j = i;
        while((a[j] > 0)&&(j < a.size())){

            end = j++;
        }

        int sum = 0;
        for(int x = i; x <= end; x++)
            sum += a[x];

        if(sum > max)
            max = sum;
    }
    cout<<max<<endl;


    return 0;
}
Copy
Movies IssamAbdoh
GNU G++17
1084 ms
8.9 MB
Time Limit Exceeded