Cod sursa(job #2553839)

Utilizator MarcGrecMarc Grec MarcGrec Data 22 februarie 2020 12:21:15
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <deque>
#include <queue>
#include <cstdint>
#include <utility>
using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

int n, k;
deque<int> Dq;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;

int main()
{
	fin >> n >> k;
	
	for (int i = 1, x; i <= k; ++i)
	{
		fin >> x;
		Dq.push_back(x);
		Q.emplace(x, i);
	}
	
	int64_t r = Q.top().first;
	
	for (int i = k + 1, x; i <= n; ++i)
	{
		fin >> x;
		Dq.pop_front();
		Dq.push_back(x);
		while (!Q.empty() && (Q.top().second < (i - k + 1)))
		{ 
			Q.pop();
		}
		Q.emplace(x, i);
		r += Q.top().first;
	}
	
	fout << r;
	
	fin.close();
	fout.close();
	return 0;
}