Cod sursa(job #2881442)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 30 martie 2022 15:10:46
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "deque";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");


int n, k;
int a[5000005];
deque<int> d;
int main() {
	int ans = 0;
	fin >> n >> k;
	for (int i = 1; i <= n; ++i)
		fin >> a[i];
	for (int i = 1; i <= n; ++i) {
		while (!d.empty() && a[i] <= a[d.back()])
			d.pop_back();
		while (!d.empty() && d.front() <= i - k)
			d.pop_front();
		d.push_back(i);
		if (i >= k)
			ans += a[d.front()];
	}
	fout << ans << '\n';

	return 0;
}