Cod sursa(job #1364978)

Utilizator mariapascuMaria Pascu mariapascu Data 27 februarie 2015 22:34:38
Problema Subsecventa de suma maxima Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
using namespace std;

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

int n, a[6000004];
int smax, imax, jmax;
int s, i, j;

int main()
{
    fin >> n;
    for ( i = 1; i <= n; i++ )
        fin >> a[i];
  //  for ( int i = 1; i <= n; i++ )
    //    fout << a[i] << ' ';
    //fout << '\n';
    i = 1;
    for ( int j = 1; j <= n; j++ )
    {
        if ( s + a[j] <= 0 )
        {
            s = 0, i = j + 1;
        }
        else
        {
            s += a[j];
            if ( smax < s )
            {
                smax = s;
                imax = i;
                jmax = j;
            }
            else
                if ( smax == s && imax > i )
                {
                    imax = i;
                    jmax = j;
                }
            else
                if ( smax == s && imax == i && jmax > j )
                    jmax = j;
        }
    }
    fout << smax << ' ' << imax << ' ' << jmax;

    fin.close();
    fout.close();
    return 0;
}