Cod sursa(job #1458133)

Utilizator tiby10Tibi P tiby10 Data 6 iulie 2015 23:33:53
Problema Subsecventa de suma maxima Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
//subsecv de suma maxima
#include<bits/stdc++.h>
#define debug cerr<<"ok";
#define MAXN 6000000

using namespace std;

int n,i,j,a[MAXN],best[MAXN];

int main ()
{
    freopen("ssm.in","r",stdin);
    freopen("ssm.out","w",stdout);
    scanf("%d",&n);

    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);

    int celMaiTheBest=-(int)1e4;
    int s=0,e=0,bestS=0,bestE=0;
    for(i=1;i<=n;i++){

        if(a[i] > best[i-1]+a[i]){
            bestS=s=i;
            best[i]=a[i];
        }
        else
            best[i]=best[i-1]+a[i];

        if(best[i] > celMaiTheBest){
            celMaiTheBest=best[i];
            bestE=e=i;
        }
        if(best[i] == celMaiTheBest && e-s < bestE-bestS){
            bestE=e;
            bestS=s;
        }

    }

    printf("%d %d %d",celMaiTheBest,bestS,bestE);
    return 0;
}