Under construction

Still being built. Things may change

Pay2Win

Time limit1sMemory limit1024 MB

Summary
Given K zone-swap patterns repeated until a boss with H health is defeated, find the start zone that maximizes the minimum skip cost needed to win.
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree, Array
Solved
No attempts yet

Problem

Hibi recently made a new game. This game is a so-called Pay2Win game, where players must spend real money.

With development almost finished, Hibi has only the boss battle left, and has already planned the boss patterns. The boss patterns that Hibi planned are as follows.

  • The boss stage is divided into NN zones, and the player starts at a starting position chosen by Hibi.
  • The boss uses KK patterns, and each pattern number is a distinct positive integer at most KK.
  • The boss uses all patterns one by one in order of their numbers. Each pattern is written as (i,j,x)(i,j,x), which swaps zone ii and zone jj. If the player is at zone ii, the player moves to zone jj, and if the player is at zone jj, the player moves to zone ii. The player can skip this pattern by paying xx won.
  • The boss starts with HH health, and when the boss health reaches 00, the player wins and the game ends.
  • After all KK patterns have passed, if the player is in zone NN, the player reduces the boss health by 11. Otherwise, the player loses and the game ends.
  • If the game has not ended, the KK patterns are applied again in order, and the check for whether the player is in zone NN repeats. The player does not return to the starting position, but skipping a pattern that was already skipped requires paying its cost again.

Hibi wants to earn as much as possible. So Hibi chooses a starting position that maximizes the minimum cost the player must pay to win. For a fair game, the player must have a way to win from the chosen starting position.

Input

The first line contains the number of zones NN, the number of patterns KK, and the boss health HH, separated by spaces. (1≤N≤200 0001\le N\le 200\,000, 0≤K≤200 0000\le K\le 200\,000, 1≤H≤1001\le H\le 100)

From the second line onward, the (p+1)(p+1)-th line contains three integers ii, jj, and xx separated by spaces, which describe the pp-th boss pattern. (1≤i<j≤N1\le i<j\le N, 0≤x≤1090\le x\le 10^9)

Output

Print, on the first line, the minimum cost the player must pay when Hibi chooses the starting position appropriately.

Examples2

  1. Example 1

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

    Input
    4 2 1
    1 2 1000000000
    3 4 0
    
    Expected output
    0