Cod sursa(job #3176026)

Utilizator AlexCroitoriuAlex Croitoriu AlexCroitoriu Data 26 noiembrie 2023 17:21:29
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#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;
}