Cod sursa(job #2730536)

Utilizator AACthAirinei Andrei Cristian AACth Data 26 martie 2021 15:24:36
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
const int Max = 1e5 + 1;
void nos()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

void solve()
{
    int sum_max = 0;
    int sum = 0;
    int i,n;
    f>>n;
    int start_max,end_max;
    int start = 1;
    int all_negative = true;
    int maxx=INT_MIN,poz_max;
    for(i=1;i<=n;i++)
    {
        int x;
        f>>x;
        if(x > 0)
            all_negative = false;
        if(x>maxx)
        {
            maxx = x;
            poz_max = i;
        }
        sum += x;
        if(sum < 0 )
            sum = 0,start = i+1;
        if(sum > sum_max)
        {
            sum_max = sum;
            start_max = start;
            end_max = i;
        }
    }
    if(!all_negative)
    g<<sum_max<<' '<<start_max<<' '<<end_max;
    else
    g<<maxx<<' '<<poz_max<<' '<<poz_max;

}


int32_t main()
{
        solve();
    return 0;
}