Cod sursa(job #2863252)

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

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

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

int n;
int a[50000005], k;
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';


	fin.close();
	fout.close();
	return 0;
}