Cod sursa(job #2378411)

Utilizator baltoi.teodorTeodor Baltoi baltoi.teodor Data 12 martie 2019 10:47:00
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
typedef long long ll;
int main()
{
    ll S,n,i,max1=0,x,st,stop,start,fn,max2=INT_MIN,st1,fn1;
    fin>>n;
    S=0;
    st=1;
    fn=0;
    for(i=1;i<=n;++i)
    {
        fin>>x;
        if(x>max2) {max2=x;st1=i;fn1=i;}
        fn++;
        S+=x;
        if(S>max1) {max1=S,start=st,stop=fn;}
        if(S<0) {S=0,st=fn+1,fn=fn;}
    }
    if(S>max1) {max1=S,start=st,stop=fn;}
    if(max2>=0)
    fout<<max1<<" "<<start<< " "<<stop<<"\n";
    else fout<<max2<<" "<<st1<<" "<<fn1<<"\n";
    return 0;
}