Colored Rectangles (Codeforces 1398D)
Tags: 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:   take a pair of sticks of one color;  take a pair of sticks of another color different from the first one;  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: 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: Print the maximum possible total area of the constructed rectangles.

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.

Sample Inputs:
1 1 1
3
5
4

2 1 3
9 5
1
2 8 5

10 1 1
11 7 20 15 19 14 2 4 13 14
8
11

Sample Outputs:
20

99

372

Solution (Python):
import sys
input = sys.stdin.readline
(rr, gg, bb) = map(int, input().split())
inf = 114514
r = list(map(int, input().split())) + [inf]
g = list(map(int, input().split())) + [inf]
b = list(map(int, input().split())) + [inf]
r.sort(reverse=True)
g.sort(reverse=True)
b.sort(reverse=True)
dp = []
for _ in range(rr + 1):
    dp.append([[0] * (bb + 1) for _ in range(gg + 1)])
ans = 0
for i in range(rr + 1):
    ri = r[i]
    for j in range(gg + 1):
        gj = g[j]
        for k in range(bb + 1):
            bk = b[k]
            if (i + j + k) % 2:
                continue
            dpijk = 0
            if i > 0 and j > 0:
                dpijk = max(dp[i - 1][j - 1][k] + ri * gj, dpijk)
            if j > 0 and k > 0:
                dpijk = max(dp[i][j - 1][k - 1] + gj * bk, dpijk)
            if k > 0 and i > 0:
                dpijk = max(dp[i - 1][j][k - 1] + bk * ri, dpijk)
            dp[i][j][k] = dpijk
            if ans < dpijk:
                ans = dpijk
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
(rr, gg, bb) = map(int, input().split())
inf = 114514
r = list(map(int, input().split())) + [inf]
g = list(map(int, input().split())) + [inf]
b = list(map(int, input().split())) + [inf]
r.sort(reverse=True)
g.sort(reverse=True)
b.sort(reverse=True)
dp = []
for _ in range(rr + 1):
    dp.append([[0] * (bb + 1) for _ in range(gg + 1)])
ans = 0
for i in range(rr + 1):
    ri = r[i]
    for j in range(gg + 1):
        gj = g[j]
        for k in range(bb + 1):
            bk = b[k]
            if (i + j + k) % 2:
                continue
            dpijk = 0
            if i > 0 and j > 0:
                dpijk = max(dp[i - 1][j - 1][k] + ri * gj, dpijk)
            if j > 0 and k > 0:
                dpijk = max(dp[i][j - 1][k - 1] + gj * bk, dpijk)
            if k > 0 and i > 0:
                dpijk = max(dp[i - 1][j][k - 1] + bk * ri, dpijk)
            dp[i][j][k] = dpijk
            if ans < dpijk:
                ans = dpijk
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.readline())

def MI():
    return map(int, sys.stdin.readline().split())

def LI():
    return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def SI():
    return sys.stdin.readline()[:-1]
(rn, gn, bn) = MI()
rr = LI()
gg = LI()
bb = LI()
rr.sort(reverse=True)
gg.sort(reverse=True)
bb.sort(reverse=True)
dp = [[[-1] * (bn + 1) for _ in range(gn + 1)] for _ in range(rn + 1)]
dp[0][0][0] = 0
ans = 0
for i in range(rn + 1):
    for j in range(gn + 1):
        for k in range(bn + 1):
            pre = dp[i][j][k]
            if pre == -1:
                continue
            ans = max(ans, pre)
            if i < rn and j < gn:
                dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k], pre + rr[i] * gg[j])
            if i < rn and k < bn:
                dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1], pre + rr[i] * bb[k])
            if j < gn and k < bn:
                dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], pre + gg[j] * bb[k])
print(ans)