Pagini recente » Cod sursa (job #1380787) | Cod sursa (job #2917977) | Cod sursa (job #2880775) | Cod sursa (job #1481615) | Cod sursa (job #3276144)
#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;
}