Why Hobanwoo Was Late for School 8
Time limit2sMemory limit1024 MB
Find the minimum total time to reach planet N from the origin, where a move to planet e costs a_e if the start lies on the hull of planets 0 to e, otherwise b_e.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Geometry, Divide and conquer
- Solved
- No attempts yet
Problem
Now Hobanwoo wants to return to the beloved Kyungpook National University. There is one problem, though. Getting from Earth to the otherworld is easy, but getting back from the otherworld to Earth is hard.
In a universe that can be drawn on a two-dimensional coordinate plane, there are planets besides the otherworld. Sorted by increasing value, they are called planet 1, planet 2, ..., planet .
For each with , planet is located at the integer coordinate in the first quadrant and has positive integers and .
At first, Hobanwoo is at , which is the otherworld and the position of planet 0. Hobanwoo wants to reach planet , where a dimensional gate back to Earth is located, by passing through several planets.
Hobanwoo can pass over planets in a single move. More precisely, when Hobanwoo is at planet and wants to move to planet , it must hold that . The travel time is if planet lies on the boundary of the smallest convex polygon that contains all planets from planet 0 to planet . Otherwise, the travel time is . A straight line counts as a convex polygon.
Help Hobanwoo arrive at planet from the otherworld quickly so that he is not late for school!
Input
The first line contains the number of test cases . ()
Each test case begins with a line containing and , separated by a space. ()
From the second line onward, the next lines each contain , , , and , separated by spaces. ()
For every test case, always holds.
The sum of over all test cases does not exceed .
Output
For each test case, print the minimum time it takes for Hobanwoo to start from the otherworld and reach planet .
Hint
Input and output are large, so fast I/O is recommended. Fast I/O for common languages is described below.
- C++: If you use
cinandcout, you must callcin.tie(nullptr); ios::sync_with_stdio(false);once before any I/O. To start a new line, use'\n'instead ofendl. - Java: You must use
BufferedReaderandBufferedWriter. - Python3, PyPy3: Use
sys.stdin.readline().rstrip()instead ofinput().