Cod sursa(job #3039370)

Utilizator VertimaXxFlorea Vlad VertimaXx Data 28 martie 2023 14:22:56
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
deque<int> dechiu;
long long int n,v[5000005],k,sol;


int main()
{
    ifstream cin("deque.in");
    ofstream cout("deque.out");
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> v[i];
    }
    for(int i = 1; i <= n; i++){
        while(!dechiu.empty() && v[dechiu.back()] >= v[i]){
            dechiu.pop_back();
        }
        dechiu.push_back(i);
        while(i-dechiu.front()+1 > k){
            dechiu.pop_front();
        }
        if(i >= k){
            sol += v[dechiu.front()];
        }
    }
    cout << sol;
}