Cod sursa(job #2835509)

Utilizator CiuiGinjoveanu Dragos Ciui Data 18 ianuarie 2022 20:07:47
Problema Secventa 2 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
 
ifstream fin("secv2.in");
ofstream fout("secv2.out");
 
const int MAX_SIZE = 50001;
int sp[MAX_SIZE];
 
int main() {
    int n, v[MAX_SIZE], k;
    fin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        sp[i] = sp[i - 1] + v[i];
    }
    int min = 0, maxSum = -1250000000, minIndex = 0, startIndex = 0, stopIndex = 0;
    for (int i = 1; i <= n; ++i) {
        if (sp[i] - min > maxSum && i - minIndex >= k) {
            maxSum = sp[i] - min;
            startIndex = minIndex + 1;
            stopIndex = i;
        }
        if (min > sp[i] && sp[i] > 0) {
            min = sp[i];
            minIndex = i;
        }
    }
    fout << startIndex << " " << stopIndex << " " << maxSum;
}
 
/*
 3 2
 -25000 -25000 -25000
 
 3 3
 -25000 -25000 -25000

 
 */