結果

問題 No.1334 Multiply or Add
ユーザー gew1fw
提出日時 2025-06-12 20:58:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,199 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 266,524 KB
最終ジャッジ日時 2025-06-12 21:02:52
合計ジャッジ時間 10,193 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46 WA * 4 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

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

groups = []
i = 0

while i < n:
    # Skip leading 1s
    while i < n and A[i] == 1:
        groups.append(1)
        i += 1
    if i >= n:
        break
    
    # Find the next number >1 after i
    j = i + 1
    while j < n and A[j] == 1:
        j += 1
    
    if j < n and A[j] > 1:
        # There are at least two numbers >1, start forming a group
        k = j
        while True:
            # Look for the next number >1 after k
            l = k + 1
            while l < n and A[l] == 1:
                l += 1
            if l < n and A[l] > 1:
                k = l
            else:
                break
        # Group from i to k inclusive
        group = A[i:k+1]
        product = 1
        sum_group = 0
        for num in group:
            product *= num
            sum_group += num
        if product >= sum_group:
            groups.append(product % MOD)
        else:
            for num in group:
                groups.append(num % MOD)
        i = k + 1
    else:
        # Only one number >1 in this segment
        groups.append(A[i] % MOD)
        i += 1

total = sum(groups) % MOD
print(total)
0