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.end());
      	y1.push_back(ds[i].end());
      	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:19:13: error: no matching function for call to 'sort(std::vector<int>::iterator, std::vector<std::vector<int> >::iterator)'
   19 |         sort(ds[i].begin(),ds.end());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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: 'template<class _RAIter> void std::sort(_RAIter, _RAIter)'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4762:5: note:   template argument deduction/substitution failed:
a.cc:19:13: note:   deduced conflicting types for parameter '_RAIter' ('__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and '__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >')
   19 |         sort(ds[i].begin(),ds.end());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/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:20:21: error: no matching function for call to 'std::vector<int>::push_back(std::vector<int>::iterator)'
   20 |         y1.push_back(ds[i].end());
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'std::vector<int>::iterator' to 'const std::vector<int>::value_type&' {aka 'const int&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'std::vector<int>::iterator' to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
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());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
/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: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);
      |                      ^