Cod sursa(job #2911136)

Utilizator bumblebeeGeorge Bondar bumblebee Data 27 iunie 2022 10:30:05
Problema Secventa 2 Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("secv2.in");
ofstream fout("secv2.out");

int main() {
    int n, k, best = 0, value, startIdx, maxStartIdx = 1, maxEndIdx = 1, maxBest = -25001, counter = 0;
    fin >> n >> k;
    for (int i = 1; i <= n; i++) {
        fin >> value;
        if (best + value >= value) {
            best += value;
            ++counter;
        } else if (value > best) {
            startIdx = i;
            best = value;
            counter = 0;
        }
        if (counter >= k && best > maxBest) {
            maxBest = best;
            maxStartIdx = startIdx;
            maxEndIdx = i;
        }
    }
    if (maxBest == -25001) {
        fout << 1 << " " << 1 << " " << 1;
    } else {
        fout << maxStartIdx << " " << maxEndIdx << " " << maxBest;
    }
    return 0;
}