Cod sursa(job #2258693)

Utilizator Constantin.Dragancea Constantin Constantin. Data 11 octombrie 2018 20:30:51
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;
typedef long long ll;

deque <pii> Q;
int n, k;
ll ans;

int main(){
    ifstream cin ("deque.in");
    ofstream cout ("deque.out");
    cin >> n >> k;
    for (int i=1, x; i<k; i++){
        cin >> x;
        while (!Q.empty() && Q.back().first >= x) Q.pop_back();
        Q.push_back({x, i});
    }
    for (int i=k, x; i<=n; i++){
        cin >> x;
        while (!Q.empty() && Q.back().first >= x) Q.pop_back();
        Q.push_back({x, i});
        if (Q.front().second <= i-k) Q.pop_front();
        ans += Q.front().first;
    }
    cout << ans;
    return 0;
}