Under construction

Still being built. Things may change

This Means War

Time limit2sMemory limit512 MB

Summary
Split sorted soldiers into contiguous groups to maximize the sum of f_a(x_b) over each group, where a is the group's leftmost and b its rightmost soldier.
Level

Hard8 of 10

Topics
Dynamic programming, Geometry
Solved
No attempts yet

Problem

An army is going into war, and the commander wants to divide all soldiers into groups so that their total strength is maximized. Treat the NN soldiers as points on the x-axis. All soldiers stand at distinct locations, and the ii-th soldier stands at xix_i. The soldiers are numbered from 1 to NN and are given sorted by their x values from left to right.

Divide the soldiers into one or more groups. Each soldier belongs to exactly one group, and every group is a contiguous block. A group [a,b][a, b] with a≤ba \le b contains soldiers aa through bb (inclusive), and no soldier from another group stands between them.

Each soldier ii has a function fif_i. This function is used only when soldier ii is the leftmost soldier of its group. The strength of a group from position aa to position bb is fa(xb)f_a(x_b): the value of faf_a at the x value of the rightmost soldier in the group.

You are given MM distinct sorted values z1,z2,…,zMz_1, z_2, \dots, z_M and the values fi(zj)f_i(z_j) for every function. For any zz other than these MM values, connect consecutive points (zj,fi(zj))(z_j, f_i(z_j)) with straight line segments. This defines each fif_i from z1z_1 to zMz_M.

The strength of the army is the sum of the strengths of all groups. Find the grouping that maximizes this sum.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100). Each test case is given as follows.

The first line contains two integers NN and MM (1≤N≤1051 \le N \le 10^5, 2≤M2 \le M, N×M≤105N \times M \le 10^5). The next line contains the sorted integers x1,x2,…,xNx_1, x_2, \dots, x_N (−106≤xi≤106-10^6 \le x_i \le 10^6). The next line contains the sorted integers z1,z2,…,zMz_1, z_2, \dots, z_M (−106≤zj≤106-10^6 \le z_j \le 10^6). The next NN lines each contain MM integers. The jj-th integer on the ii-th line is fi(zj)f_i(z_j) (−106≤fi(zj)≤106-10^6 \le f_i(z_j) \le 10^6).

It is guaranteed that z1≤x1z_1 \le x_1 and xN≤zMx_N \le z_M.

Output

For each test case, print one line with the maximum strength of the army, rounded to exactly 6 decimal places.

Hint

The following image shows the first test case.

The 3 solid circles on the x-axis mark the locations of the 3 soldiers. The 3 functions f1f_1, f2f_2, and f3f_3 are plotted as described above, one for each soldier. The best solution puts the first 2 soldiers in one group and leaves the last soldier alone. The first group has strength f1(x2)f_1(x_2), which is f1(2)=4f_1(2) = 4. The last soldier's group has strength f3(x3)=f3(3)=2.666667f_3(x_3) = f_3(3) = 2.666667. The total strength is therefore 6.666667 (rounded to 6 decimal places).

Examples1

  1. Example 1

    Input
    3
    3 4
    -5 2 3
    -6 1 4 5
    -1 3 6 0
    2 -2 -4 -6
    -4 0 4 5
    5 2
    -2 5 8 9 10
    -2 10
    -7 -6
    -3 -7
    0 -8
    9 -10
    5 -4
    2 2
    0 7
    -2 7
    -10 -2
    -4 -10
    
    Expected output
    6.666667
    -6.000000
    -2.000000