Cod sursa(job #3362875)

Utilizator horia.boeriuBoeriu Horia Andrei horia.boeriu Data 12 august 2026 21:22:17
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <iostream>

int readInt(FILE *fin) {
    int x, semn;
    char ch;
    ch = fgetc(fin);
    while (isspace(ch)) {
        ch = fgetc(fin);
    }
    semn = 1;
    if (ch == '-') {
        semn = -1;
        ch = fgetc(fin);
    }
    x = 0;
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = fgetc(fin);
    }
    return x * semn;
}
int main()
{
    FILE *fin, *fout;
    int n, i, mins, poz, st, dr, rez, s, x;
    fin = fopen("ssm.in", "r");
    fscanf(fin, "%d%d", &n, &x);
    rez = s = x;
    st = dr = 1;
    mins = poz = 0;
    if (x < 0) {
        mins = x;
        poz = 1;
    }
    for (i = 2; i <= n; i++) {
        x = readInt(fin);
        s += x;
        if (s - mins > rez) {
            rez = s - mins;
            st = poz + 1;
            dr = i;
        }
        if (s < mins) {
            mins = s;
            poz = i;
        }
    }
    fclose(fin);
    fout = fopen("ssm.out", "w");
    fprintf(fout, "%d %d %d\n", rez, st, dr);
    fclose(fout);
    return 0;
}