Language: C++

#include <bits/stdc++.h>

using namespace std;

struct P {
  int v, c, i;
};

struct comp
{
    bool operator()(const P &r, const P &l)
    {
        return r.v < l.v;
    }
};


P newP(int value, int cuts, int iV) {
  P p;
  p.v = value;
  p.c = cuts;
  p.i = iV;
  return p;
}

typedef long long ll;
const int mxn = 2e5;
int x,n, k;
set<P,comp> a;

void printP(P p) {
  cout << p.v << " " << p.c << " " << p.i << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  int temp;
  for (int i = 0;i<n;i++) {
    cin >> temp;
    a.insert(newP(temp, 0, temp));
  }

  while (k>0) {
    auto it = a.rbegin();
    int c = (*it).c+1;
    int i = (*it).i;
    int v = i-(i/(c+1))*c;
    //cout << v << "   ";
    a.erase((*it));
    P p = newP(v, c,i);
    a.insert(p);
    //printP(p);
    k--;
  }
  auto it = a.rbegin();
  cout << (*it).v <<endl;

}


Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = P; _Val = P; _KeyOfValue = std::_Identity<P>; _Compare = comp; _Alloc = std::allocator<P>; _Const_Link_type = const std::_Rb_tree_node<P>*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = P; _Val = P; _KeyOfValue = std::_Identity<P>; _Compare = comp; _Alloc = std::allocator<P>; key_type = P]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = P; _Key = P; _Val = P; _KeyOfValue = std::_Identity<P>; _Compare = comp; _Alloc = std::allocator<P>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:523:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = P; _Compare = comp; _Alloc = std::allocator<P>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<P, P, std::_Identity<P>, comp, std::allocator<P> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<P>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<P>, P>::rebind<P>; typename _Alloc::value_type = P; value_type = P]'
  523 |           _M_t._M_insert_unique(std::move(__x));
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:43:13:   required from here
   43 |     a.insert(newP(temp, 0, temp));
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const comp&, const P&, const P&>' evaluates to false