Cod sursa(job #3226109)

Utilizator InformaticianInDevenire1Munteanu Mihnea Gabriel InformaticianInDevenire1 Data 20 aprilie 2024 09:05:47
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("deque.in");
ofstream fout ("deque.out");

deque <pair<int,int>> d;

int main()
{
    long long n,k,S = 0,Min = 999999999,x;
    fin >> n >> k;
    for (int i=1;i<=n;++i){
        fin >> x;
        while (!d.empty() and d.back().first>=x()) d.pop_back();
        while (!d.empty() and i-d.front().second=>k)d.pop_front();
        d.push_back({x,i});
        if (i>=k){
            S+=d.front().first;
        }
    }
    fout << S;
    return 0;
}