Under construction

Still being built. Things may change

Fastest Speedrun

Time limit5sMemory limit512 MB

Summary
Given n levels with per-item clear times and a shortcut item for each, find the minimum total time to beat all levels, where beating level i grants item i.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree
Solved
No attempts yet

Problem

The classic video game "Prince of Python" has nn levels, numbered from 1 to nn. You want to speedrun the game by finishing all levels as fast as possible. You may beat the levels in any order.

You enter each level with one of n+1n + 1 magical items. At the start, you only have item 0 in your inventory. When you beat a level, you keep the item with the same number as that level. For example, after finishing level 5, you obtain the Gauntlet of 5 Fingers, which you can equip from then on instead of the Sword of 0 Damage you start with.

The time to beat a level depends on the item you bring into it. Higher-numbered items are stronger, so when playing by the rules, using a higher-numbered item is never slower than using a lower-numbered one.

Each level also has a shortcut left in by the developers. The shortcut for a level is triggered by using one specific item in an unconventional way. Using the shortcut finishes the level in a time that is no greater than the time for any other item.

What is the minimum total time needed to beat all of the levels?

Input

The input consists of:

  • One line with an integer nn (1≤n≤25001 \le n \le 2500), the number of levels.
  • nn lines describing the levels.
    The ii-th line starts with two integers xix_i and sis_i (0≤xi≤n0 \le x_i \le n, 1≤si≤1091 \le s_i \le 10^9), the shortcut item for level ii and the completion time for level ii when using the shortcut. The rest of the line has n+1n + 1 integers ai,0,…,ai,na_{i,0}, \ldots, a_{i,n} (109≥ai,0≥ai,1≥…≥ai,n≥si10^9 \ge a_{i,0} \ge a_{i,1} \ge \ldots \ge a_{i,n} \ge s_i), where ai,ja_{i,j} is the completion time for level ii when playing by the rules with item jj.

Output

Output the minimum time needed to beat all of the levels, in any order.

Examples2

  1. Example 1

    Input
    3
    1 1 40 30 20 10
    3 1 95 95 95 10
    2 1 95 50 30 20
    
    Expected output
    91
    
  2. Example 2

    Input
    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
    
    Expected output
    17