Pay2Win
Time limit1sMemory limit1024 MB
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 zones, and the player starts at a starting position chosen by Hibi.
- The boss uses patterns, and each pattern number is a distinct positive integer at most .
- The boss uses all patterns one by one in order of their numbers. Each pattern is written as , which swaps zone and zone . If the player is at zone , the player moves to zone , and if the player is at zone , the player moves to zone . The player can skip this pattern by paying won.
- The boss starts with health, and when the boss health reaches , the player wins and the game ends.
- After all patterns have passed, if the player is in zone , the player reduces the boss health by . Otherwise, the player loses and the game ends.
- If the game has not ended, the patterns are applied again in order, and the check for whether the player is in zone 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 , the number of patterns , and the boss health , separated by spaces. (, , )
From the second line onward, the -th line contains three integers , , and separated by spaces, which describe the -th boss pattern. (, )
Output
Print, on the first line, the minimum cost the player must pay when Hibi chooses the starting position appropriately.