Cod sursa(job #2999384)

Utilizator FastmateiMatei Mocanu Fastmatei Data 10 martie 2023 22:13:05
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

deque<pair<int, int>>q;
int n, k;

int main()
{
	fin >> n >> k;
	long long s = 0;
	for (int i = 1; i <= n; i++)
	{
		int x;
		fin >> x;
		while (!q.empty() && q.back().first >= x)
			q.pop_back();
		q.push_back({ x,i });
		while (!q.empty() && q.front().second <= i - k)
			q.pop_front();
		if (i >= k) s += q.front().first;
	}
	fout << s << "\n";
}