Cod sursa(job #3218457)

Utilizator Ics.maker09Iancu Cezar-Stefan Ics.maker09 Data 27 martie 2024 11:28:53
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.43 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque <int> st;
long long n,k,x,v[5000005],S,i;
int main()
{
    f >>n >>k;
    for(i=1;i<=n;i++)
    {
        f>>v[i];
        while(!st.empty() && st.back()<=i-k)st.pop_back();
        while(!st.empty() && v[i]<v[st.front()])st.pop_front();
        st.push_front(i);
        if(i>=k)S+=v[st.back()];
    }
    g<<S;
}