Cod sursa(job #3334975)

Utilizator rrfeierFeier Raul rrfeier Data 20 ianuarie 2026 21:17:43
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>
#include <fstream>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ifstream cin{"deque.in"};
  ofstream cout{"deque.out"};

  int N, K;
  cin >> N >> K;

  deque<pair<int, int>> q;
  long long sum = 0;

  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;

    while (!q.empty() && x <= q.back().first) {
      q.pop_back();
    }

    while (!q.empty() && q.front().second <= i - K) {
      q.pop_front();
    }

    q.push_back({x, i});

    if (K <= i) {
      sum += q.front().first;
    }
  }

  cout << sum << '\n';

  return 0;
}

/*
-7
9
2
4
-1
5
6
7
1

-7 + 2 -1 -1 -1 5 1

3^2 + (1 + 3) ^ 2 + (5 + 1 + 3) ^ 2 + (5 + 1) ^ 2 + 1^2 + 5^2 =
9 + 16 + 81 + 36 + 1 + 25 = 168
3 + 1 + 3 + 5 + 1 + 3 + 5 + 1 + 1 + 5
*/