Cod sursa(job #1801370)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 8 noiembrie 2016 22:32:48
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("ssm.in");
ofstream fout ("ssm.out");

int main()
{
    int n, x;
    int s, smax;
    int first, last;
    int p, i;

    fin >> n;

    fin >> x;
    s=smax=x;
    first=last=1;
    if (0>s)
    {
        s=0;
        p=2;
    }

    for (i=2; i<=n; i++)
    {
        fin >> x;
        s+=x;
        if (s>smax)
        {
            smax=s;
            first=p;
            last=i;
        }
        if (s<0)
        {
            s=0;
            p=i+1;
        }
    }

    fout<< smax<<" "<<first<<" "<<last<<"\n";
    fin.close();
    fout.close();
    return 0;
}