Cod sursa(job #1767298)

Utilizator preda.andreiPreda Andrei preda.andrei Data 28 septembrie 2016 21:29:44
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 fin("ssm.in");
    ofstream fout("ssm.out");

    int n;
    fin >> n;

    int smax = -(1 << 30), start = 0, stop = 0;
    int curent = 0, st = 0;

    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;

        curent += x;
        if (curent < 0 || st == 0) {
            curent = x;
            st = i;
        }

        if (curent > smax) {
            smax = curent;
            start = st;
            stop = i;
        }

        if (curent < 0)
            curent = st = 0;
    }

    fout << smax << " " << start << " " << stop << "\n";
    return 0;
}