Cod sursa(job #3326172)

Utilizator SkibidiCezarCezar Bolba SkibidiCezar Data 27 noiembrie 2025 16:46:58
Problema Deque Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("deque.in");
ofstream fout ("deque.out");
long long n, k, sum, st, dr;
struct el{
    int v, s, d;
};
el a[5000005];
stack <int> sky;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    fin >> n >> k;
    for(int i = 1; i <= n; i++){
        fin >> a[i].v;
        while(!sky.empty() && a[i].v <= a[sky.top()].v){
            a[sky.top()].d = i;
            sky.pop();
        }
        sky.push(i);
    }
    while(!sky.empty()){ sky.pop(); }
    for(int i = n; i >= 1; i--){
        if(a[i].d == 0) a[i].d = n + 1;
        while(!sky.empty() && a[i].v <= a[sky.top()].v){
            a[sky.top()].s = i;
            sky.pop();
        }
        sky.push(i);
    }
    for(int i = 1; i <= n; i++){
        //cout << a[i].s << " " << a[i].d << "\n";
        st = max(1LL * (a[i].s + 1), 1LL * (i - k + 1));
        dr = min(1LL * (a[i].d - k), 1LL * i);
        if(st <= dr){
            sum += a[i].v * (dr - st + 1);
        }
    }
    fout << sum;
    return 0;
}