Created
March 24, 2024 06:24
-
-
Save maehrm/0c22d27f41513399b8315d2cb6945194 to your computer and use it in GitHub Desktop.
U - Grouping https://atcoder.jp/contests/dp/tasks/dp_u
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = int(input()) | |
a = [list(map(int, input().split())) for _ in range(N)] | |
dp = [0] * (1 << N) | |
cst = [0] * (1 << N) | |
for msk in range(1 << N): # あらかじめmskグループのときの点数をcstに求めておく | |
for i in range(N): | |
for j in range(i + 1, N): | |
if (msk & (1 << i)) and (msk & (1 << j)): | |
cst[msk] += a[i][j] | |
for msk in range(1 << N): | |
msk2 = msk # msk2は、mskの部分集合を表す | |
while msk2 > 0: | |
nxt = dp[msk - msk2] + cst[msk2] | |
dp[msk] = max(dp[msk], nxt) | |
msk2 = (msk2 - 1) & msk # この式でmskの部分集合を作っていく | |
print(dp[(1 << N) - 1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment