Cod sursa(job #2352508)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 23 februarie 2019 12:54:47
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

ll N, K;
deque < ll > d1, d2;

int main(){

    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);

    scanf("%d%d", &N, &K);
    ll S=0, Min;
    for(ll i=1, x; i<=K; i++){
        scanf("%lld", &x);
        while(!d1.empty() && d1.back()>=x){
            d1.pop_back();
            d2.pop_back();
        }
        d1.push_back(x);
        d2.push_back(i);
    }
    S = d1.front();

    for(ll i=K+1, x; i<=N; i++){
        scanf("%lld", &x);
        while(!d1.empty() && d1.back()>=x){
            d1.pop_back();
            d2.pop_back();
        }
        d1.push_back(x);
        d2.push_back(i);
        while(!d1.empty() && d2.front()<i-K+1){
            d1.pop_front();
            d2.pop_front();
        }
        S += d1.front();
    }

    printf("%lld", S);

    return 0;
}