Language: C++

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
using ll = long long;

template<typename T, class F = function<T(const T&, const T&)>>
class SegmentTree {
  public:
    SegmentTree(int n_, T i, F f, F g) : I(i), get(f), upd(g) {
      for(n = 1; n < n_; n *= 2);
      data.assign(n*2 - 1, I);
    }

    void update(int i, T x){
      i += n - 1;
      data[i] = upd(data[i], x);
      while(i){
        i = (i-1) / 2;
        data[i] = get(data[i*2 + 1], data[i*2 + 2]);
      }
    }
    // [a,b)
    T query(int a, int b){
      return query(a, b, 0, 0, n);
    }

  private:
    int n;
    vector<T> data;
    T I;
    F get, upd;

    T query(int a, int b, int i, int l, int r){
      if(b<=l || r<=a) return I;
      if(a<=l && r<=b) return data[i];
      int vl = query(a, b, i*2+1, l, (l+r)/2);
      int vr = query(a, b, i*2+2, (l+r)/2, r);
      return get(vl, vr);
    }
};

int main() {
  cin.tie(0); ios_base::sync_with_stdio(false);
  ll n, m, a, b; cin >> n >> m;

  vector<pair<ll, ll>> ps;
  for(int i = 0; i < n; ++i) {
    cin >> a >> b;
    ps.emplace_back(b, a);
  }
  sort(ps.rbegin(), ps.rend());

  ll ans = 0;
  SegmentTree<int> st(m+1, -1, [](int i, int j){ return max(i, j); }, [](int i, int j){ return j; });
  for(int i = 0; i <= m; ++i) st.update(i, i);
  for(int i = 0; i < n; ++i) {
    b = ps[i].first, a = ps[i].second;
    int res = st.query(0, m - a + 1);
    if(res != -1) {
      st.update(res, -1);
      ans += b;
    }
  }
  cout << ans;
  return 0;
}


Compiler output:
a.cc:9:32: error: 'function' does not name a type; did you mean 'union'?
    9 | template<typename T, class F = function<T(const T&, const T&)>>
      |                                ^~~~~~~~
      |                                union
a.cc:9:40: error: expected '>' before '<' token
    9 | template<typename T, class F = function<T(const T&, const T&)>>
      |                                        ^
a.cc: In function 'int main()':
a.cc:57:18: error: template argument 2 is invalid
   57 |   SegmentTree<int> st(m+1, -1, [](int i, int j){ return max(i, j); }, [](int i, int j){ return j; });
      |                  ^
a.cc:57:100: error: expression list treated as compound expression in initializer [-fpermissive]
   57 |   SegmentTree<int> st(m+1, -1, [](int i, int j){ return max(i, j); }, [](int i, int j){ return j; });
      |                                                                                                    ^
a.cc:57:71: error: invalid user-defined conversion from 'main()::<lambda(int, int)>' to 'int' [-fpermissive]
   57 |   SegmentTree<int> st(m+1, -1, [](int i, int j){ return max(i, j); }, [](int i, int j){ return j; });
      |                                                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:57:71: note: candidate is: 'constexpr main()::<lambda(int, int)>::operator int (*)(int, int)() const' (near match)
   57 |   SegmentTree<int> st(m+1, -1, [](int i, int j){ return max(i, j); }, [](int i, int j){ return j; });
      |                                                                       ^
a.cc:57:71: note:   no known conversion from 'int (*)(int, int)' to 'int'
a.cc:58:34: error: request for member 'update' in 'st', which is of non-class type 'int'
   58 |   for(int i = 0; i <= m; ++i) st.update(i, i);
      |                                  ^~~~~~
a.cc:61:18: error: request for member 'query' in 'st', which is of non-class type 'int'
   61 |     int res = st.query(0, m - a + 1);
      |                  ^~~~~
a.cc:63:10: error: request for member 'update' in 'st', which is of non-class type 'int'
   63 |       st.update(res, -1);
      |          ^~~~~~