Cod sursa(job #2371636)

Utilizator HelloWorldBogdan Rizescu HelloWorld Data 6 martie 2019 18:46:53
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 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,inceput_negative,negative,max_for_negatives=INT_MIN;
int main()
{
    in>>n;
    for (i=1; i<=n; ++i)
    {
        in>>x;
        if (x>max_for_negatives)
        {
            max_for_negatives=x;
            inceput_negative=i;
        }
        max_ending_here+=x;
        if (max_ending_here<0)
        {
            negative++;
            max_ending_here=0;
            inceput=i+1;
        }
        if (max_so_far<max_ending_here)
        {
            max_so_far=max_ending_here;
            inceput2=inceput;
            sfarsit=i;
        }
    }
    if (negative==n) out<<max_for_negatives<<" "<<inceput_negative<<" "<<inceput_negative<<"\n";
    else out<<max_so_far<<" "<<inceput2<<" "<<sfarsit<<"\n";
}