Cod sursa(job #1686786)

Utilizator PaulTPaul Tirlisan PaulT Data 12 aprilie 2016 14:02:26
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <climits>
using namespace std;

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

int main()
{
    int x, n, i = 1, j, I, J, I1, s = 0, S = INT_MIN, nmax = INT_MIN;
    fin >> n;
    for (j = 1; j <= n; j++)
    {
        fin >> x;
        if (x > nmax)
        {
            nmax = x;
            I1 = j;
        }
        if ( s + x >= 0 )
        {
            s += x;
            if ( S < s )
            {
                S = s;
                I = i;
                J = j;
            }
        }
        else
        {
            i = j + 1;
            s = 0;
        }
    }
    if (S > INT_MIN)
        fout << S << ' ' << I << ' ' << J << '\n';
    else
        fout << nmax << ' ' << I1 << ' ' << I1 << '\n';

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