Cod sursa(job #2570689)

Utilizator ciutanpCiuta Andrei Calin ciutanp Data 4 martie 2020 18:36:16
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int NAX = 5 * 1e6 + 5;

ifstream f("deque.in");
ofstream g("deque.out");

ll s;
int v[ NAX ], n, k;
deque<int>Q;

int main()
{
    f >> n >> k;
    for(int i = 1 ; i <= n ; ++i)
    {
        f >> v[ i ];
        while(!Q.empty() && v[ i ] <= v[ Q.back() ])
            Q.pop_back();
        Q.push_back(i);

        if( i >= k){
        if(i - Q.front() == k )
        {
            Q.pop_front();
        }

            s += v[ Q.front() ];
        }
    }
    g << s;
}