Cod sursa(job #2264383)

Utilizator Valentin0709Datcu George Valentin Valentin0709 Data 20 octombrie 2018 08:58:23
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

#define NMAX 5000010

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

int n, k, a[NMAX];
long long s;
deque<int> d;

void read() {
    f >> n >> k;
    for(int i = 1; i <= n; i++) f >> a[i];
}

void solve() {
    for(int i = 1; i <= n; i++) {
        while(!d.empty() && a[d.back()] >= a[i]) d.pop_back();
        d.push_back(i);
        if(i - d.front() == k) d.pop_front();
        if(i >= k) s += a[d.front()];
    }

    g << s;
}

int main() {

    read();
    solve();

    return 0;
}