Cod sursa(job #1459661)
Utilizator | Data | 10 iulie 2015 15:02:14 | |
---|---|---|---|
Problema | Deque | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.69 kb |
#include<cstdio>
using namespace std;
int n, k, i;
int v[5000010], Deque[5000010];
int prim, ult;
long long sum;
int main()
{
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++)
scanf("%d", &v[i]);
prim=1;
ult=0;
for(i = 1; i <= n; i++)
{
while (prim <= ult && v[i] <= v[ Deque[ult] ])
ult--;
Deque[++ult]=i;
if (Deque[prim] == i - k)
prim++;
if (i >= k)
sum += v[Deque[prim]];
}
printf("%lld\n", sum);
}