Directions
Time limit4sMemory limit512 MB
Pick the cheapest set of direction vectors whose nonnegative multiples let Snuke reach any point from any other, or print -1 if no set works.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
Initially, Snuke cannot move at all. There are tickets, and the price of the -th ticket is . If Snuke buys the -th ticket, then for every point and every nonnegative number , he can move from to . Snuke wants to buy tickets so that he can travel between any two points. Compute the minimal possible total price of the tickets he must buy.
Input
The first line contains one integer (). Then lines follow, the -th of which contains three integers , , (, ).
Output
Print the minimal possible total price of the tickets he must buy to be able to move between any two points. If this is impossible, print -1 instead.
Hint
For example, Snuke can buy tickets 1, 3, and 6.