Cod sursa(job #2862068)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 4 martie 2022 20:48:14
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include "bits/stdc++.h"
 
using namespace std;
 
using ld = long double;
using ll = long long;
using ull = unsigned long long;
 
#if defined(ONPC)
#include "bits/debug.h"
#endif

const int mxN = 5e6;
int v[mxN];
 
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    
    int n, k;
    cin >> n >> k;
    deque<int> Q;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
        while (!Q.empty() && v[i] < v[Q.back()]) {
            Q.pop_back();
        }
        Q.push_back(i);
        if (Q.front() == i - k) Q.pop_front();
        if (i + 1 >= k) {
            ans += v[Q.front()];
        }
    }
    cout << ans << "\n";
}