Pagini recente » Cod sursa (job #881575) | Cod sursa (job #97224) | Cod sursa (job #610742) | Cod sursa (job #1538161) | Cod sursa (job #2352508)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
ll N, K;
deque < ll > d1, d2;
int main(){
freopen("deque.in", "r", stdin);
freopen("deque.out", "w", stdout);
scanf("%d%d", &N, &K);
ll S=0, Min;
for(ll i=1, x; i<=K; i++){
scanf("%lld", &x);
while(!d1.empty() && d1.back()>=x){
d1.pop_back();
d2.pop_back();
}
d1.push_back(x);
d2.push_back(i);
}
S = d1.front();
for(ll i=K+1, x; i<=N; i++){
scanf("%lld", &x);
while(!d1.empty() && d1.back()>=x){
d1.pop_back();
d2.pop_back();
}
d1.push_back(x);
d2.push_back(i);
while(!d1.empty() && d2.front()<i-K+1){
d1.pop_front();
d2.pop_front();
}
S += d1.front();
}
printf("%lld", S);
return 0;
}