#P16989. [NWERC 2018] Fastest Speedrun

[NWERC 2018] Fastest Speedrun

Problem Description

The classic video game “Prince of Python” comprises nn levels, numbered from 11 to nn. You are going to speedrun this game by finishing all of the levels as fast as possible, and you can beat them in any order that you want.

You enter each level equipped with one of n+1n+1 magical items. In the beginning you only have item 00 in your inventory. Once you beat a level, you get to keep the item numbered the same as that level.

Beating a level can take different amounts of time depending on which item you take into the level with you. Higher-numbered items are more powerful, so if playing by the rules it is always at least as fast to finish the level with a higher-numbered item as with a lower-numbered item.

However, each level also has a shortcut left in by the developers. The shortcut for a level can be accessed by applying a specific item in an unconventional way. By doing so you can finish the level as fast as, or even faster than, if you had used any of the other items.

How long will it take you to beat all of the levels of the game?

Input Format

The input consists of:

  • One line containing an integer nn (1n25001\le n\le 2500), the number of levels.
  • nn lines, describing the levels. The iith line starts with two integers xix_i and sis_i (0xin0\le x_i\le n, 1si1091\le s_i\le 10^9). The remainder has n+1n+1 integers ai,0,,ai,na_{i,0},\ldots,a_{i,n} (109ai,0ai,nsi10^9\ge a_{i,0}\ge\cdots\ge a_{i,n}\ge s_i), where ai,ja_{i,j} is the completion time for level ii when playing by the rules using item jj.

Output Format

Output the minimum time it takes to beat, in any order, all of the levels in the game.

3
1 1 40 30 20 10
3 1 95 95 95 10
2 1 95 50 30 20
91
4
4 4 5 5 5 5 5
4 4 5 5 5 5 5
4 4 5 5 5 5 5
4 4 5 5 5 5 5
17