Cod sursa(job #1854819)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 23 ianuarie 2017 11:35:38
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#define top 6000005

using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

int a[top],best,bestmax=0,pozbestmax,lg[top];

int main()
{
    int n,i;

    fin>>n;
    for(i=1; i<=n; i++) fin>>a[i];
    fin.close();

    best=a[1];
    bestmax=a[1];
    pozbestmax=1;
    lg[1]=1;
    for(i=2; i<=n; i++)
    {
        if(best+a[i]>=a[i]) { best=best+a[i];
                             lg[i]=lg[i-1]+1;
                             if(best>bestmax) { bestmax=best; pozbestmax=i;}
                             else if(best==bestmax && lg[i]<lg[pozbestmax]) { bestmax=best; pozbestmax=i;}
                           }
        else { best=a[i];
               lg[i]=1;
               if(best>bestmax) { bestmax=best; pozbestmax=i;}
             }
    }

  fout<<bestmax<<' '<<pozbestmax-lg[pozbestmax]+1<<' '<<pozbestmax<<'\n';
  fout.close();


    fin.close();

    return 0;
}