Jump
Time limit2sMemory limit512 MB
Tiles on a grid give energy when visited; jump only right or up at cost B, and maximize the energy left on reaching tile N.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Sorting
- Solved
- No attempts yet
Problem
There are tiles on a two-dimensional plane. Tile sits at and carries one energy bottle that refills your jumping energy by .
You jump to the right or up, and nowhere else. A jump from tile to tile is allowed when and (to the right), or when and (up). The two tiles do not have to be adjacent. Any tile that lies in that direction on the same row or the same column is a legal landing spot. You cannot jump to a place with no tile.
Every jump costs energy. You cannot jump while your energy is smaller than . The moment you land on a new tile you take its energy bottle and your energy grows by .
You start on tile with energy . Jump from tile to tile so that the energy left when you arrive at tile is as large as possible.
Write a program that finds that maximum energy.
Input
The first line contains the number of test cases . ()
The first line of each test case contains two integers and . (, )
The next lines describe tile through tile in order. Each line contains three integers , , . (, )
No two tiles share a coordinate. At least one sequence of jumps from tile to tile obeys the energy rule.
Output
For each test case, print on one line the maximum energy you can hold when you arrive at tile .