Pagini recente » Cod sursa (job #3203854) | Cod sursa (job #665552) | Cod sursa (job #3183102) | Cod sursa (job #5543) | Cod sursa (job #3287894)
#include <bits/stdc++.h>
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
deque<pair<int,int>> q;
int n,k,i,x;
long long s;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
in>>n>>k;
while(n--)
{
++i;
in>>x;
while(!q.empty() && q.front().second<=i-k)
q.pop_front();
while(!q.empty() && q.back().first>x)
q.pop_back();
q.push_back({x,i});
if(i>=k)
s+=q.front().first;
}
out<<s;
}