#P2334. [SCOI2006] 城堡

[SCOI2006] 城堡

Description

To rescue his beloved princess Julie, Billy has arrived at the demon’s castle. After fighting for three days and nights, the Demon King’s hall is within reach.

This is a narrow corridor. Billy is at position 00, and the Demon King’s hall is at position n+1n + 1. At each unit of time, Billy may move one unit left, one unit right, or stay in place. Above each cell, stones periodically fall; the period of cell ii is cic_i.

For the stones above cell ii, we use cic_i integers h1,h2,,hcih_1, h_2, \cdots, h_{c_i} to describe them: at time t=kci+xt = k c_i + x (1xci1 \le x \le c_i) for any integer k0k \ge 0, being on that cell will cause a loss of hxh_x HP. Here hx=0h_x = 0 means no stone falls at that moment.

Compute the minimum total HP that Billy must lose to reach the Demon King’s hall. Assume Billy will not die. Note that from position 11 you may move back to position 00, and at position 00 no HP is lost.

Input Format

The first line contains an integer nn, the length of the corridor.

The next nn lines describe cells 1,2,,n1, 2, \cdots, n. In each line, the first integer is cic_i, the falling period, followed by cic_i integers h1,h2,,hcih_1, h_2, \cdots, h_{c_i}.

Output Format

Output a single integer HP, the minimum total HP lost.

4
2 1 0
2 0 1
1 2
7 0 1 1 1 1 1 1

2

Hint

  • For 50%50\% of the testdata, n20n \le 20.
  • For 100%100\% of the testdata: 0n10000 \le n \le 1000, 1ci101 \le c_i \le 10, 0hx1000 \le h_x \le 100.
  • 2023-04-12: Added one hack testdata (not scored).

Translated by ChatGPT 5