Pagini recente » Cod sursa (job #116841) | Cod sursa (job #2216596) | Cod sursa (job #3206593) | Cod sursa (job #2671430)
#include <fstream>
#include <deque>
#include <utility>
using namespace std;
typedef long long ll;
deque<pair<int,int> > Q;
ifstream fin("deque.in");
ofstream fout("deque.out");
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
int n,i,x,k;
ll ans;
fin>>n>>k;
ans=0;
for(i=1;i<=n;i++){
fin>>x;
while(!Q.empty() and Q.back().first>x)
Q.pop_back();
Q.push_back(make_pair(x,i));
if(Q.front().second<=i-k)
Q.pop_front();
if(i>=k)
ans=ans+Q.front().first;
}
fout<<ans;
fin.close();
fout.close();
return 0;
}