Language: C++

#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main(){
	int N, K;
	cin >> N >> K;
	vector<int> t(N);
	vector<long long> d(N);
	for (int i = 0; i < N; i++){
		cin >> t[i] >> d[i];
		t[i]--;
	}
	vector<vector<int>> id(N);
	for (int i = 0; i < N; i++){
		id[t[i]].push_back(i);
	}
	set<pair<long long, int>> st1;
	set<pair<long long, int>> st2;
	for (int i = 0; i < N; i++){
		st1.insert(make_pair(d[i], i));
	}
	int x = 0;
	long long ans = 0;
	for (int i = 0; i < K; i++){
		bool use_st1 = false;
		if (st2.empty()){
			use_st1 = true;
		} else if ((*st2.rbegin()).first < (*st1.rbegin()).first + x * 2 + 1){
			use_st1 = true;
		}
		if (use_st1){
			ans += (*st1.rbegin()).first + x * 2 + 1;
			x++;
			int tmp = (*st1.rbegin()).second;
			st1.erase(st1.rbegin());
			for (int j : id[tmp]){
				pair<long long, int> P = make_pair(d[j], j);
				if (st1.count(P)){
					st1.erase(P);
					st2.insert(P);
				}
			}
		} else {
			ans += (*st2.rbegin()).first;
			st2.erase(st2.rbegin());
		}
	}
	cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:36:34: error: no matching function for call to 'std::set<std::pair<long long int, int> >::erase(std::set<std::pair<long long int, int> >::reverse_iterator)'
   36 |                         st1.erase(st1.rbegin());
      |                         ~~~~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/set:63,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_set.h:656:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator]'
  656 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:656:28: note:   no known conversion for argument 1 from 'std::set<std::pair<long long int, int> >::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<std::pair<long long int, int> > >'} to 'std::set<std::pair<long long int, int> >::const_iterator' {aka 'std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator'}
  656 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:686:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; size_type = long unsigned int; key_type = std::pair<long long int, int>]'
  686 |       erase(const key_type& __x)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:686:29: note:   no known conversion for argument 1 from 'std::set<std::pair<long long int, int> >::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<std::pair<long long int, int> > >'} to 'const std::set<std::pair<long long int, int> >::key_type&' {aka 'const std::pair<long long int, int>&'}
  686 |       erase(const key_type& __x)
      |             ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:708:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator, const_iterator) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator]'
  708 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:708:7: note:   candidate expects 2 arguments, 1 provided
a.cc:46:34: error: no matching function for call to 'std::set<std::pair<long long int, int> >::erase(std::set<std::pair<long long int, int> >::reverse_iterator)'
   46 |                         st2.erase(st2.rbegin());
      |                         ~~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:656:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator]'
  656 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:656:28: note:   no known conversion for argument 1 from 'std::set<std::pair<long long int, int> >::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<std::pair<long long int, int> > >'} to 'std::set<std::pair<long long int, int> >::const_iterator' {aka 'std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator'}
  656 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:686:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; size_type = long unsigned int; key_type = std::pair<long long int, int>]'
  686 |       erase(const key_type& __x)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:686:29: note:   no known conversion for argument 1 from 'std::set<std::pair<long long int, int> >::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<std::pair<long long int, int> > >'} to 'const std::set<std::pair<long long int, int> >::key_type&' {aka 'const std::pair<long long int, int>&'}
  686 |       erase(const key_type& __x)
      |             ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:708:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator, const_iterator) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator]'
  708 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:708:7: note:   candidate expects 2 arguments, 1 provided