This page is still under construction.

Parts of this page are still being built. What you see may change.

The Way to Bytemountain

Time limit1sMemory limit32 MB

Summary
Find a walk from crossing 1 to crossing n that deviates from the signpost arrows at most k times and maximizes total trail beauty.
Level

Hard8 of 10

Topics
Graph, Dynamic programming, Binary search, Greedy
Solved
No attempts yet

Problem

Byteman is staying at a mountain hostel and wants to climb to the peak of Bytemountain. The mountain has nn trail crossings, numbered from 11 to nn. The hostel is at crossing 11 and the peak is crossing nn.

At every crossing stands a single signpost that points along exactly one of the trails leaving that crossing. Normally every signpost points toward the peak, but the signposts are currently being reorganized, so a signpost may point along any trail — even the signpost at the peak points along some trail.

A guide gives directions of the following form. Starting at the hostel, keep following the trails indicated by the signposts until you reach crossing s1s_1; there, ignore the signpost and instead take the trail joining s1s_1 and c1c_1. Then follow the signposts again until you reach s2s_2 and take the trail joining s2s_2 and c2c_2, and so on. After the ii-th such step of consulting the map (taking a chosen trail instead of the signpost trail) at crossing sis_i, keep following the signposts and you will eventually arrive at the peak.

Byteman does not want the directions to be too complicated, so he asks the guide to consult the map at most kk times (that is, to take at most kk trails that differ from the signpost trails).

The route may pass through the same trail or crossing several times. Byteman's walk ends the first time he reaches the peak after all of the guide's instructions have been carried out; he may pass through the peak earlier without stopping.

Every trail has a beauty value. Find the maximum possible total beauty of the trails traversed on a route from the hostel to the peak that consults the map at most kk times.

Input

The first line contains two integers nn and kk (1≤n≤50 0001 \le n \le 50\,000, 0≤k≤1000 \le k \le 100): the number of crossings and the maximum number of times Byteman may consult the map. Crossings are numbered from 11 to nn; the hostel is crossing 11 and the peak is crossing nn.

Each of the next nn lines describes one crossing. The ii-th of these lines begins with an integer mim_i (1≤mi≤n−11 \le m_i \le n-1), the number of trails leaving crossing ii, followed by mim_i pairs ai,j bi,ja_{i,j}\ b_{i,j} (1≤ai,j≤n1 \le a_{i,j} \le n, 1≤bi,j≤10 0001 \le b_{i,j} \le 10\,000), each meaning that a trail leads from crossing ii to crossing ai,ja_{i,j} and has beauty bi,jb_{i,j}. The first pair on each line is the trail that the signpost at crossing ii points to.

Every trail is bidirectional and joins two different crossings, and any two crossings are joined by at most one trail. Each trail appears twice in the input, once in the list of each of its two endpoints, with the same beauty both times. The total number of trails does not exceed 100 000100\,000.

Output

Print a single integer: the maximum total beauty of the trails on a valid route from the hostel to the peak that consults the map at most kk times. Such a route is guaranteed to exist.

Hint

In the figure, the edges are trails joining crossings, the numbers next to the edges are the beauties of the trails, and the arrows show the trail that each signpost points to.

Consulting the map twice, at crossings 33 and 22, yields the route 1→3→4→2→51 \to 3 \to 4 \to 2 \to 5 with total beauty 1414.

Examples3

  1. Example 1

    Input
    5 2
    2 3 4 2 2
    3 1 2 5 4 4 3
    2 1 4 4 3
    3 2 3 5 5 3 3
    2 2 4 4 5
    
    Expected output
    14
    
  2. Example 2

    Input
    2 0
    1 2 5
    1 1 5
    
    Expected output
    5
    
  3. Example 3

    Input
    3 0
    1 2 3
    2 3 7 1 3
    1 2 7
    
    Expected output
    10