Cod sursa(job #2466362)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 1 octombrie 2019 22:34:36
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 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;
}