Cod sursa(job #2979591)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 15 februarie 2023 16:48:34
Problema Deque Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
/// [A][M][C][B][N] ///
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const char sp = ' ', nl = '\n';
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    fin >> n >> k;
    deque<pair<int, int>> dq;
    dq.push_back({ 0, 0 });
    for (int i = 1; i < k; ++i) {
        int x;
        fin >> x;
        dq.push_back({ i, x });
    }
    long long ans = 0;
    for (int i = k; i <= n; ++i) {
        int x;
        fin >> x; 
        while (dq.size() && i - dq.front().first >= k) {
            dq.pop_front();
        }
        while (dq.size() && dq.back().second > x) {
            dq.pop_back();
        }
        dq.push_back({ i, x });
        ans += dq.front().second;
    }
    fout << ans;
}