結果

問題 No.1334 Multiply or Add
ユーザー maspymaspy
提出日時 2021-01-08 22:03:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,216 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 110,272 KB
最終ジャッジ日時 2024-04-28 03:04:01
合計ジャッジ時間 9,832 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,488 KB
testcase_01 AC 39 ms
52,136 KB
testcase_02 AC 39 ms
53,816 KB
testcase_03 AC 132 ms
109,736 KB
testcase_04 AC 122 ms
109,860 KB
testcase_05 AC 122 ms
109,680 KB
testcase_06 AC 124 ms
109,848 KB
testcase_07 AC 138 ms
109,988 KB
testcase_08 AC 119 ms
109,800 KB
testcase_09 AC 139 ms
109,708 KB
testcase_10 AC 146 ms
110,272 KB
testcase_11 AC 136 ms
109,740 KB
testcase_12 AC 129 ms
110,040 KB
testcase_13 AC 124 ms
109,692 KB
testcase_14 AC 121 ms
109,900 KB
testcase_15 AC 124 ms
109,808 KB
testcase_16 AC 88 ms
104,808 KB
testcase_17 AC 87 ms
103,892 KB
testcase_18 AC 88 ms
103,808 KB
testcase_19 AC 198 ms
109,744 KB
testcase_20 AC 227 ms
109,688 KB
testcase_21 AC 210 ms
109,656 KB
testcase_22 AC 109 ms
109,596 KB
testcase_23 AC 145 ms
109,944 KB
testcase_24 AC 117 ms
109,884 KB
testcase_25 AC 134 ms
109,736 KB
testcase_26 AC 121 ms
109,860 KB
testcase_27 AC 118 ms
109,996 KB
testcase_28 AC 122 ms
109,744 KB
testcase_29 AC 131 ms
109,864 KB
testcase_30 AC 124 ms
109,776 KB
testcase_31 AC 136 ms
109,776 KB
testcase_32 AC 131 ms
109,792 KB
testcase_33 AC 124 ms
109,864 KB
testcase_34 AC 143 ms
110,080 KB
testcase_35 AC 149 ms
109,816 KB
testcase_36 AC 141 ms
109,968 KB
testcase_37 AC 141 ms
109,904 KB
testcase_38 AC 140 ms
109,744 KB
testcase_39 AC 39 ms
52,620 KB
testcase_40 AC 84 ms
89,048 KB
testcase_41 AC 64 ms
78,156 KB
testcase_42 AC 67 ms
79,632 KB
testcase_43 AC 105 ms
102,048 KB
testcase_44 AC 82 ms
89,056 KB
testcase_45 AC 66 ms
78,588 KB
testcase_46 AC 85 ms
89,224 KB
testcase_47 AC 99 ms
98,596 KB
testcase_48 AC 87 ms
90,948 KB
testcase_49 AC 118 ms
105,676 KB
testcase_50 AC 95 ms
108,136 KB
testcase_51 AC 94 ms
108,560 KB
testcase_52 AC 94 ms
109,324 KB
testcase_53 AC 93 ms
107,880 KB
testcase_54 AC 94 ms
109,408 KB
testcase_55 AC 93 ms
107,624 KB
testcase_56 AC 42 ms
52,568 KB
testcase_57 AC 40 ms
52,548 KB
testcase_58 AC 42 ms
53,912 KB
testcase_59 AC 39 ms
52,204 KB
testcase_60 AC 40 ms
52,080 KB
testcase_61 AC 39 ms
52,836 KB
testcase_62 AC 41 ms
52,944 KB
testcase_63 AC 40 ms
52,672 KB
testcase_64 AC 39 ms
52,192 KB
testcase_65 AC 40 ms
51,936 KB
testcase_66 AC 83 ms
97,676 KB
testcase_67 AC 71 ms
86,312 KB
testcase_68 AC 46 ms
60,432 KB
testcase_69 AC 89 ms
103,164 KB
testcase_70 AC 87 ms
102,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

MOD = 1_000_000_007

def main(A):
    # 総積がでかいなら、端の 1 以外はまとめてしまう
    p = 1
    INF = 1 << 20
    for x in A:
        p = min(p * x, INF)
    if p == INF:
        add = 0
        for x in A:
            if x == 1:
                add += 1
            else:
                break
        for x in A[::-1]:
            if x == 1:
                add += 1
            else:
                break
        p = 1
        for x in A:
            p = p * x % MOD
        return (p + add) % MOD
    # 以下、総積が小さい場合。
    # 途中経過は、"a + b*" or "a+" と書ける。
    # b ごとに optimal な a を持つ
    dp = {}
    max_v = 0
    for x in A:
        newdp = {}
        vv = 0
        for b, a in dp.items():
            # a + b * [x] +
            vv = max(vv, a + b * x)
            # a + b * [x] *
            newdp[b * x] = max(newdp.get(b * x, 0), a)
        newdp[x] = max(newdp.get(x, 0), max_v)
        dp = newdp
        max_v = max(vv, max_v + x)
    return max_v

A = tuple(map(int, read().split()))[1:]

print(main(A))
0