Pagini recente » Cod sursa (job #2081769) | Cod sursa (job #2649740) | Cod sursa (job #2142324) | Cod sursa (job #2805145) | Cod sursa (job #2227357)
#include <stdio.h>
#include <ctype.h>
#define BUFFER_SIZE 1 << 17
char buffer[BUFFER_SIZE];
int p = BUFFER_SIZE;
inline char next()
{
if(p == BUFFER_SIZE) fread(buffer, 1, BUFFER_SIZE, stdin), p = 0;
return buffer[p++];
}
inline int get_int()
{
int x = 0, semn = 1;
char ch = next();
while(!isdigit(ch) && ch != '-') ch = next();
if(ch == '-') ch = next(), semn = -1;
while(isdigit(ch))
x = x * 10 + ch - '0', ch = next();
return x * semn;
}
int main()
{
int N, K, v[5000001], deque[5000001], front = 1, back = 0;
long long sum = 0;
freopen("deque.in", "r", stdin);
freopen("deque.out", "w", stdout);
N = get_int(); K = get_int();
for(int i = 1; i <= N; i++)
{
v[i] = get_int();
}
for(int i = 1; i < K; i++)
{
while(front <= back && v[i] <= v[deque[back]])
{
back--;
}
deque[++back] = i;
}
for(int i = K; i <= N; i++)
{
while(front <= back && v[i] <= v[deque[back]])
{
back--;
}
deque[++back] = i;
if(deque[front] == i - K) front++;
sum += v[deque[front]];
}
printf("%lld", sum);
return 0;
}