Cod sursa(job #3240127)

Utilizator winemomComan Erin winemom Data 12 august 2024 18:11:42
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int n, k, x;
long long rez;
deque <pair <int, int>> dq;

int main()
{
    f >> n >> k >> x;
    dq.push_back(make_pair(x, 1));
    for(int i=2; i<=k; i++)
    {
        f >> x;
        while(x <= dq.back().first && !dq.empty())
            dq.pop_back();
        dq.push_back(make_pair(x, i));
    }
    rez = dq.front().first;

    for(int i=k+1; i<=n; i++)
    {
        f >> x;
        while(x <= dq.back().first && !dq.empty())
            dq.pop_back();
        dq.push_back(make_pair(x, i));

        while(dq.front().second <= i-k)
            dq.pop_front();
        rez +=dq.front().first;
    }
    g << rez;
}