Cod sursa(job #2041221)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 16 octombrie 2017 22:42:00
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
struct nod
{ long long val;
  long long poz;
};
nod st[5000001];
long long n,k,x,top,pr,sum;
int main()
{ fin>>n>>k;
int i;
fin>>x;
pr=1;
top=1;
st[1].val=x;
st[1].poz=1;
 for(i=2;i<=n;i++)
   {fin>>x;
    if(i-st[pr].poz+1>k) pr++;
    while(x<st[top].val&&top>=pr) top--;
    top++;
    st[top].val=x;
    st[top].poz=i;
    if(i>=k) sum+=st[pr].val;
   }
   fout<<sum;
    return 0;
}