This page is still under construction.

Parts of this page are still being built. What you see may change.

The Used Bookstore

Interview

Time limit1sMemory limit128 MB

Summary
Choose exactly K of N books to sell, maxing the total where selling t books of one genre adds t(t-1) extra to that genre's group.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Sorting, Prefix sum
Solved
No attempts yet

Problem

There is a used bookstore in the city where Sanggeun lives. Struggling to keep up with the cost of his dates, Sanggeun decides to sell some of the books he owns to the bookstore. Every book has a fixed base price, and by default the bookstore buys it at that price.

The bookstore sorts all books into 10 genres, such as novels, comics, and magazines, numbered from 1 to 10. When the store buys several books of the same genre together, it pays more for them.

If TT books of the same genre are bought together, each of those TT books is purchased for T−1T-1 won more than its base price. For example, if three same-genre books with base prices 100100, 120120, and 150150 won are sold together, they are bought for 102102, 122122, and 152152 won respectively, because all three are purchased at once.

For tomorrow's date, Sanggeun wants to sell exactly KK of the NN books he owns. Given the base price and genre of each book, write a program that finds the maximum total purchase price he can obtain by choosing which KK books to sell.

Input

The first line contains the number of books NN that Sanggeun owns and the number of books KK he wants to sell. (2≤N≤20002 \le N \le 2000, 1≤K<N1 \le K < N)

Each of the next NN lines contains the base price CiC_i and genre GiG_i of a book, separated by a space. (1≤Ci≤1051 \le C_i \le 10^5, 1≤Gi≤101 \le G_i \le 10)

Output

Print, on the first line, the maximum total purchase price obtainable by selling exactly KK books.

Hint

Once you fix how many books tt you sell from a genre, the added amount t(t−1)t(t-1) is the same, so within that genre it is always best to take the tt highest-priced books. You therefore only need to decide how many books to sell from each genre, and combine the 10 genres like a knapsack so that the total number sold is exactly KK.

Examples1

  1. Example 1

    Input
    7 4
    14 1
    13 2
    12 3
    14 2
    8 2
    16 3
    11 2
    
    Expected output
    60