Pagini recente » Cod sursa (job #1264526) | Cod sursa (job #1203641) | Cod sursa (job #2156727) | Cod sursa (job #799865) | Cod sursa (job #2758421)
#include <bits/stdc++.h>
#define fx first
#define sx0 second
#define pb push_back
typedef long long ll;
const int Max = 100006;
const int Inf = 2e9;
const int MOD = 666013;
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
deque <ll> q;
void add_in (ll a) {
while (!q.empty() && q.back() > a) q.pop_back();
q.push_back(a);
}
void remove (ll a) {
if (q.front() == a) q.pop_front();
}
void solve () {
ll n, k;
in >> n >> k;
ll a[n+6];
for (int i=1; i<=n; i++) in >> a[i];
ll ans = 0;
for (ll i=1; i<=n; i++) {
add_in(a[i]);
if (i - k > 0) remove(a[i-k]);
if (i >= k) ans += q.front();
}
out << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}