結果

問題 No.1334 Multiply or Add
ユーザー gew1fw
提出日時 2025-06-12 21:02:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,669 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 82,008 KB
実行使用メモリ 102,560 KB
最終ジャッジ日時 2025-06-12 21:04:48
合計ジャッジ時間 9,414 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 37 WA * 13 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

n = int(input())
A = list(map(int, input().split()))

current_group = None
groups = []

for num in A:
    if num == 1:
        if current_group is not None:
            if current_group['type'] == 'product':
                groups.append(current_group)
                current_group = {'type': 'ones', 'count': 1}
            else:
                current_group['count'] += 1
        else:
            current_group = {'type': 'ones', 'count': 1}
    else:
        if current_group is not None:
            if current_group['type'] == 'ones':
                groups.append(current_group)
                current_group = {'type': 'product', 'product': num}
            else:
                current_group['product'] *= num
        else:
            current_group = {'type': 'product', 'product': num}
if current_group is not None:
    groups.append(current_group)

i = 0
while i < len(groups):
    current = groups[i]
    if current['type'] == 'ones' and i > 0 and i < len(groups) - 1:
        prev = groups[i-1]
        next_grp = groups[i+1]
        if prev['type'] == 'product' and next_grp['type'] == 'product':
            k = current['count']
            p1 = prev['product']
            p2 = next_grp['product']
            if p1 * p2 > p1 + p2 + k:
                merged_product = p1 * p2
                groups = groups[:i-1] + [{'type': 'product', 'product': merged_product}] + groups[i+2:]
                i = max(i - 2, 0)
                continue
    i += 1

sum_total = 0
for grp in groups:
    if grp['type'] == 'product':
        sum_total += grp['product']
    else:
        sum_total += grp['count']
sum_total %= MOD

print(sum_total)
0