Cod sursa(job #2680957)

Utilizator bg16-2009bg16 bg16 bg16-2009 Data 4 decembrie 2020 18:12:08
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main() {
    int S = 0, n, x, start = 0, st, dr, S_max = 0, len = 0, len_S_max = 6000000;
    f >> n;
    st = dr = 1;
    for (int i = 1; i <= n; i++) {
        f >> x;
        if (S < 0) {
            S = 0;
            start = i;
        }
        S += x;
        if (S > S_max) {
            S_max = S;
            dr = i;
            st = start;
            len_S_max = len;
            len = 0;
        } else if (S == S_max && len < len_S_max) {
            S_max = S;
            dr = i;
            st = start;
            len_S_max = len;
            len = 0;
        }
        len++;
    }
    g << S_max << ' ' << st << ' ' << dr;
    f.close();
    g.close();
    return 0;
}