Cod sursa(job #2282209)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 13 noiembrie 2018 14:28:50
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
using namespace std;

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

int main()
{
    int N;
    fin >> N;

    int best, best_i, best_j;
    fin >> best;
    best_i = best_j = 1;

    int sum = best;
    N--;

    int i = 1, j = 1;
    while(N--)
    {
        j++;
        int x;
        fin >> x;
        if(x > sum + x)
        {
            sum = x;
            i = j;
        }
        else
            sum += x;

        if(sum > best)
        {
            best = sum;
            best_i = i;
            best_j = j;
        }
    }

    fout << best << ' ' << best_i << ' ' << best_j << '\n';

    return 0;
}