Pagini recente » Cod sursa (job #3278142) | Cod sursa (job #2514227) | Cod sursa (job #2920903) | Cod sursa (job #2514225) | Cod sursa (job #1146982)
#include <fstream>
#include <deque>
using namespace std;
int v[5000500];
deque<int> dq;
int main()
{
ifstream in("deque.in");
ofstream out("deque.out");
int n,k,i;
long long s=0;
in>>n>>k;
for(i=1;i<=n;++i){
in>>v[i];
while(!dq.empty() && v[dq.back()]>v[i]) dq.pop_back();
dq.push_back(i);
if(i-dq.front()==k) dq.pop_front();
if(i>=k) s=s+v[dq.front()];
}
out<<s;
return 0;
}