Pagini recente » Cod sursa (job #1088067) | Cod sursa (job #831171) | Cod sursa (job #2948253) | Cod sursa (job #1246220) | Cod sursa (job #2613484)
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define maxn 5000010
long long Sum;
int N, K, L;
int A[maxn];
int H[maxn], P[maxn];
void push(int x)
{
int aux;
while (x/2>=1 && A[H[x]] < A[H[x/2]])
{
aux = H[x], H[x] = H[x/2], H[x/2] = aux;
P[H[x]] = x;
P[H[x/2]] = x/2;
x /= 2;
}
}
void pop(int x)
{
int aux, y = 0;
while (x != y)
{
y = x;
if (y*2<=L && A[H[x]]>A[H[y*2]]) x = y*2;
if (y*2+1<=L && A[H[x]]>A[H[y*2+1]]) x = y*2+1;
aux = H[x], H[x] = H[y], H[y] = aux;
P[H[x]] = x;
P[H[y]] = y;
}
}
int main()
{
freopen("deque.in", "r", stdin);
freopen("deque.out", "w", stdout);
scanf("%d %d ", &N, &K);
int i;
for (i = 1; i <= N; i++)
{
scanf("%d ", &A[i]);
L++;
H[L] = i;
P[H[L]] = L;
push(L);
while (H[1] <= i-K)
{
H[1] = H[L--];
P[H[1]] = 1;
pop(1);
}
if (i >= K) Sum += A[H[1]];
}
printf("%lld\n", Sum);
return 0;
}