Pagini recente » Cod sursa (job #2601084) | Cod sursa (job #1931773) | Istoria paginii runda/simulara.oji.2015.9d/clasament | Cod sursa (job #2340272) | Cod sursa (job #2203590)
// https://goo.gl/fBmFxu
#include <bits/stdc++.h>
using namespace std;
#define NMAX 5000005
#define MMAX 200009
#define kInf (1 << 30)
#define kInfLL (1LL << 60)
#define kMod 666013
#define edge pair<int, int>
#define x first
#define y second
#define USE_FILES "MLC"
#ifdef USE_FILES
#define cin fin
#define cout fout
ifstream fin("input");
ofstream fout("output");
#endif
// number of tests from "in"
int test_cnt = 1;
void clean_test();
// your global variables are here
int N, K, a[NMAX];
long long sol;
deque <int> d;
// your solution is here
void solve() {
cin >> N >> K;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
while (d.size() && a[d.back()] >= a[i]) {
d.pop_back();
}
d.push_back(i);
if (i - d.front() >= K) {
d.pop_front();
}
if (i >= K) {
sol += 1LL * a[d.front()];
}
}
cout << sol << '\n';
if (test_cnt > 0) {
clean_test();
}
}
void clean_test() {
// clean if needed
}
int main() {
// cin >> test_cnt;
while (test_cnt--) {
solve();
}
return 0;
}