Cod sursa(job #2355699)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 26 februarie 2019 11:37:58
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <bits/stdc++.h>
using namespace std;
int a[5000005], d[5000005];
int main()
{
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    int n, k, f = 0, l = 0;
    long long ans = 0;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) {
            while(f <= l && a[i] <= a[d[l]]) l--;
            d[++l] = i;
            if(d[f] == i - k) f++;
            if(i >= k) ans += a[d[f]];
    }
    printf("%lld", ans);
    return 0;
}