Cod sursa(job #2867385)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 10 martie 2022 12:24:22
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#pragma region Header
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <map>
#include <thread>
#include <mutex>
#include <future>

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int uint;
#define endl '\n'
using namespace std;
#if 1

#include <fstream>

ifstream fin("deque.in");
ofstream fout("deque.out");
#define cin fin
#define cout fout
#endif

#pragma endregion
const int nmx = 5000002;
int a[nmx];
int dq[nmx];
int main()
{
	
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	int front=1, back=0;
	ll sum=0;
	for (int i = 1; i <= n; i++) {
		while (a[dq[back]] > a[i] && front <= back)
			back--;
		dq[++back] = i;
		if (dq[front] == i - k)
			front++;
		if (i >= k)
			sum += a[dq[front]];
	}
	cout << sum;
}