Cod sursa(job #3342589)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 24 februarie 2026 20:40:14
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define dbg(x) #x << " = " << x
typedef long long ll;

const int nmax = 5e6;
int N, K;
int a[nmax + 5];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
	freopen("deque.in", "r", stdin);
	freopen("deque.out", "w", stdout);
    cin >> N >> K;
    for (int i = 1; i <= N; ++i) {
        cin >> a[i];
    }
    multiset<int> window;
    long long sum = 0;
    for (int i = 1; i <= N; ++i) {
        window.insert(a[i]);
        if (window.size() > K) {
            window.erase(window.find(a[i - K]));
        }
        if (i >= K) {
            sum += *window.begin();
        }
    }
    cout << sum;
}