Cod sursa(job #3341538)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 19 februarie 2026 21:13:25
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include "bits/stdc++.h"
#define int long long

const int NMAX = 5e6;

int n, k, v[NMAX + 5], ans;

std :: deque < int > dq;

void Solve(){
    std :: cin >> n >> k;
    for(int i = 1; i <= n; i++){
        std :: cin >> v[i];
    }

    for(int i = 1; i <= k; i++){
        while(!dq.empty() and v[i] < v[dq.back()]){
            dq.pop_back();
        }
        dq.push_back(i);
    }

    for(int i = k + 1; i <= n; i++){
        ans += v[dq.front()];
        if(dq.front() == i - k){
            dq.pop_front();
        }
        while(!dq.empty() and v[dq.back()] > v[i]){
            dq.pop_back();
        }
        dq.push_back(i);
    }
    ans += v[dq.front()];
    std :: cout << ans;
}

signed main(){
    std :: ios_base :: sync_with_stdio(false);
    std :: cin.tie(nullptr);

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

    Solve();

    return 0;
}