Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<functional>

using namespace std;
const int MAX = 1e5;
const long long INF = 1e17;
typedef pair<long long,long long> P;
bool flag[MAX+1] = { 0 };
int k = 0;
long long kmax[MAX+1] = { 0 };
long long ans = 0;
long long tt[MAX+1] = { 0 };
//priority_queue<P, vector<P>, greater<P>>que;
//priority_queue<P, vector<P>>que2;
priority_queue<long long, greater<long long>>que3;
priority_queue<long long>que4;
vector<P> p;

int main() {
	for (int i = 0; i < MAX+1; i++) {
		kmax[i] = -INF;
	}
	int K, N;
	cin >> N >> K;
	
	long long temp, tempp;
	for (int i = 0; i < N; i++) {
		cin >> temp >> tempp;
		p.push_back(P(tempp, temp));
	}
	sort(p.rbegin(), p.rend());
	temp = 0;
	for (int i = 0; i < K; i++) {
		temp += p[i].first;
		if (flag[p[i].second] == false) {
			flag[p[i].second] = true;
			k++;
		}
		else {
			//que.push(P(p[i].first, p[i].second));
			que3.push(p[i].first);
		}
	}
	kmax[k] = temp;

	for (int i = K; i < N; i++) {
		if (flag[p[i].second] == false) {
			tt[p[i].second] = p[i].first;
			flag[p[i].second] = true;
		}
	}
	for (int i = 0; i <= N; i++) {
		if (tt[i] != 0) {
			//que2.push(P(tt[i], i));
			que4.push(tt[i]);
		}
	}
	if (que3.empty() == true || que4.empty() == true) {}
	else {
	for (int i = k + 1; i <= N; i++) {
		kmax[i] = kmax[i - 1];
		//P temporary = que.top(); que.pop();
		long long temporary = que3.top(); que3.pop();
		//kmax[i] -= temporary.first;[
		kmax[i] -= temporary;
		//P temporary2 = que2.top(); que2.pop();
		long long temporary2 = que4.top(); que4.pop();
		//kmax[i] += temporary2.first;
		kmax[i] += temporary2;
		if (que3.empty() == true || que4.empty() == true)break;
	}
}
	for (int i = k; i <= N; i++) {
		if (kmax[i] == -INF)continue;
		ans = max(kmax[i] + i * i, ans);
	}

	cout << ans << endl;	
	
	return 0;
}
	/*
	int N;
	cin >> N;
	int h[100];
	for (int i = 0; i < N; i++) {
		cin >> h[i];
	}
	int ans = 0;
	bool flag = 0;
	for (int i = 0; i < 100; i++) {
		flag = false;
		for (int j = 0; j < N; j++) {
			if (h[j] > 0) {
				h[j]--;
				flag = true;
			}
			else if (flag == true) {
				flag = false;
				ans++;
			}
		}
		if (flag == true) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
	*/

Compiler output:
a.cc:18:44: error: no type named 'value_type' in 'struct std::greater<long long int>'
   18 | priority_queue<long long, greater<long long>>que3;
      |                                            ^~
a.cc:18:44: error: template argument 3 is invalid
a.cc: In function 'int main()':
a.cc:44:30: error: request for member 'push' in 'que3', which is of non-class type 'int'
   44 |                         que3.push(p[i].first);
      |                              ^~~~
a.cc:61:18: error: request for member 'empty' in 'que3', which is of non-class type 'int'
   61 |         if (que3.empty() == true || que4.empty() == true) {}
      |                  ^~~~~
a.cc:66:44: error: request for member 'top' in 'que3', which is of non-class type 'int'
   66 |                 long long temporary = que3.top(); que3.pop();
      |                                            ^~~
a.cc:66:56: error: request for member 'pop' in 'que3', which is of non-class type 'int'
   66 |                 long long temporary = que3.top(); que3.pop();
      |                                                        ^~~
a.cc:73:26: error: request for member 'empty' in 'que3', which is of non-class type 'int'
   73 |                 if (que3.empty() == true || que4.empty() == true)break;
      |                          ^~~~~