Cod sursa(job #3121279)

Utilizator AlexCroitoriuAlex Croitoriu AlexCroitoriu Data 11 aprilie 2023 15:49:02
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;
fstream f("deque.in", ios::in), g("deque.out", ios::out);
int main()
{
    int n, k, x;
    long long ans = 0;
    deque<pair<int, int>> q;
    f >> n >> k;
    for (int i = 1; i <= k; i++)
    {
        f >> x;
        while (!q.empty() && q.back().first > x)
            q.pop_back();
        q.push_back({x, i});
    }
    ans += q.front().first;
    for (int i = k + 1; i <= n; i++)
    {
        f >> x;
        while (!q.empty() && q.back().first > x)
            q.pop_back();
        while (!q.empty() && q.front().second <= i - k)
            q.pop_front();
        q.push_back({x, i});
        ans += q.front().first;
    }
    g << ans;
}