Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 11, 2026 10:54
Show Gist options
  • Select an option

  • Save maehrm/9808891e11f385d4601ad88e00792667 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/9808891e11f385d4601ad88e00792667 to your computer and use it in GitHub Desktop.
D - General Weighted Max Matching https://atcoder.jp/contests/abc318/tasks/abc318_d
N = int(input())
D = [[0] * N for _ in range(N)]
for i in range(N - 1):
d = list(map(int, input().split()))
for j in range(len(d)):
D[i][i + j + 1] = d[j]
D[i + j + 1][i] = d[j]
dp = [0] * (1 << N)
for bit in range(1 << N):
i = -1
for k in range(N):
if not ((bit >> k) & 1):
i = k
break
if i == -1:
continue
for j in range(i + 1, N):
if not ((bit >> j) & 1):
next_bit = bit | 1 << i | 1 << j
if dp[next_bit] < dp[bit] + D[i][j]:
dp[next_bit] = dp[bit] + D[i][j]
print(dp[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment