Pagini recente » Cod sursa (job #1649453) | Cod sursa (job #193809) | Cod sursa (job #837308) | Cod sursa (job #25871) | Cod sursa (job #1316874)
#include <iostream>
#include <fstream>
using namespace std;
long long n,k,que[5000001],a[5000001],i,sum;
int first,last;
int main()
{
ifstream f("deque.in");
ofstream g("deque.out");
f>>n>>k;
for (i=1;i<=n;i++)
f>>a[i];
first=1;
last=0;
for (i=1;i<=n;i++)
{
while ((first <= last) && (a[i]<= a[ que [last] ])) last--;
que [++last]=i;
if (que[first]==i-k) first++;
if (i>=k) sum+=a[que[first]];
}
g<<sum;
return 0;
}