Language: C++

//Solution for aoj 0561:Books
#include <iostream>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
int n, k, a[11], fir[11], i, j, ma, num, ans, save[2001][2], sum;
int books[2001][3], books2[2001][11], books3[2001], books4[11];
void solve(int i){
	for (i; i <= k; i++){
		ma = 0;
		for (int j = 1; j <= 10; j++){
			if (ma<books2[books4[j]][j] + books4[j] * 2 - 2){
				ma = books2[books4[j]][j] + books4[j] * 2 - 2;
				num = j;
			}
			else if (ma == books2[books4[j]][j] + books4[j] * 2 - 2){
				save[i][0] = books4[j];
				save[i][1] = sum;
				books4[j]++;
				sum += ma;
				solve(i + 1);
				books4[j] = save[i][0];
				sum = save[i][1];
			}
		}
		books4[num]++;
		sum += ma;
	}
	ans = max(ans, sum);
}
int main(){
	cin >> n >> k;
	for (i = 1; i <= n; i++){
		cin >> books[i][0] >> books[i][1];
		if (fir[books[i][1]] == 0)
			fir[books[i][1]] = i;
		books[a[books[i][1]]][2] = i;
		a[books[i][1]] = i;
	}
	for (i = 1; i <= 10; i++){
		if (a[i])
			books[a[i]][2] = -1;
		books4[i] = 1;
	}
	for (i = 1; i <= 10; i++){
		int roop = 1;
		j = fir[i];
		if (j == 0)
			break;
		while (j != -1){
			books3[roop] = books[j][0];
			roop++;
			j = books[j][2];
		}
		sort(books3+1, books3 + roop + 1, greater<int>());
		for (int l = 1; l < roop; l++){
			books2[l][i] = books3[l];
		}
	}//Solution for aoj 0561:Books
#include <iostream>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
int n, k, a[11], fir[11], i, j, ma, num, ans, save[2001][2], sum;
int books[2001][3], books2[2001][11], books3[2001], books4[11];
void solve(int i){
	for (i; i <= k; i++){
		ma = 0;
		for (int j = 1; j <= 10; j++){
			if (ma<books2[books4[j]][j] + books4[j] * 2 - 2){
				ma = books2[books4[j]][j] + books4[j] * 2 - 2;
				num = j;
			}
			else if (ma == books2[books4[j]][j] + books4[j] * 2 - 2){
				save[i][0] = books4[j];
				save[i][1] = sum;
				books4[j]++;
				sum += ma;
				solve(i + 1);
				books4[j] = save[i][0];
				sum = save[i][1];
			}
		}
		books4[num]++;
		sum += ma;
	}
	ans = max(ans, sum);
}
int main(){
	cin >> n >> k;
	for (i = 1; i <= n; i++){
		cin >> books[i][0] >> books[i][1];
		if (fir[books[i][1]] == 0)
			fir[books[i][1]] = i;
		books[a[books[i][1]]][2] = i;
		a[books[i][1]] = i;
	}
	for (i = 1; i <= 10; i++){
		if (a[i])
			books[a[i]][2] = -1;
		books4[i] = 1;
	}
	for (i = 1; i <= 10; i++){
		int roop = 1;
		j = fir[i];
		if (j == 0)
			break;
		while (j != -1){
			books3[roop] = books[j][0];
			roop++;
			j = books[j][2];
		}
		sort(books3+1, books3 + roop + 1, greater<int>());
		for (int l = 1; l < roop; l++){
			books2[l][i] = books3[l];
		}
	}
	solve(1);
	cout << ans << endl;
	return 0;
}
	solve(1);
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:68:18: error: a function-definition is not allowed here before '{' token
   68 | void solve(int i){
      |                  ^
a.cc:91:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   91 | int main(){
      |         ^~
a.cc:91:9: note: remove parentheses to default-initialize a variable
   91 | int main(){
      |         ^~
      |         --
a.cc:91:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:91:11: error: a function-definition is not allowed here before '{' token
   91 | int main(){
      |           ^