Cod sursa(job #2975815)

Utilizator CobzaruAntonioCobzaru Paul-Antonio CobzaruAntonio Data 7 februarie 2023 17:22:44
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#define ll long long
using namespace std;
ifstream cin ("deque.in");
ofstream cout ("deque.out");
int a[5000005];
int deq[5000005];
ll answ;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int i,j,n,k;
    cin >> n >> k;
    for (i=1;i<=n;i++)
        cin >> a[i];
    int st = 1,dr = 0;
    for (i=1;i<=n;i++)
    {
        while (st<=dr && a[i]<=a[deq[dr]])
            dr--;
        deq[++dr] = i;
        if (deq[st] == i - k)
            st++;
        if (i>=k)
            answ = answ + a[deq[st]];
    }
    cout << answ;
    return 0;
}