Destroy it!
Contest: Codeforces Round 565 (Div. 3)
Difficulty rating: 2100
Tags: dp, implementation, sortings

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 $$$n$$$ turns. During each turn, you will get several cards. Each card has two parameters: its cost $$$c_i$$$ and damage $$$d_i$$$. 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 $$$3$$$. 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 $$$10$$$-th card you play deals double damage.

What is the maximum possible damage you can deal during $$$n$$$ turns?

Input format:
The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of turns.

Then $$$n$$$ blocks of input follow, the $$$i$$$-th block representing the cards you get during the $$$i$$$-th turn.

Each block begins with a line containing one integer $$$k_i$$$ ($$$1 \le k_i \le 2 \cdot 10^5$$$) — the number of cards you get during $$$i$$$-th turn. Then $$$k_i$$$ lines follow, each containing two integers $$$c_j$$$ and $$$d_j$$$ ($$$1 \le c_j \le 3$$$, $$$1 \le d_j \le 10^9$$$) — the parameters of the corresponding card.

It is guaranteed that $$$\sum \limits_{i = 1}^{n} k_i \le 2 \cdot 10^5$$$.

Output format:
Print one integer — the maximum damage you may deal.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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 $$$18$$$ damage.

During the second turn, play both cards and deal $$$7$$$ damage.

During the third turn, play the first and the third card and deal $$$13$$$ damage.

During the fourth turn, play the first and the third card and deal $$$25$$$ damage.

During the fifth turn, play the only card, which will deal double damage ($$$200$$$).

Example input:
5
3
1 6
1 7
1 5
2
1 4
1 3
3
1 10
3 5
2 3
3
1 15
2 4
1 10
1
1 100

Example output:
263

Editorial:
The first (and crucial) observation is that we don't need all the cards that we get during each turn. In fact, since the total cost is limited to $$$3$$$, we may leave three best cards of cost $$$1$$$, one best card of cost $$$2$$$ and one best card of cost $$$3$$$, and all other cards may be discarded. So, the problem is reduced: we get only $$$5$$$ cards each turn.
The problem may be solved with dynamic programming: $$$dp_{x, y}$$$ is the maximum damage we may deal if we played $$$x$$$ turns and the last card we played had remainder $$$y$$$ modulo $$$10$$$. Processing each turn may be done with auxiliary dp: $$$z_{c, f}$$$ — the maximum damage we can deal during the turn if we play $$$c$$$ cards, and $$$f$$$ denotes whether some card (there will be only one such card, obviously) deals double damage. To calculate this auxiliary dp, we may do almost anything since we are limited to $$$5$$$ cards during each turn. It is possible to calculate it in a fast way using some casework, but it is easier, for example, to try all possible permutations of $$$5$$$ cards and play some prefix of a fixed permutation.
By combining these two techniques, we get a solution.