Cod sursa(job #3276144)

Utilizator IleaIlea Bogdan Ilea Data 12 februarie 2025 19:23:20
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <deque>
using namespace std;

deque<pair<long long, int>> q;
int n, k;
signed main(){
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    cin>>n>>k;
    long long sum=0;
    for(int i = 1; i <= n; i ++) {
        long long x;
        cin>>x;
        if(q.size() && q.back().second<=i-k){
            q.pop_back();
        }
        while(q.size() && q.front().first>=x){
            q.pop_front();
        }
        q.push_front({x, i});
        if(i>=k){
            sum+=q.back().first;
        }
    }
    cout<<sum;
    return 0;
}