Cod sursa(job #1901419)

Utilizator raduzxstefanescu radu raduzx Data 3 martie 2017 22:32:48
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>
#define maxn 5000020
using namespace std;
int a[maxn],ind[maxn];
long long s;
ifstream f("deque.in");
ofstream g("deque.out");
int main()
{
    int n,i,j,x,prim,last,k;
    f>>n>>k;
    for(i=1;i<=n;i++)
    {
        f>>a[i];
    }
    prim=1;
    last=0;
    for(i=1;i<=n;i++)
    {
        while(prim<=last and a[i]<=a[ind[last]])last--;
        last+=1;
        ind[last]=i;
        while(ind[prim]<=i-k)
            prim++;
        if(i>=k)
            s+=a[ind[prim]];
    }
    g<<s;
    return 0;
}