Cod sursa(job #3275669)

Utilizator EricRaiaEricRaia EricRaia Data 11 februarie 2025 14:22:29
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>

using namespace std;

ifstream cin ("deque.in");
ofstream cout ("deque.out");

const int NMAX=5000001;
int v[NMAX],vv[NMAX];

long long s,n,k,inc,sf;

int main()
{
    inc=sf=1;
    cin>>n>>k;
    for(int i=1;i<=n;++i){

        cin>>v[i];
        while(sf>=inc && v[vv[sf]]>=v[i]  )
            --sf;
        ++sf;
        vv[sf]=i;

        if(i>=k){
            if(i-vv[inc]+1>k)
                inc++;
            s+=v[vv[inc]];

        }
    }
    cout<<s;

    return 0;
}