Cod sursa(job #3358731)

Utilizator DavidboangiuBoangiu David Andrei Davidboangiu Data 19 iunie 2026 17:16:43
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
struct numar
{
    int poz,val;
};
deque <numar> q;
numar y;
long long s,i,n,k,x;
int main()
{
    f >> n >> k;
    for(i=1; i<=n; i++)
    {
        f>>x;
        y.poz=i;
        y.val=x;
        if(!q.empty() and i-q.front().poz+1>k) q.pop_front();
        while(!q.empty() and q.back().val>y.val)
            q.pop_back();
        q.push_back(y);
        if(i>=k) s+=q.front().val;
    }
    g<<s;
}