Cod sursa(job #2374715)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 7 martie 2019 20:06:52
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <deque>
using namespace std;

deque<int> DQ, currItems;
int n, k, x;
long long total;

void rez()
{
    scanf("%d %d", &n, &k);
    for(int i = 0; i<k; i++)
    {
        scanf("%d", &x);
        currItems.push_front(x);
        while(!DQ.empty() && DQ.front() > x)
        {
            DQ.pop_front();
        }
        DQ.push_front(x);
    }
    total+=DQ.back();
    for(int i = k; i<n; i++)
    {
        if(DQ.back() == currItems.back())
            DQ.pop_back();
        currItems.pop_back();
        scanf("%d", &x);
        currItems.push_front(x);
        while(!DQ.empty() && DQ.front() > x)
        {
            DQ.pop_front();
        }
        DQ.push_front(x);
        total+=DQ.back();
    }
    printf("%lld", total);
}

int main()
{
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    rez();
    return 0;
}