結果

問題 No.357 品物の並び替え (Middle)
ユーザー norioc
提出日時 2025-03-20 06:50:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 524 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 70,824 KB
最終ジャッジ日時 2025-03-20 06:50:29
合計ジャッジ時間 2,761 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

N, M = map(int, input().split())
d = defaultdict(list)
for _ in range(M):
    a, b, c = map(int, input().split())
    d[b].append((a, c))

dp = [-1] * (1 << N)
dp[0] = 0
for i in range(1 << N):
    if dp[i] == -1: continue

    for j in range(N):
        if i & (1 << j): continue
        score = 0
        for x, c in d[j]:
            if i & (1 << x):
                score += c

        ni = i | (1 << j)
        dp[ni] = max(dp[ni], dp[i] + score)

ans = dp[(1 << N) - 1]
print(ans)
0