Cod sursa(job #739397)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 22 aprilie 2012 22:42:21
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.48 kb
#include <fstream>
#define LE 5000004
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int V[LE],d[LE],s,i,n,start,k;
long long suma;
int main()
{
	f>>n>>k;
	for(i=1;i<=n;++i) f>>V[i];
	
	start=1;
	V[0]=-1*LE*10;
	
	for(i=1;i<=n;++i)
	{
		while (V[i]<V[d[s]]&&s>=start) 
			--s;
		d[++s]=i;
		
		while (d[start]<=i-k) 
			++start;
		
		if (i>=k) 
	      suma+=V[d[start]];
	}
	g<<suma<<'\n';
     f.close();
     g.close();	 
	return 0;
}