Pagini recente » Cod sursa (job #1552286) | Cod sursa (job #2615530) | Cod sursa (job #2365658) | Cod sursa (job #2217659) | Cod sursa (job #1615894)
#include <iostream>
#include <deque>
using namespace std;
const int mxn = 5000010;
int a[mxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
deque < pair<int,int> > cand;
for (int i = 0; i < k - 1; ++i) {
while (!cand.empty() && cand.back().first > a[i]) {
cand.pop_back();
}
cand.push_back( make_pair(a[i], i) );
}
long long ans = 0;
for (int i = k - 1; i < n; ++i) {
while (!cand.empty() && cand.front().second <= i - k) {
cand.pop_front();
}
while (!cand.empty() && cand.back().first > a[i]) {
cand.pop_back();
}
cand.push_back( make_pair(a[i], i) );
ans += cand.front().first;
}
cout << ans << "\n";
}