結果

問題 No.437 cwwゲーム
ユーザー はむ吉🐹
提出日時 2016-10-29 15:48:09
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,107 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-11-24 17:58:57
合計ジャッジ時間 2,295 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 39 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import itertools
import sys


REC_LIMIT = 10000


def cww_score(x):
    if x // 1000 > 0:
        return
    c, ww = divmod(x, 100)
    if c == 0:
        return
    w, m = divmod(ww, 11)
    if m != 0 or c == w:
        return
    else:
        return x


def compute_maximum_score(n):
    a = [int(c) for c in str(n)]
    all_digits = frozenset(range(len(a)))
    memo = dict()

    def rec(used_digits):
        if used_digits in memo:
            return memo[used_digits]
        res = 0
        unused_digits = all_digits - used_digits
        for i, j, k in itertools.combinations(unused_digits, 3):
            cww = 100 * a[i] + 10 * a[j] + a[k]
            score = cww_score(cww)
            if score is None:
                continue
            new_used_digits = used_digits | {i, j, k}
            res = max(res, rec(new_used_digits) + score)
        memo[used_digits] = res
        return res

    return rec(frozenset())


def main():
    sys.setrecursionlimit(REC_LIMIT)
    n = int(input())
    print(compute_maximum_score(n))


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