Cod sursa(job #3212612)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 11 martie 2024 23:13:36
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x << ": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "deque";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n, k;
int a[5000005];
deque<int> d;
ll ans;

int main()
{
    fin >> n >> k;
    for (int i = 1; i <= n; ++i)
        fin >> a[i];
    for (int i = 1; i <= n; ++i)
    {
        while (!d.empty() && a[i] <= a[d.back()])
            d.pop_back();
        while (!d.empty() && d.front() <= i - k)
            d.pop_front();
        d.push_back(i);
        if (i >= k)
            ans += a[d.front()];
    }
    fout << ans << "\n";
    return 0;
}