Cod sursa(job #2595237)

Utilizator CruandyAndrey Cruceru Cruandy Data 7 aprilie 2020 13:33:17
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
using namespace std;
ifstream fin ("ssm.in");
ofstream fout ("ssm.out");

long long n, smax, s, i, j, imax, jmax, a, vmax, kmax;
int main () {
    fin >> n;
    smax = -1000000000;
    smax = smax * 1000;
    vmax = smax;
    i = 1;
    s = 0;
    for (j = 1; j <= n; ++j){
        fin >> a;
        s += a;
        if (s < 0){
            i = j + 1;
            s = 0;
        }
        else {
            if (s > smax){
                smax = s;
                imax = i;
                jmax = j;
            }
        }
        if (a > vmax){
            vmax = a;
            kmax = j;
        }
    }
    if (vmax < 0){
        fout << vmax << " " << kmax << " " << kmax;
    }
    else {
        fout << smax << " " << imax << " " << jmax;
    }

}