結果
問題 |
No.3105 Parallel Connection and Spanning Trees
|
ユーザー |
![]() |
提出日時 | 2025-06-12 20:52:42 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,284 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 81,796 KB |
実行使用メモリ | 84,604 KB |
最終ジャッジ日時 | 2025-06-12 20:56:57 |
合計ジャッジ時間 | 7,243 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 2 |
other | WA * 3 TLE * 1 -- * 28 |
ソースコード
import sys from itertools import combinations def main(): input = sys.stdin.read().split() ptr = 0 T = int(input[ptr]) ptr +=1 for _ in range(T): N, P = int(input[ptr]), int(input[ptr+1]) ptr +=2 A = list(map(int, input[ptr:ptr+N])) ptr +=N total =0 for k in range(1, N+1): for subset in combinations(A, k): S = sum(subset) dp = {0} max_sum = 0 for num in subset: new_dp = set() for s in dp: new_s1 = s + num new_s2 = s - num new_dp.add(new_s1) new_dp.add(new_s2) if new_s1 > max_sum: max_sum = new_s1 if new_s2 < -max_sum: max_sum = -new_s2 dp = new_dp min_abs = float('inf') for s in dp: if abs(s) < min_abs: min_abs = abs(s) total += min_abs if total >= P: total %= P print(total % P) if __name__ == '__main__': main()