Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
	int n,k;
  	cin >>n >>k;
  	vector<vector<int>> ds(n);
  	rep(i,n){
    	int t,d;
      	cin >>t >>d;t--;
      	ds[t].push_back(d);
    }
  	vector<int> y0,y1;
  	rep(i,n){
    	if(ds[i].size()==0) continue;
      	sort(ds[i].begin(),ds[i].end());
      	y1.push_back(ds[i].back());
      	ds[i].pop_back();
      	y0.insert(y0.end(), ds[i].begin(), ds[i].end());
    }//y1には各種類の最大、y0はそれ以外を昇順で
  	sort(y0.rbegin, y0.rend());
    sort(y1.rbegin, y1.rend());//それぞれ降順
  	long long ans=0;
  	int Y=max(0,k-int(y0.size()))
    long long X=0;
  	rep(i,Y)   X +=y1[i]; //種類がk個に満たない時
  	rep(i,k-Y) X +=y0[i];
  	while(1){
    	ans=max(ans, X+(long long)Y*Y);
      	if(Y >= k || Y >= y1.size()) break;
      	X += y1[Y];
      	X -= y0[k-Y-1];
      	++Y;
    }
    cout <<ans <<endl;
  return 0;

}


Compiler output:
a.cc: In function 'int main()':
a.cc:24:13: error: no matching function for call to 'sort(<unresolved overloaded function type>, std::vector<int>::reverse_iterator)'
   24 |         sort(y0.rbegin, y0.rend());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:4762:5: note: candidate: 'void std::sort(_RAIter, _RAIter) [with _RAIter = reverse_iterator<__gnu_cxx::__normal_iterator<int*, vector<int> > >]'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4762:32: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |          ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note: candidate: 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)'
 4793 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare)'
  292 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator)'
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note:   candidate expects 3 arguments, 2 provided
a.cc:25:9: error: no matching function for call to 'sort(<unresolved overloaded function type>, std::vector<int>::reverse_iterator)'
   25 |     sort(y1.rbegin, y1.rend());//それぞれ降順
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4762:5: note: candidate: 'void std::sort(_RAIter, _RAIter) [with _RAIter = reverse_iterator<__gnu_cxx::__normal_iterator<int*, vector<int> > >]'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4762:32: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |          ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note: candidate: 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)'
 4793 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare)'
  292 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator)'
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note:   candidate expects 3 arguments, 2 provided
a.cc:28:5: error: expected ',' or ';' before 'long'
   28 |     long long X=0;
      |     ^~~~
a.cc:29:20: error: 'X' was not declared in this scope
   29 |         rep(i,Y)   X +=y1[i]; //種類がk個に満たない時
      |                    ^
a.cc:30:20: error: 'X' was not declared in this scope
   30 |         rep(i,k-Y) X +=y0[i];
      |                    ^
a.cc:32:22: error: 'X' was not declared in this scope
   32 |         ans=max(ans, X+(long long)Y*Y);
      |                      ^