Cod sursa(job #2657967)

Utilizator Vaida_Radu_AndreiVaida Radu Andrei Vaida_Radu_Andrei Data 12 octombrie 2020 19:06:18
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <cstdio>
#include <deque>

using namespace std;

struct Nr {
    int pos;
    long long int value;
};

void solve() {

}

int main() {
    freopen("deque.in","r",stdin);
    freopen("deque.out","w",stdout);
    int i,n,k;
    long long int newVal,sol;
    Nr newNr;
    deque <Nr> q;
    scanf("%d%d",&n,&k);
    for(i=1;i<k;++i) {
        scanf("%lld",&newVal);
        newNr.value=newVal;
        newNr.pos=i;
        for(;!q.empty()&&newNr.value<q.back().value;q.pop_back());
        q.push_back(newNr);
    }
    for(sol=0;i<=n;++i) {
        if(q.front().pos==i-k) {
            q.pop_front();
        }
        scanf("%lld",&newVal);
        newNr.value=newVal;
        newNr.pos=i;
        for(;!q.empty()&&newNr.value<q.back().value;q.pop_back());
        q.push_back(newNr);
        sol+=q.front().value;
    }
    printf("%lld",sol);
    return 0;
}