Cod sursa(job #2596739)

Utilizator CharacterMeCharacter Me CharacterMe Data 10 aprilie 2020 12:14:45
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
typedef long long ll;

int n, k;
ll sol;
int nums[5000005];
deque<int> d;

int main()
{

    fin >> n >> k;
    for(int i = 1; i <= n; ++i){
        fin >> nums[i];

        while(!d.empty() && d.front() + k <= i) d.pop_front();
        while(!d.empty() && nums[d.back()] > nums[i]) d.pop_back();
        d.push_back(i);

        if(i >= k)sol += 1LL*nums[d.front()];
    }

    fout << sol;

    return 0;
}