結果

問題 No.1279 Array Battle
ユーザー lam6er
提出日時 2025-03-20 20:21:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,112 KB
実行使用メモリ 69,048 KB
最終ジャッジ日時 2025-03-20 20:22:41
合計ジャッジ時間 1,948 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools

def main():
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    b = list(map(int, sys.stdin.readline().split()))
    
    dp = {}
    dp[0] = (0, 1)  # (max_sum, count)
    
    for m in range(n):  # current step m (0-based)
        for subset in itertools.combinations(range(n), m):
            mask = sum(1 << j for j in subset)
            if mask not in dp:
                continue
            current_sum, current_count = dp[mask]
            # Next step is (m+1), uses b[m]
            for j in range(n):
                if j not in subset:
                    contribution = max(0, a[j] - b[m])
                    new_sum = current_sum + contribution
                    new_mask = mask | (1 << j)
                    if new_mask not in dp:
                        dp[new_mask] = (new_sum, current_count)
                    else:
                        existing_sum, existing_count = dp[new_mask]
                        if new_sum > existing_sum:
                            dp[new_mask] = (new_sum, current_count)
                        elif new_sum == existing_sum:
                            dp[new_mask] = (new_sum, existing_count + current_count)
    
    final_mask = (1 << n) - 1
    print(dp.get(final_mask, (0, 0))[1])

if __name__ == "__main__":
    main()
0