Cod sursa(job #3307425)

Utilizator InformaticianInDevenire1Munteanu Mihnea Gabriel InformaticianInDevenire1 Data 20 august 2025 17:24:10
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;

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

deque <pair <int,int>> dq;

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