#ARC116F. [ARC116F] Deque Game

[ARC116F] Deque Game

Score : 800800 points

Problem Statement

We have KK sequences. The ii-th sequence AiA_i has the length of NiN_i.

Takahashi and Aoki will play a game using these. They will alternately do the following operation until the length of every sequence becomes 11:

  • Choose a sequence of length at least 22, and delete its first or last element.

Takahashi goes first. Takahashi wants to maximize the sum of the KK elements remaining in the end, while Aoki wants to minimize it.

Find the sum of the KK elements remaining in the end when both players play optimally.

Constraints

  • All values in input are integers.
  • 1K2×1051 \leq K \leq 2 \times 10^5
  • 1Ni1 \leq N_i
  • iNi2×105\sum_i N_i \leq 2 \times 10^5
  • 1Ai,j1091 \leq A_{i, j} \leq 10^9

Input

Input is given from Standard Input in the following format:

KK

N1N_1 A1,1A_{1, 1} A1,2A_{1, 2} \cdots A1,N1A_{1, N_1}

N2N_2 A2,1A_{2, 1} A2,2A_{2, 2} \cdots A2,N2A_{2, N_2}

\vdots

NKN_K AK,1A_{K, 1} AK,2A_{K, 2} \cdots AK,NKA_{K, N_K}

Output

Print the answer.

2
3 1 2 3
2 1 10
12

Here is one possible progression of the game:

  • Takahashi deletes the first element of A2A_2. Now we have A1=(1,2,3),A2=(10)A_1 = \left(1, 2, 3\right), A_2 = \left(10\right).
  • Aoki deletes the last element of A1A_1. Now we have A1=(1,2),A2=(10)A_1 = \left(1, 2\right), A_2 = \left(10\right).
  • Takahashi deletes the first element of A1A_1. Now we have A1=(2),A2=(10)A_1 = \left(2\right), A_2 = \left(10\right). The length of every sequence has become 11, so the game ends.

In this case, the sum of the KK elements remaining in the end is 1212. Note that the players may have made suboptimal moves here.

8
1 2
2 1 2
3 1 2 1
4 1 1 1 2
5 1 1 2 2 1
6 2 2 2 2 1 1
7 1 2 1 1 2 2 2
8 2 2 2 1 1 1 1 2
12