QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#22314. Meteor Shower

Statistics

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that $M$ meteors $(1 \le M \le 50,000)$ will strike, with meteor $i$ will striking point $(X_{i}, Y_{i})$ $(0 \le X_{i} \le 300; 0 \le Y_{i} \le 300)$ at time $T_{i}$ $(0 \le T_{i} \le 1,000)$. Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

Input

Read the input from standard input.

  • Line 1: A single integer: $M$
  • Lines $2,\ldots,M+1$: Line $i+1$ contains three space-separated integers: $X_{i}$, $Y_{i}$, and $T_{i}$

Output

Write the output to standard output.

  • Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Examples

Input 1

4
0 0 2
2 1 2
1 1 2
0 3 5

Output 1

5

There are four meteors, which strike points $(0, 0)$; $(2, 1)$; $(1, 1)$; and $(0, 3)$ at times 2, 2, 2, and 5, respectively.

    t = 0                t = 2              t = 5
5|. . . . . . .     5|. . . . . . .     5|. . . . . . .    
4|. . . . . . .     4|. . . . . . .     4|# . . . . . .   * = meteor impact
3|. . . . . . .     3|. . . . . . .     3|* # . . . . .  
2|. . . . . . .     2|. # # . . . .     2|# # # . . . .   # = destroyed pasture
1|. . . . . . .     1|# * * # . . .     1|# # # # . . .   
0|B . . . . . .     0|* # # . . . .     0|# # # . . . .   
  --------------      --------------      -------------- 
  0 1 2 3 4 5 6       0 1 2 3 4 5 6       0 1 2 3 4 5 6

Examining the plot above at $t=5$, the closest safe point is $(3, 0)$ -- but Bessie's path to that point is too quickly blocked off by the second meteor. The next closest point is $(4,0)$ -- also blocked too soon. Next closest after that are lattice points on the $(0,5)$-$(5,0)$ diagonal. Of those, any one of $(0,5)$, $(1,4)$, and $(2,3)$ is reachable in 5 timeunits.

       5|. . . . . . .   
       4|. . . . . . .   
       3|3 4 5 . . . .    Bessie's locations over time
       2|2 . . . . . .    for one solution
       1|1 . . . . . .   
       0|0 . . . . . .   
         -------------- 
         0 1 2 3 4 5 6

Problem credits: Jeffrey Wang, 2007

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.