Pagini recente » Cod sursa (job #2448110) | Cod sursa (job #2506069) | Cod sursa (job #2497725) | Planificare infoarena | Cod sursa (job #3215618)
#include <fstream>
#include <deque>
#include <vector>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
int n,k;
deque<int> dp;
vector<int> A;
int main()
{
cin>>n>>k;
A.resize(n);
for(int i=0;i<k-1;i++)
{
cin>>A[i];
while(dp.size() && A[dp[dp.size()-1]]>A[i])
dp.pop_back();
dp.push_back(i);
}
long long s=0;
for(int i=k-1;i<n;i++)
{
cin>>A[i];
while(dp.size() && A[dp[dp.size()-1]]>A[i])
dp.pop_back();
dp.push_back(i);
if(dp[0]<=i-k)
dp.pop_front();
s=s+A[dp[0]];
}
cout<<s;
return 0;
}