Cod sursa(job #2866513)

Utilizator DooMeDCristian Alexutan DooMeD Data 9 martie 2022 19:22:13
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.42 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nmax = 5e6;

ll v[nmax+5];

int main() {
	ifstream f("deque.in");
	ofstream g("deque.out");
	
	int n,k; f >> n >> k;
	deque<int> q;
	ll s = 0;
	for(int i=1; i<=n; i++) {
		f >> v[i];
		while(!q.empty() and v[i]<=v[q.back()]) q.pop_back();
		q.push_back(i);
		while(q.front()<=i-k) q.pop_front();
		if(i>=k) s += v[q.front()];
	}
	g << s;
	return 0;
}