Pagini recente » Cod sursa (job #76073) | Cod sursa (job #1339567) | Cod sursa (job #182755) | Cod sursa (job #270600) | Cod sursa (job #2227311)
///just wondering how this is faster than mine..
#include <stdio.h>
#include <ctype.h>
#include <deque>
using namespace std;
FILE *fin = fopen("deque.in", "r");
FILE *fout = fopen("deque.out", "w");
#define BUF 1 << 17
char buf[BUF];
int pos = BUF;
inline char next() {
if(pos == BUF)
fread(buf, 1, BUF, fin), pos = 0;
return buf[pos++];
}
inline int read() {
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;
}
#define MAXN 5000001
deque <int> q;
int N, K;
int v[MAXN];long long sum;
int main() {
N = read(), K = read();
for(int i = 1;i <= N;i++) {
v[i] = read();
}
for(int i = 1;i <= N;i++) {
while(!q.empty() && v[i] <= v[q.back()])
q.pop_back();
q.push_back(i);
if(q.front() == i - K)
q.pop_front();
if(i >= K)
sum += 1LL * v[q.front()];
}
fprintf(fout, "%lld", sum);
fclose(fin);
fclose(fout);
return 0;
}