[Python] ABC018 D 30点

問題

D – バレンタインデー

回答

AtCoder Beginner Contest 018 解説 from AtCoder Inc.

itertools.combinations を使い、全ての組み合わせを列挙します。

import itertools

N, M, P, Q, R = map(int, input().split())
xyzs = [list(map(int, input().split())) for _ in range(R)]

girls_cmb = list(itertools.combinations(range(1, N+1), P))
boys_cmb = list(itertools.combinations(range(1, M+1), Q))

ans = 0
for girls in girls_cmb:
    for boys in boys_cmb:
        happiness = 0
        for xyz in xyzs:
            girl = xyz[0]
            boy = xyz[1]
            if girl in girls and boy in boys:
                happiness += xyz[2]
        ans = max(ans, happiness)

print(ans)