Cod sursa(job #3285322)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 12 martie 2025 18:31:26
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;

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

deque < pair <int,int> > q;
long long s;
int n, k;

int main()
{
    int i, x;
    fin >> n >> k;
    for (i = 1; i <= n; i++)
    {
        fin >> x;
        while (!q.empty() && q.back().first > x) q.pop_back();
        while (!q.empty() && q.front().second < i - k + 1) q.pop_front();
        q.push_back({x, i});
        if (i >= k) s += q.front().first;
    }
    fout << s << "\n";
    return 0;
}
//0, 1, 3-5, 7-18, 21, 22, 24-27, 29, 30, 34-36, 42, 44, 45, 47, 48, 51, 54, 56-58