Cod sursa(job #2466716)

Utilizator Carol_LucaCarol Luca Carol_Luca Data 2 octombrie 2019 20:26:06
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("deque.in");

ofstream fout ("deque.out");

deque<pair<int,int>> q;

int n, r;

long long x, s;

int main()

{

    fin >> n >> r;

    for(int i=1; i<=r; i++)

    {

        fin >> x;

        while(!q.empty() && x < q.back().first)

            q.pop_back();

        q.push_back({x, i});

    }

    s+=q.front().first;

    for(int i=r+1; i<=n; i++)

    {

        fin >> x;

        while(!q.empty() && x < q.back().first)

            q.pop_back();

        if(!q.empty())

            if(q.front().second <= i-r)

                q.pop_front();

        q.push_back({x, i});

        s+=q.front().first;

    }

    fout << s;

    return 0;

}