Crossing the Border
Time limit15sMemory limit2048 MB
Pack n weighted items into knapsacks of capacity W so that the sum of each knapsack's maximum tax is minimal, and count the optimal packings.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, Combinatorics
- Solved
- No attempts yet
Statement
Kostya F. is crossing the border of a certain country with taxable items. Each item is described by two integers (its weight) and (the tax paid to carry it across the border).
Kostya must distribute all items among knapsacks. He may use any number of knapsacks. The total weight of the items in each knapsack cannot exceed .
Customs uses a special rule. The officers open each knapsack and set its tax to the maximum among the items inside it. The total tax is the sum of the taxes of all knapsacks.
Find the minimum total tax Kostya can pay, and the number of ways to achieve it. Since the count can be very large, output it modulo . Two distributions are the same if there is a bijection between knapsacks such that matching knapsacks hold exactly the same sets of items.
Input
The first line contains two integers and (; ).
Each of the next lines contains two integers and (; ).
Output
Print two integers: the minimum total tax, and the number of ways to achieve it modulo .
Hint
In the example, items are numbered from 1 to 5. There are 4 ways to reach the total tax of 9:
- [1, 3], [2, 4, 5]: tax 5 + 4 = 9
- [1, 4], [2, 3, 5]: tax 5 + 4 = 9
- [1, 5], [2, 3, 4]: tax 5 + 4 = 9
- [1, 2], [3, 4], [5]: tax 5 + 3 + 1 = 9