Cod sursa(job #424120)

Utilizator darrenRares Buhai darren Data 24 martie 2010 16:45:28
Problema Deque Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include<fstream>
#include<deque>
using namespace std;

void read();
void write();

deque<int> d;
long long s;
int n, k;

int main() {
	read();
	write();
	return 0;
}

void read() {
	ifstream fin("deque.in");
	fin >> n >> k;
	int i, aux, bk;
	for (i = 0; i < k; ++i) {
		fin >> aux;
		while (!d.empty() && aux < d.front())
			d.pop_front();
		d.push_front(aux);
		if (aux == d.back())
			bk = i;
	}
	s += d.back();
	for (i = k; i < n; ++i) {		
		if (i - bk >= k)
			d.pop_back();
			
		fin >> aux;
		while (!d.empty() && aux < d.front())
			d.pop_front();
		
		d.push_front(aux);
		if (d.back() == aux)
			bk = i;
		
		s += d.back();
	}
	fin.close();
}

void write() {
	ofstream fout("deque.out");
	fout << s;
	fout.close();
}