The Used Bookstore
InterviewTime limit1sMemory limit128 MB
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 books of the same genre are bought together, each of those books is purchased for won more than its base price. For example, if three same-genre books with base prices , , and won are sold together, they are bought for , , and won respectively, because all three are purchased at once.
For tomorrow's date, Sanggeun wants to sell exactly of the 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 books to sell.
Input
The first line contains the number of books that Sanggeun owns and the number of books he wants to sell. (, )
Each of the next lines contains the base price and genre of a book, separated by a space. (, )
Output
Print, on the first line, the maximum total purchase price obtainable by selling exactly books.
Hint
Once you fix how many books you sell from a genre, the added amount is the same, so within that genre it is always best to take the 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 .