CSES - Bugaavia koodeja

Bugaavia koodeja DT-valmennustehtävään "Tikku"

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int x, n;
  cin >> x >> n;
  set<pair<int, int>> osat;
  multiset<int> maksimi;
  osat.insert({0, x});
  maksimi.insert(x);
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    auto it = osat.upper_bound({k, 0});
    it--;
    auto [p, s] = *it;
    osat.erase(it);
    maksimi.erase(s);
    int l = k - p;
    int r = s - l;
    osat.insert({p, l});
    osat.insert({k, r});
    maksimi.insert(l);
    maksimi.insert(r);
    cout << *maksimi.rbegin() << " ";
  }
  cout << endl;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int x, n;
  cin >> x >> n;
  set<pair<int, int>> osat;
  multiset<int> maksimi;
  osat.insert({0, x});
  maksimi.insert(x);
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    auto it = osat.lower_bound({k+1, 2e9});
    it--;
    auto [p, s] = *it;
    osat.erase(it);
    maksimi.erase(maksimi.find(s));
    int l = k - p;
    int r = s - l;
    osat.insert({p, l});
    osat.insert({k, r});
    maksimi.insert(l);
    maksimi.insert(r);
    cout << *maksimi.rbegin() << " ";
  }
  cout << endl;
}