結果

問題 No.698 ペアでチームを作ろう
ユーザー roaris
提出日時 2019-08-21 12:01:37
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 727 ms / 1,000 ms
コード長 686 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2024-10-08 10:05:49
合計ジャッジ時間 8,197 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def rec(S, v):
    if memo[S][v] != -1:
        return memo[S][v]
        
    if S == (1 << N) - 1:
        memo[S][v] = 0
        return 0
    
    res = -10 ** 18
    c = bin(S).count("1")
    
    for u in range(N):
        if not S >> u & 1:
            if c % 2 == 1:
                res = max(res, rec(S | 1 << u, u) + (A[v] ^ A[u]))
            else:
                res = max(res, rec(S | 1 << u, u))
    
    memo[S][v] = res
    
    return res

N = int(input())
A = list(map(int, input().split()))
memo = [[-1 for _ in range(N)] for _ in range(1 << N)]
ans = -10 ** 18

for i in range(N):
    ans = max(ans, rec(1 << i, i))

print(ans)
0