#P4056. [JSOI2009] 火星藏宝图
[JSOI2009] 火星藏宝图
Description
After traveling on Mars for many days, jyy accidentally discovered a treasure map. According to the map, the treasure is buried on islands in a huge lake. For convenience, the map divides the entire lake into rows and columns , a total of cells, and numbers all islands from to . The -th island is located at row , column (that is, on the cell with coordinates , where are integers satisfying ), and the island contains treasure worth . There are islands at the top-left and bottom-right corners of the lake, and there are bridges connecting them to the land.
jyy soon found the lake and, to his amusement, discovered that the so-called treasure is the rare fruits produced on each island. At the shore of the island at the top-left corner, jyy found a small wooden boat, with which he can row to other islands. Rowing consumes stamina equal to the square of the Euclidean distance between two islands (that is, rowing from to costs units of stamina). jyy can eat fruit to restore stamina; consuming 1 unit of fruit value restores 1 unit of stamina.
Now jyy plans to travel from to , collecting rare fruits along the way. As hinted on the treasure map, after leaving an island, he may only go to the region to its lower-right (moving directly down or directly right is also allowed); otherwise, he will encounter a water monster. jyy may travel while hungry, i.e., stamina can be negative. Upon reaching the destination, as long as he has enough fruit with him, he will eat to restore his stamina to the pre-trip level.
jyy wants to know, after one trip, what is the maximum net gain, defined as jyy’s total collected fruit value minus jyy’s total stamina spent during the trip. (If he is still hungry after eating all the fruit, the net gain is negative; see the sample for a specific example.)
Input Format
The first line contains two integers .
Lines to : each line contains integers. On line , the integers are , , and .
Each island has distinct coordinates. It is guaranteed that there are islands at coordinates and .
Output Format
Output a single integer on the first line, representing the maximum net gain.
4 10
1 1 20
10 10 10
3 5 60
5 3 30
-4
Hint
Sample Explanation: $20+60+10-\left ( \left(3-1 \right )^2+\left (5-1 \right )^2 \right )-\left ( \left (10-3 \right )^2+\left (10-5 \right )^2 \right )=-4$
Constraints:
- For of the testdata, , and .
- For of the testdata, , and .
- For of the testdata, , and .
Translated by ChatGPT 5
京公网安备 11011102002149号