F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Did I miss the lethal?

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 256    Accepted Submission(s): 84


Problem Description
How much damage can you deal with two "Soulfire"s in hand? 8? Are you sure?


-skyline- is playing Hearthstone, and his favorite class is Warlock. There's a special mechanism designed for cards of the Warlock class that after playing some cards from hand, one may need to discard some cards from the remaining hand randomly.



-skyline- has a hand of $n$ cards, with each card can deal some damage, but also possibly would discard some other cards from hand after playing. He is planning for a lethal turn. As a professional player of Hearthstone, he doesn't believe in his luck, so he wants to calculate that in the worst case, what is the maximum damage he can deal using this hand(We ignore the mana costs of the cards and assume that every card can be played, if not discarded).

Formally, there are $n$ cards in -skyline-'s hand, and the $i$th card has two properties $d_i$ and $a_i$, denoting the damage it can deal and the number of cards one needs to discard after playing this card, respectively.

-skyline- can choose the order to play cards from his hand, one by one. After he chooses to play the $i$th card from his hand(after playing, this card no longer exists in his hand), he would deal $d_i$ damage to the enemy hero, then $a_i$ cards from his remaining hand are uniformly chosen at random, then discarded(If currently -skyline- has a hand of less than $a_i$ cards, then all cards in his hand would be discarded). After a card is discarded from hand, it can not be played anymore.

-skyline- wants to choose the best strategy, such that in the worst case, he would deal the maximum number of total damage to the enemy hero. Please help -skyline- find out what this total damage is.
 

Input
The first line contains a number $T(1\leq T\leq 20)$, denoting the number of test cases.

The first line of each test case contains one number $n(1\leq n\leq 200)$, denoting the number of cards in -skyline-'s hand.

Then $n$ lines follow. The $i$th line contains two integers $d_i(1\leq d_i\leq 10^7)$ and $a_i(1\leq a_i\leq 4)$, denoting the damage the $i$th card can deal and the number of cards one needs to discard after playing the $i$th card, respectively.

It is guaranteed that $\sum n \leq 2000$ over all test cases.
 

Output
For each test case, output an integer in one line, denoting the maximum number of total damage -skyline- can deal to the enemy hero in the worst case.

 

Sample Input
2 3 3 1 4 2 5 2 6 10 1 10 1 15 2 20 3 20 3 25 4
 

Sample Output
7 40
 

Hint
For the first test case of the sample, the optimal strategy for -skyline- is to play the first card, then play the only remaining card that is not discarded.
In this case, -skyline- can deal a total of 3+4=7 damage to the enemy hero in the worst case.
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-12 08:00:05, Gzip enabled