Game with triangles
Time limit2sMemory limit2048 MB
Points lie on the lines y=0 and y=2. For each k, find the maximum total area of k disjoint triangles, plus the largest possible k.
- Level
Medium7 of 10
- Topics
- Sorting, Prefix sum, Greedy, Math
- Solved
- No attempts yet
Statement
Even Little John needs money to buy a house. But he recently lost his job, so how will he earn money now? By playing a game that pays him as a reward, of course. Oh well, it is probably not the kind of game you are thinking of.
There are distinct points on the plane. Initially, your score is . To increase your score, you can perform the following operation:
- Choose three distinct points that are not collinear.
- Increase your score by the area of the triangle formed by these three points.
- Erase the three points from the plane.

An instance of the game, where the operation is performed twice.
Let be the maximum number of operations that can be performed. For example, if no operation is possible, is . Define as the maximum score achievable by performing the operation exactly times. is defined for every integer with .
Find the value of , and find the value of for each integer .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The test cases follow.
The first line of each test case contains two integers and ().
The second line contains pairwise distinct integers , the points on ().
The third line contains pairwise distinct integers , the points on ().
The sum of and the sum of over all test cases each do not exceed .
Output
For each test case, output at most two lines.
The first line contains the value of .
The second line contains integers . You may omit this line if is .
Under the constraints of this problem, every value of is an integer no greater than .
Hint
In the first test case, there are points . It can be shown that you cannot perform two or more operations. So is , and only is asked.
You can choose , , and as the three vertices. The triangle has area , so your score increases by and the three points are erased. It can be shown that the maximum score after one operation is . Therefore .
In the fifth test case, there are points. It can be shown that you cannot perform three or more operations. So is , and and are asked.
To maximize the score with one operation, choose , , and . The score becomes , so .
To maximize the score with exactly two operations, first choose , , and . Then choose , , and . The score becomes , so .