結果

問題 No.193 筒の数式
ユーザー lam6er
提出日時 2025-03-20 20:29:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 36 ms / 1,000 ms
コード長 1,392 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,620 KB
実行使用メモリ 54,324 KB
最終ジャッジ日時 2025-03-20 20:30:35
合計ジャッジ時間 1,493 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

def is_valid(formula):
    n = len(formula)
    if n < 3:
        return False
    if formula[0] in '+-' or formula[-1] in '+-':
        return False
    has_operator = False
    prev_is_op = False
    for i in range(n):
        c = formula[i]
        if c in '+-':
            if prev_is_op:
                return False
            has_operator = True
            prev_is_op = True
        else:
            prev_is_op = False
    return has_operator

def evaluate(formula):
    tokens = []
    current_number = []
    for c in formula:
        if c in '+-':
            num = int(''.join(current_number))
            tokens.append(num)
            tokens.append(c)
            current_number = []
        else:
            current_number.append(c)
    if current_number:
        num = int(''.join(current_number))
        tokens.append(num)
    else:
        return None  # should not happen for valid formula
    current = tokens[0]
    for i in range(1, len(tokens), 2):
        op = tokens[i]
        num = tokens[i+1]
        if op == '+':
            current += num
        else:
            current -= num
    return current

S = input().strip()
n = len(S)
max_val = -float('inf')

for i in range(n):
    rotated = S[i:] + S[:i]
    if not is_valid(rotated):
        continue
    val = evaluate(rotated)
    if val is not None and val > max_val:
        max_val = val

print(max_val)
0