Colored Rectangles
Contest: Educational Codeforces Round 93 (Rated for Div. 2)
Difficulty rating: 1800
Tags: dp, greedy, sortings

You are given three multisets of pairs of colored sticks:

- $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$;
- $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$;
- $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$;

You are constructing rectangles from these pairs of sticks with the following process:

1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.

Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.

Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.

What is the maximum area you can achieve?

Input format:
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.

The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks.

The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks.

The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.

Output format:
Print the maximum possible total area of the constructed rectangles.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.

In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.

In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.

Example input:
1 1 1
3
5
4

Example output:
20

Example input:
2 1 3
9 5
1
2 8 5

Example output:
99

Editorial:
Let's build some rectangles and take a look at the resulting pairings. For example, consider only red/green rectangles. Let the rectangles be $$$(r_{i1}, g_{i1})$$$, $$$(r_{i2}, g_{i2})$$$, .... Sort them in a non-decreasing order of $$$r_{ij}$$$. I claim that in the most optimal set $$$g_{ij}$$$ are also sorted in a non-decreasing order. It's easy to prove with some induction.
Moreover, if there are some green or red sticks that are not taken and that are longer than the smallest taken corresponding sticks, then it's always optimal to take those instead.
These facts helps us to conclude that from each set only some suffix of the largest sticks are taken. And they also give us the idea of the solution: sort the sticks in each set and pick the largest from any of the two sets into a pair until no pairs can be taken.
However, the greedy approach of "take from any two of the three sets" is incorrect. We need to choose these two sets smartly. Let $$$dp[r][g][b]$$$ store the maximum total area that can be obtained by taking $$$r$$$ largest red sticks, $$$g$$$ largest green sticks and $$$b$$$ largest blue sticks. Each transition chooses a pair of colors and takes the next pairs in both of them.
The answer is the maximum value in all the $$$dp$$$.
Overall complexity: $$$O(RGB + R \log R + G \log G + B \log B)$$$.