Cod sursa(job #430487)

Utilizator SpiderManSimoiu Robert SpiderMan Data 31 martie 2010 08:50:47
Problema Deque Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>

#define MAX 5000005
#define hg 8192

int N, K;
int V[MAX], Q[MAX];
int st = 0, dr = 1, poz;
long long rez;
char ch[hg];

inline void cit (int &x)
{
    int semn = 0;
    x = 0;
    if (ch[0] == '\0') fread (ch, 1, hg, stdin);
    else while ((ch[poz] < '0' || ch[poz] > '9') && ch[poz]!='-')
            if (++poz == hg)
                fread (ch, 1, hg, stdin), poz = 0;

    if (ch[poz] == '-')
    {
        semn = 1;
        if (++poz == hg)
            fread(ch, 1, hg, stdin), poz = 0;
    }


    while (ch[poz] >= '0' && ch[poz] <= '9')
    {
        x = x * 10 + ch[poz] - '0';
        if (++poz == hg)
            fread (ch, 1, hg, stdin), poz = 0;
    }
    if (semn) x = -x;
}

int main()
{
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);

    int i;

   scanf("%d %d ", &N, &K);  //cit(N) , cit(K);


    for (i = 1; i <= N; i++)
        scanf("%d ", &V[i]);//cit(V[i]);

    for (i = 1; i <= N; i++)
    {
        for (; st <= dr && V[i] <= V[Q[dr]]; --dr) ;

        Q[++dr] = i;

        if (Q[st] == i - K) st++;

        if (i >= K) rez += V[Q[st]];
    }

    printf("%lld\n", rez);

    return 0;
}