Cod sursa(job #2909030)

Utilizator alexdvResiga Alexandru alexdv Data 7 iunie 2022 22:56:51
Problema Secventa 2 Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
using namespace std;

int max_neg = -25001, positive, maxx, countt, act, n, k, v[50001], leftt, rightt;

int main() {
    ifstream fin("secv2.in");
    ofstream fout("secv2.out");
    fin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        if (v[i] > -1) {
            ++positive;
        }
        if (v[i] > max_neg && v[i] < 0) {
            max_neg = v[i];
        }
    }
    if (positive > 0) {
        for (int i = 1; i <= n; ++i) {
            act = act + v[i];
            ++countt;
            if (maxx < act && countt >= k) {
                maxx = act;
                rightt = i;
            }
            if (act < 0) {
                act = 0;
                countt = 0;
            }
        }
        int i = rightt, summ = 0;
        while (summ < maxx) {
            summ = summ + v[i];
            leftt = i;
            --i;
        }
        fout << leftt << ' ' << rightt <<  ' ' << maxx;
    } else {
        fout << max_neg;
    }
    return 0;
}