Language: C++

#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

llint n, k;
vector<llint> vec[100005];
vector<pair<llint, llint> > vec2;
priority_queue<llint, vector<llint>, greater<llint> > Q;

int main(void)
{
	cin >> n >> k;
	llint t, d;
	for(int i = 1; i <= n; i++){
		cin >> t >> d;
		vec[t].push_back(d);
	}
	for(int i = 1; i <= n; i++) sort(vec[i].rbegin(), vec[i].rend());
	
	for(int i = 1; i <= n; i++){
		if(vec[i].size()) vec2.push_back(make_pair(vec[i][0], i));
	}
	sort(vec2.rbegin(), vec2.rend());
	
	llint ans = 0, tmp = 0, sum = 0;
	for(llint i = 0; i < min(vec2.size(), k); i++){
		int p = vec2[i].second;
		tmp += vec[p][0];
		for(int j = 1; j < vec[p].size(); j++){
			if((int)Q.size()+i+1 < k || Q.size() == 0 || Q.top() < vec[p][j]){
				Q.push(vec[p][j]);
				sum += vec[p][j];
			}
		}
		while((int)Q.size()+i+1 > k){
			sum -= Q.top();
			Q.pop();
		}
		if((int)Q.size()+i+1 < k) continue;
		ans = max(ans, tmp + sum + (i+1)*(i+1));
	}
	cout << ans << endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:32:33: error: no matching function for call to 'min(std::vector<std::pair<long long int, long long int> >::size_type, long long int&)'
   32 |         for(llint i = 0; i < min(vec2.size(), k); i++){
      |                              ~~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:32:33: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long int')
   32 |         for(llint i = 0; i < min(vec2.size(), k); i++){
      |                              ~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:32:33: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   32 |         for(llint i = 0; i < min(vec2.size(), k); i++){
      |                              ~~~^~~~~~~~~~~~~~~~