結果

問題 No.1633 Sorting Integers (Multiple of 2^K)
ユーザー rin204
提出日時 2023-12-20 23:49:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 905 ms / 2,000 ms
コード長 634 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 82,752 KB
実行使用メモリ 257,836 KB
最終ジャッジ日時 2024-09-27 10:17:24
合計ジャッジ時間 12,047 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
C = list(map(int, input().split()))
A = []
for i, c in enumerate(C, 1):
    A += [i] * c
memo = {}


def f(bit, tot, t, x, ten):
    if tot % x != 0:
        return 0
    if t == n:
        return (tot & -tot).bit_length() - 1

    tmp = tot * n + t
    if tmp in memo:
        return memo[tmp]

    ret = t
    for i in range(n):
        if not bit >> i & 1:
            if i > 0 and A[i - 1] == A[i] and not (bit >> (i - 1) & 1):
                continue
            ret = max(ret, f(bit ^ (1 << i), A[i] * ten + tot, t + 1, x * 2, ten * 10))

    memo[tmp] = ret
    return ret


ans = f(0, 0, 0, 1, 1)
print(ans)
0