Under construction

Still being built. Things may change

Why Hobanwoo Was Late for School 8

Time limit2sMemory limit1024 MB

Summary
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 NN planets besides the otherworld. Sorted by increasing xx value, they are called planet 1, planet 2, ..., planet NN.

For each ii with 1≤i≤N1 \le i \le N, planet ii is located at the integer coordinate (xi,yi)(x_i, y_i) in the first quadrant and has positive integers aia_i and bib_i.

At first, Hobanwoo is at (0,0)(0, 0), which is the otherworld and the position of planet 0. Hobanwoo wants to reach planet NN, where a dimensional gate back to Earth is located, by passing through several planets.

Hobanwoo can pass over MM planets in a single move. More precisely, when Hobanwoo is at planet ss and wants to move to planet ee, it must hold that s≤e≤s+Ms \le e \le s+M. The travel time is aea_e if planet ss lies on the boundary of the smallest convex polygon that contains all planets from planet 0 to planet ee. Otherwise, the travel time is beb_e. A straight line counts as a convex polygon.

Help Hobanwoo arrive at planet NN from the otherworld quickly so that he is not late for school!

Input

The first line contains the number of test cases TT. (1≤T≤60 0001 \le T \le 60\,000)

Each test case begins with a line containing NN and MM, separated by a space. (1≤M≤N≤300 0001 \le M \le N \le 300\,000)

From the second line onward, the next NN lines each contain xix_i, yiy_i, aia_i, and bib_i, separated by spaces. (1≤xi,yi,ai,bi≤1091 \le x_i, y_i, a_i, b_i \le 10^9)

For every test case, x1<x2<⋯<xNx_1 < x_2 < \cdots < x_N always holds.

The sum of NN over all test cases does not exceed 300 000300\,000.

Output

For each test case, print the minimum time it takes for Hobanwoo to start from the otherworld and reach planet NN.

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 cin and cout, you must call cin.tie(nullptr); ios::sync_with_stdio(false); once before any I/O. To start a new line, use '\n' instead of endl.
  • Java: You must use BufferedReader and BufferedWriter.
  • Python3, PyPy3: Use sys.stdin.readline().rstrip() instead of input().

Examples1

  1. Example 1

    Input
    3
    2 1
    1 2 3 1
    2 3 2 1
    3 2
    1 2 2 2
    7 3 1 1
    12 9 3 4
    4 2
    2 5 2 6
    6 4 7 3
    11 6 9 1
    17 7 4 9
    
    Expected output
    5
    4
    16