Cod sursa(job #1494849)

Utilizator alexandru822Bosinta Alexandru alexandru822 Data 1 octombrie 2015 22:03:25
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>
#define SIZE_MAX 5000001
using namespace std;
ofstream out("deque.out");
ifstream in("deque.in");
int deq[SIZE_MAX], v[SIZE_MAX];
main()
{
    int n, k, top = 0, bottom = 0;
    long long sum = 0;
    in >> n >> k;
    for(int i=1; i<=n; i++)
    {
        in >> v[i];
        while(top > bottom && v[i] < v[deq[top-1]])
            top --;
        deq[top++] = i;
        if(deq[bottom] == i-k)
            bottom ++;
        if(i>=k) sum += v[deq[bottom]];

    }
    out << sum;

}