Cod sursa(job #2911336)

Utilizator maiaauUngureanu Maia maiaau Data 28 iunie 2022 16:32:58
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("deque.in");
ofstream g("deque.out");

int n, k, x;
int64_t s;
deque<pair<int, int>> d;

int main()
{
    f >> n >> k;
    for (int i = 1; i <= k; i++){
        f >> x;
        while (!d.empty() && d.back().first >= x) d.pop_back();
        d.push_back({x, i});
    }
    s += d.front().first;
    
    for (int i = k + 1; i <= n; i++){
        f >> x;
        while(!d.empty() && d.back().first >= x) d.pop_back();
        d.push_back({x, i});
        while(k <= i - d.front().second) d.pop_front();
        s += d.front().first;
    }
    
    g << s;
    return 0;
}

//