Cod sursa(job #3126543)

Utilizator corinarobuRobu Corina corinarobu Data 6 mai 2023 18:47:00
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int arr[5000001], a[5000001], n, k, i, fr;
int main(){
    int bk = -1;
    long rez = 0;
    f >> n >> k;

    for( i = 0; i < n; i++)
        f >> a[i];


   i = 0;
   while(i < n ){
        while(fr <= bk && a[i] <= a[arr[bk]])
            bk--;

        arr[++bk] = i;

        if(i - k == arr[fr])
            fr++;

        if(i >= k - 1)
            rez += a[arr[fr]];

      i++;
    }

    g << rez;
    return 0;
}