201165: [AtCoder]ARC116 F - Deque Game

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $800$ points

Problem Statement

We have $K$ sequences. The $i$-th sequence $A_i$ has the length of $N_i$.

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

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

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

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

Constraints

  • All values in input are integers.
  • $ 1 \leq K \leq 2 \times 10^5$
  • $1 \leq N_i$
  • $\sum_i N_i \leq 2 \times 10^5$
  • $1 \leq A_{i, j} \leq 10^9$

Input

Input is given from Standard Input in the following format:

$K$
$N_1$ $A_{1, 1}$ $A_{1, 2}$ $\cdots$ $A_{1, N_1}$
$N_2$ $A_{2, 1}$ $A_{2, 2}$ $\cdots$ $A_{2, N_2}$
$\vdots$
$N_K$ $A_{K, 1}$ $A_{K, 2}$ $\cdots$ $A_{K, N_K}$

Output

Print the answer.


Sample Input 1

2
3 1 2 3
2 1 10

Sample Output 1

12

Here is one possible progression of the game:

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

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


Sample Input 2

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

Sample Output 2

12

Input

题意翻译

有 $K$ 个数列,第 $i$ 个数列的长度为 $N_i$,命名为 $A_i$。第 $i$ 个数列的第 $j$ 个元素被称为 $A_{i,j}$。 Takahashi 和 Aoki 在玩游戏。每一轮中,可以选择一个剩余元素数量 $>1$ 的数列,并删掉最前面或者最后面的元素。 Takahashi 先手。当每个数列都只剩下一个元素时,游戏结束。 定义一局游戏的得分为最后剩下的元素之和。Takahashi 想要最大化得分,而 Aoki 想要最小化得分。 假设两人都绝顶聪明,请输出最后的得分。 $1\le K,N_i,\sum N_i\le 2\times 10^5$,$1\le A_{i,j,}\le 10^9$。

加入题单

算法标签: