Cod sursa(job #1290041)

Utilizator cristinamateiCristina Matei cristinamatei Data 10 decembrie 2014 19:10:35
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include<iostream>

using namespace std;


int main()
{
    ifstream in("ssm.in");
    ofstream out("ssm.out");
    int n, i, sc, stc, stmax, drmax, smax, x;
    in >> n;
    in >> x;
    /*for ( i = 1; i <= n; i++ )
        in >> a[i];*/
    //smax = a[1];
    smax = x;
    stmax = 1;
    drmax = 1;
    sc = x;
     for ( i = 2; i <= n; i++ )
    {
        in >> x;
        if ( sc < 0 )
        {
            sc = 0;
            stc = i;
        }
        sc+= x;
        if ( sc > smax)
        {
            smax = sc;
            stmax = stc;
            drmax = i;
        }
    }
    out << smax << ' ' << stmax <<' '<< drmax;
    return 0;
}