Pagini recente » Cod sursa (job #2619784) | Cod sursa (job #297285) | Cod sursa (job #2579337) | Cod sursa (job #2982520) | Cod sursa (job #2164924)
#include<fstream>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int n,k,inceput,sfarsit,q[5000001],mi,i,x,j,a[5000001];
long long s;
int main()
{
f>>n>>k;
inceput=1;
for(i=1;i<=n;i++)
f>>a[i];
for(i=1;i<=n;i++)
{
while(inceput<=sfarsit&&a[i]<=a[q[sfarsit]])
sfarsit--;
q[++sfarsit]=i;
if(q[inceput]==i-k)
inceput++;
if (i>=k)
s+=a[q[inceput]];
}
g<<s;
return 0;
}