Language: C++

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9+7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL)-9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

LL N,K;
vector<int> sushi[100000];
vector<int> top,other;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << setprecision(9);

	cin >> N >> K;
	for(int i=0; i<N; i++){
		int t,d;
		cin >> t >> d;
		t--;
		sushi[t].push_back(d);
	}
	for(int i=0; i<N; i++) sort(sushi[i].begin(), sushi[i].end(), greater<int>());
	for(int i=0; i<N; i++){
		if(sushi[i].size() > 0){
			top.push_back(sushi[i][0]);
			for(int j=1; j<sushi[i].size(); j++) other.push_back(sushi[i][j]);
		}
	}
	sort(top.begin(), top.end(), greater<int>());
	sort(other.begin(), other.end(), greater<int>());

	LL ans = 0;
	if(top.size() >= K){
		LL tmp = 0;
		for(int i=0; i<K; i++) tmp += top[i];
		ans = max(ans, tmp+K*K);
		for(int i=1; i<K; i++){
			if(i-1 >= other.size()) break;
			tmp -= top[K-i];
			tmp += other[i-1];
			ans = max(ans, tmp+(K-i)*(K-i));
		}
	}
	else{
		LL tmp = 0;
		for(int i=0; i<top.size(); i++) tmp += top[i];
		for(int i=0; i<K-top.size(); i++) tmp += other[i];
		ans = max(ans, tmp+(LL)top.size()*top.size());
		for(int i=1; i<top.size(); i++){
			if(K-top.size()+i-1 >= other.size()) break;
			tmp -= top[top.size()-i];
			tmp += other[K-top.size()+i-1];
			ans = max(ans, tmp+(LL)(top.size()-i)*(top.size()-i));
		}
	}
	cout << ans << endl;

	return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:75:26: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   75 |                 ans = max(ans, tmp+(LL)top.size()*top.size());
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:75:26: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   75 |                 ans = max(ans, tmp+(LL)top.size()*top.size());
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:17:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:75:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   75 |                 ans = max(ans, tmp+(LL)top.size()*top.size());
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:80:34: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   80 |                         ans = max(ans, tmp+(LL)(top.size()-i)*(top.size()-i));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:80:34: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   80 |                         ans = max(ans, tmp+(LL)(top.size()-i)*(top.size()-i));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:80:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   80 |                         ans = max(ans, tmp+(LL)(top.size()-i)*(top.size()-i));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~