Cod sursa(job #2731639)

Utilizator NT-SANeacsu-Tranciuc Sasa-Andrei NT-SA Data 28 martie 2021 00:02:21
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;
}