This Means War
Time limit2sMemory limit512 MB
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 soldiers as points on the x-axis. All soldiers stand at distinct locations, and the -th soldier stands at . The soldiers are numbered from 1 to 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 with contains soldiers through (inclusive), and no soldier from another group stands between them.
Each soldier has a function . This function is used only when soldier is the leftmost soldier of its group. The strength of a group from position to position is : the value of at the x value of the rightmost soldier in the group.
You are given distinct sorted values and the values for every function. For any other than these values, connect consecutive points with straight line segments. This defines each from to .
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 (). Each test case is given as follows.
The first line contains two integers and (, , ). The next line contains the sorted integers (). The next line contains the sorted integers (). The next lines each contain integers. The -th integer on the -th line is ().
It is guaranteed that and .
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 , , and 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 , which is . The last soldier's group has strength . The total strength is therefore 6.666667 (rounded to 6 decimal places).