Pagini recente » Atasamentele paginii Profil marina | Profil comisie_baraj_shumen_ichb_vianu_juniori | Diferente pentru notiuni-de-geometrie-si-aplicatii intre reviziile 7 si 8 | Cod sursa (job #748403) | Cod sursa (job #3176026)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
freopen("deque.in", "r", stdin);
freopen("deque.out", "w", stdout);
ll n, k, ans = 0;
cin >> n >> k;
deque<pair<ll, ll>> q;
for (ll i = 1, x; i <= n; i++)
{
cin >> x;
while (!q.empty() && q.front().second <= i - k)
q.pop_front();
while (!q.empty() && q.back().first >= x)
q.pop_back();
q.push_back({ x, i });
if (i >= k)
ans += q.front().first;
}
cout << ans;
}