Cod sursa(job #2848084)

Utilizator cristiWTCristi Tanase cristiWT Data 12 februarie 2022 09:48:10
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>

#define NMAX 50010

using namespace std;

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

int N, K, sp[NMAX];

int main() {
    f >> N >> K;
    for (int i = 1; i <= N; i++) {
        int x;
        f >> x;
        sp[i] = sp[i - 1] + x;
    }

    int Min = 0, ans = INT_MIN, minIndex = 0, startIndex = 0, stopIndex = 0;
    for (int i = 1; i <= N; i++) {
        if (sp[i] - Min > ans && i - minIndex >= K) {
            ans = sp[i] - Min;
            startIndex = minIndex + 1;
            stopIndex = i;
        }

        if (i - K + 1 >= 1 && sp[i] - sp[i - K] > ans) {
            ans = sp[i] - sp[i - K];
            startIndex = i - K + 1;
            stopIndex = i;
        }

        if (sp[i] < Min) {
            Min = sp[i];
            minIndex = i;
        }
    }

    g << startIndex << ' ' << stopIndex << ' ';
    g << ans;
}