Pagini recente » Cod sursa (job #1515235) | Cod sursa (job #2389014) | Cod sursa (job #2917743) | Cod sursa (job #1562868) | Cod sursa (job #2227348)
#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], d[5000001], f = 1, b = 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(f <= b && v[i] <= v[d[b]])
{
b--;
}
d[++b] = i;
}
for(int i = K; i <= N; i++)
{
while(f <= b && v[i] <= v[d[b]])
{
b--;
}
d[++b] = i;
if(d[f] == i - K) f++;
sum += v[d[f]];
}
printf("%lld", sum);
return 0;
}