Cod sursa(job #2395494)

Utilizator LunguPetruPetru Lungu LunguPetru Data 2 aprilie 2019 16:53:53
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fi("ssm.in");
ofstream fo("ssm.out");
const int nmax=6e6;
int n, smax, st, dr, stmax, drmax;
int X[nmax+5], S[nmax+5];

int main()
{
    fi>>n;
    for(int i=1; i<=n; i++)
        fi>>X[i];

    smax=S[1]=X[1];
    st=dr=1;
    for(int i=2; i<=n; i++)
    {
        if(S[i-1]>=0)
        {
            S[i]=S[i-1]+X[i];
            dr=i;
        }
        else
        {
            S[i]=X[i];
            st=dr=i;
        }
        if(S[i]>smax)
        {
            smax=S[i];
            stmax=st;
            drmax=dr;
        }
    }

    for(int i=stmax; i<=drmax; i++)
        if(S[i]==smax)
        {
            drmax=i;
            break;
        }

    fo<<smax<<" "<<stmax<<" "<<drmax;

    fi.close();
    fo.close();
    return 0;
}