Cod sursa(job #1893816)

Utilizator savigunFeleaga Dragos-George savigun Data 26 februarie 2017 01:25:49
Problema Subsecventa de suma maxima Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
using namespace std;

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

int s[6000005], l[6000005], pmax, lmax, smax;

int main() {
    int n, x;
    in >> n;

    in >> x;
    s[1] = x;
    l[1] = 1;
    pmax = 1;

    for (int i = 2; i <= n; ++i) {
        in >> x;
        s[i] = max(s[i-1] + x, x);
        if (s[i-1] + x >= x) {
            l[i] = l[i-1] + 1;
        } else {
            l[i] = 1;
        }

        if (s[i] > smax) {
            smax = s[i];
            lmax = l[i];
            pmax = i;
        } else if (s[i] == smax) {
            if (lmax > l[i]) {
                lmax = l[i];
                pmax = i;
            }
        }
    }

    out << smax << " " << pmax - lmax + 1 << " " << pmax;

    return 0;
}