Cod sursa(job #1458126)

Utilizator tiby10Tibi P tiby10 Data 6 iulie 2015 23:26:19
Problema Subsecventa de suma maxima Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 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;
    for(i=1;i<=n;i++){

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

        if(best[i] > celMaiTheBest){
            celMaiTheBest=best[i];
            e=i;
        }

    }

    printf("%d %d %d",celMaiTheBest,s,e);
    return 0;
}