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-----

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-----

Print one integer — the maximum damage you may deal.


-----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

Output
263



-----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$).

Difficulty: HARD
Tags: Dynamic programming

Sorting

Implementation
Skill types: Dynamic programming

Sorting

Starter code:

Solutions:
from sys import stdin, stdout, exit
n = int(input())
inf = 10 ** 18
dp = [[-inf] * 10 for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	k = int(stdin.readline())
	cards = []
	for j in range(k):
		(c, d) = map(int, stdin.readline().split())
		cards.append((c, d))
	cards.sort(reverse=True)
	cards_by_cost = [[] for i in range(3)]
	for (c, d) in cards:
		cards_by_cost[c - 1].append(d)
	for j in range(3):
		cards_by_cost[j] = cards_by_cost[j][:3]
	for prev_played in range(10):
		val = dp[i][prev_played]
		dp[i + 1][prev_played] = max(dp[i + 1][prev_played], val)
		for num_played in range(len(cards_by_cost[0])):
			pld = num_played + prev_played + 1
			if pld >= 10:
				dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + sum(cards_by_cost[0][:num_played + 1]) + cards_by_cost[0][0])
			else:
				dp[i + 1][pld] = max(dp[i + 1][pld], val + sum(cards_by_cost[0][:num_played + 1]))
		if len(cards_by_cost[1]) > 0 and len(cards_by_cost[0]) > 0:
			pld = 2 + prev_played
			c0 = cards_by_cost[0][0]
			c1 = cards_by_cost[1][0]
			if pld >= 10:
				dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + c0 + c1 + max(c0, c1))
			else:
				dp[i + 1][pld] = max(dp[i + 1][pld], val + c0 + c1)
		if len(cards_by_cost[1]) > 0:
			pld = 1 + prev_played
			if pld >= 10:
				dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + 2 * cards_by_cost[1][0])
			else:
				dp[i + 1][pld] = max(dp[i + 1][pld], val + cards_by_cost[1][0])
		if len(cards_by_cost[2]) > 0:
			pld = 1 + prev_played
			if pld >= 10:
				dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + 2 * cards_by_cost[2][0])
			else:
				dp[i + 1][pld] = max(dp[i + 1][pld], val + cards_by_cost[2][0])
ans = max((dp[n][i] for i in range(10)))
stdout.write(str(ans) + '\n')


import sys
import math
import cProfile
DEBUG = False

def log(s):
	if DEBUG and False:
		print(s)

def calc_dmg(num, arr):
	maximum = 0
	if num - len(arr) < 0:
		maximum = max(arr)
	return sum(arr) + maximum
if DEBUG:
	sys.stdin = open('input.txt')
	pr = cProfile.Profile()
	pr.enable()
n = sys.stdin.readline()
n = int(n)
dmg = [-sys.maxsize for _ in range(10)]
for i in range(n):
	log(dmg)
	cards = [_[:] for _ in [[-sys.maxsize] * 3] * 4]
	k = sys.stdin.readline()
	k = int(k)
	for _ in range(k):
		(c, d) = sys.stdin.readline().split()
		c = int(c)
		d = int(d)
		cards[c].append(d)
	cards[1].sort(reverse=True)
	cards[2].sort(reverse=True)
	cards[3].sort(reverse=True)
	log(cards)
	new_dmg = []
	for j in range(10):
		use1 = max(cards[1][0], cards[2][0], cards[3][0])
		use2 = max(cards[1][0] + cards[1][1], cards[1][0] + cards[2][0])
		use3 = cards[1][0] + cards[1][1] + cards[1][2]
		maximum = dmg[j]
		if use1 > 0:
			maximum = max(maximum, dmg[j - 1] + calc_dmg(j, [use1]))
			if j == 1:
				maximum = max(maximum, use1)
		if use2 > 0:
			maximum = max(maximum, dmg[j - 2] + calc_dmg(j, [cards[1][0], cards[1][1]] if cards[1][0] + cards[1][1] == use2 else [cards[1][0], cards[2][0]]))
			if j == 2:
				maximum = max(maximum, use2)
		if use3 > 0:
			maximum = max(maximum, dmg[j - 3] + calc_dmg(j, [cards[1][0], cards[1][1], cards[1][2]]))
			if j == 3:
				maximum = max(maximum, use3)
		new_dmg.append(maximum)
	dmg = new_dmg
log(dmg)
print(max(dmg))
if DEBUG:
	pr.disable()
	pr.print_stats()