Pagini recente » Cod sursa (job #2853270) | Cod sursa (job #2603637) | Cod sursa (job #3000182)
#include <fstream>
#include <deque>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
int n,k;
deque<int> M;
long long a[5000001];
int main()
{
cin>>n>>k;
for(int i=1;i<k;i++)
{
cin>>a[i];
while(!M.empty() && a[M.back()]>=a[i])
M.pop_back();
M.push_back(i);
}
long long s=0;
for(int i=k;i<=n;i++)
{
cin>>a[i];
while(!M.empty() && a[M.back()]>=a[i])
M.pop_back();
M.push_back(i);
if(M.front()<=i-k)
M.pop_front();
s=s+a[M.front()];
}
cout<<s;
return 0;
}