Cod sursa(job #1854810)

Utilizator davidbejenariu2David Bejenariu davidbejenariu2 Data 23 ianuarie 2017 11:33:55
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#define nmax 6000001

using namespace std;

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

int a[nmax], lg[nmax];

int main()
{   int n, i, s, smax, imax;
    fin >> n;
    for ( i = 1; i <= n; i++ )
         fin >> a[i];
    fin.close();
    s = a[1];
    lg[1] = 1;
    smax = s;
    imax = 1;
    for ( i = 2; i <= n; i++ )
         if ( s >= 0 )
             { s = s + a[i];
               lg[i] = lg[i-1] + 1;
               if ( s > smax )
                   { smax = s;
                     imax = i;
                   }
               else if ( s == smax )
                        if ( lg[i] < lg[imax] )
                            imax = i;
             }
         else
             { s = a[i];
               lg[i] = 1;
               if ( s > smax )
                   { smax = s;
                     imax = i;
                   }
               else if ( s == smax )
                        if ( lg[i] < lg[imax] )
                            imax = i;
             }
    fout << smax << " " << imax - lg[imax] + 1 << " " << imax << "\n";
    fout.close();
    return 0;
}