Title: 古本屋 (Books)

In your town, there is a long-established second-hand bookstore called JOI Bookstore, which you frequently visit. Each book has a set standard price, and if you take it to JOI Bookstore, they will buy it at that price.

At JOI Bookstore, books are classified into 10 genres such as novels, manga, and magazines, each assigned a number from 1 to 10. They offer a service where if you sell a group of books of the same genre, they will buy them at a higher price. Specifically, if you sell T books of the same genre, the purchase price per book will be T - 1 yen higher than the standard price. For example, if you sell books priced at 100 yen, 120 yen, and 150 yen of the same genre to JOI Bookstore, the purchase prices will be 102 yen, 122 yen, and 152 yen, respectively.

Now, due to personal circumstances, you have to move suddenly. You have N books, but it's difficult to take all of them to your new residence, so you decide to sell K of the N books to JOI Bookstore.

Task:
Create a program to find the maximum total purchase price when given the standard price and genre number for each of the N books.

Constraints:
2 ≤ N ≤ 2000: Number of books you have
1 ≤ K < N: Number of books to sell to JOI Bookstore
1 ≤ Ci ≤ 100000 = 10^5: Standard price of the i-th book
1 ≤ Gi ≤ 10: Genre number of the i-th book

Input:
Read the following input from standard input:
- The first line contains two integers N and K separated by a space, representing the number of books you have and the number of books to sell to JOI Bookstore, respectively.
- The next N lines contain information about the books you have. The i+1-th line (1 ≤ i ≤ N) contains two integers Ci and Gi separated by a space, representing the standard price of the i-th book and the genre number of the i-th book.

Output:
Output a single integer representing the maximum total purchase price to standard output.

Scoring Criteria:
- 20% of the test data will have N ≤ 20.
- 20% of the test data will have all book genres as 1 or 2.
- 10% of the test data will satisfy both of the above conditions.
- 30% of the test data will satisfy at least one of the above conditions.

Example Input:
7 4
14 1
13 2
12 3
14 2
8 2
16 3
11 2

Example Output:
60

In this example, when you sell the 2nd, 4th, 6th, and 7th books, the purchase price for books of genre 2 will be 1 yen higher per book. Therefore, the purchase prices for these books will be as follows:
Number: 2, 4, 6, 7
Standard Price: 13, 14, 16, 11
Genre: 2, 2, 3, 2
Purchase Price: 15, 16, 16, 13

Thus, the total purchase price is 15 + 16 + 16 + 13 = 60 yen, which is the maximum total purchase price.

# Buggy version:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    book_map[i].sort(reverse=True)
    acc = 0
    for j in range(len(book_map[i])):
      book_map[i][j] += j * 2
      acc += book_map[i][j]
      acc_map[i].append(acc)
  print(acc_map)  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      for z in range(min(y + 1, len(acc_map[x - 1]))):
        dp[x][y] = max(dp[x][y], dp[x - 1][y - z] + acc_map[x - 1][z])
  
#  print(dp[group_num][k])
solve()



# Fixed version:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c, g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    bmi = book_map[i]
    bmi.sort(reverse=True)
    ami = acc_map[i]
    acc = 0
    for j in range(len(bmi)):
      acc += (bmi[j] + j * 2)
      ami.append(acc)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1, k + 1):
    for x in range(1, group_num + 1):
      accs = acc_map[x - 1]
      dp_pre = dp[x - 1]
      dp[x][y] = max([dp_pre[y - z] + accs[z] for z in range(min(y + 1, len(accs)))])
  
  print(dp[group_num][k])
solve()