codeforces#P1176F. Destroy it!
Destroy it!
Description
You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.
The boss battle consists of turns. During each turn, you will get several cards. Each card has two parameters: its cost and damage . You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed . After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once.
Your character has also found an artifact that boosts the damage of some of your actions: every -th card you play deals double damage.
What is the maximum possible damage you can deal during turns?
The first line contains one integer () — the number of turns.
Then blocks of input follow, the -th block representing the cards you get during the -th turn.
Each block begins with a line containing one integer () — the number of cards you get during -th turn. Then lines follow, each containing two integers and (, ) — the parameters of the corresponding card.
It is guaranteed that .
Print one integer — the maximum damage you may deal.
Input
The first line contains one integer () — the number of turns.
Then blocks of input follow, the -th block representing the cards you get during the -th turn.
Each block begins with a line containing one integer () — the number of cards you get during -th turn. Then lines follow, each containing two integers and (, ) — the parameters of the corresponding card.
It is guaranteed that .
Output
Print one integer — the maximum damage you may deal.
Samples
Note
In the example test the best course of action is as follows:
During the first turn, play all three cards in any order and deal damage.
During the second turn, play both cards and deal damage.
During the third turn, play the first and the third card and deal damage.
During the fourth turn, play the first and the third card and deal damage.
During the fifth turn, play the only card, which will deal double damage ().