Cod sursa(job #2732258)

Utilizator NSA-16Neacsu-Tranciuc Sasa-Andrei NSA-16 Data 28 martie 2021 20:42:07
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int l[5000001], i[5000001];
int main()
{
    int n, k, d = 0, s = 1;
    long long rez = 0;
    f>>n>>k;
    for (int c = 1;c <= n;c++)
    {
        f >> l[c];
    }
    for (int c = 1;c <= n;c++)
    {
        while(s <= d && l[c] <= l[i[d]])
        {
            d--;
        }
        i[++d] = c;
        if(i[s] == c - k)
        {
            s++;
        }
        if(c >= k)
        {
            rez += l[i[s]];
        }
    }
    g<<rez;
    return 0;
}