結果

問題 No.1334 Multiply or Add
ユーザー lam6er
提出日時 2025-03-26 15:54:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,517 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 271,668 KB
最終ジャッジ日時 2025-03-26 15:55:18
合計ジャッジ時間 9,148 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31 WA * 19 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

MOD = 10**9 + 7
n = int(input())
a = list(map(int, input().split()))
if n == 0:
print(0)
else:
sum_add = 0
product_add = a[0]
sum_mult = 0
product_mult = a[0]
for i in range(1, n):
# Calculate new add state
new_sum_add = max(sum_add + product_add, sum_mult + product_mult)
new_product_add = a[i]
# Calculate options for multiply state
option1_sum = sum_add
option1_product = product_add * a[i]
option1_total = option1_sum + option1_product
option2_sum = sum_mult
option2_product = product_mult * a[i]
option2_total = option2_sum + option2_product
if option1_total > option2_total:
new_sum_mult = option1_sum
new_product_mult = option1_product
elif option1_total < option2_total:
new_sum_mult = option2_sum
new_product_mult = option2_product
else:
if option1_product > option2_product:
new_sum_mult = option1_sum
new_product_mult = option1_product
else:
new_sum_mult = option2_sum
new_product_mult = option2_product
# Update states for next iteration
sum_add, product_add = new_sum_add, new_product_add
sum_mult, product_mult = new_sum_mult, new_product_mult
# Determine the maximum result
result = max(sum_add + product_add, sum_mult + product_mult) % MOD
print(result)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0