Cod sursa(job #2371696)

Utilizator HelloWorldBogdan Rizescu HelloWorld Data 6 martie 2019 19:04:57
Problema Subsecventa de suma maxima Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <climits>
using namespace std;
ifstream in("ssm.in");
ofstream out("ssm.out");
int n,i,x,max_so_far,max_ending_here,inceput,inceput2,sfarsit,max_negatives=INT_MIN,poz,negative;
int main()
{
    in>>n;
    for (i=1; i<=n; ++i)
    {
        in>>x;
        if (x<0)
        {
            negative++;
            max_negatives=max(max_negatives,x);
            poz=i;
        }
        max_ending_here+=x;
        if (max_ending_here<=0)
        {
            max_ending_here=0;
            inceput=i+1;
        }
        if (max_so_far<max_ending_here)
        {
            max_so_far=max_ending_here;
            inceput2=inceput;
            sfarsit=i;
        }
        else if (max_so_far==max_ending_here)
        {
            if (i-inceput+1<sfarsit-inceput2+1)
            {
                inceput2=inceput;
                sfarsit=i;
            }
        }
    }
    if (negative==n) out<<max_negatives<<" "<<poz<<" "<<poz<<"\n";
    else out<<max_so_far<<" "<<inceput2<<" "<<sfarsit<<"\n";
}