Pagini recente » Cod sursa (job #2456868) | Cod sursa (job #1371742) | Cod sursa (job #1648041) | Cod sursa (job #3181325) | Cod sursa (job #1037215)
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<ctime>
using namespace std;
int n,a[5000005],k,deq[5000005];
long long suma;
inline void Citire()
{
int i;
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d%d",&n,&k);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
}
inline void Rezolva()
{
int i,minim=11000000,poz,pr,ul;
pr=1;ul=0;
for (i=1;i<=k;i++)
{
while (ul>=pr && a[i]<=deq[ul])
ul--;
deq[++ul]=a[i];
if (a[i]<minim)
minim=a[i];
}
suma=suma+minim;
//sort ( deq + 1 , deq + 1 + k );
for (i=k+1;i<=n;i++)
{
while (ul>=pr && a[i]<=deq[ul])
ul--;
deq[++ul]=a[i];
if (a[i-k]==deq[pr])
pr++;
suma+=deq[pr];
}
printf("%lld",suma);
}
int main()
{
Citire();
Rezolva();
return 0;
}