結果

問題 No.2927 Reverse Polish Equation
ユーザー 寝癖
提出日時 2024-10-12 16:18:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 938 ms / 2,000 ms
コード長 748 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 128,408 KB
最終ジャッジ日時 2024-10-16 00:25:47
合計ジャッジ時間 16,749 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

Q, Y = map(int, input().split())
S = input().split()

def f(x):
    A = []
    for s in S:
        if s.isdigit():
            A.append(int(s))
        elif s == 'X':
            A.append(x)
        elif s == '+':
            A[-2] += A[-1]
            A.pop()
        elif s == 'min':
            A[-2] = min(A[-2], A[-1])
            A.pop()
        else:
            A[-2] = max(A[-2], A[-1])
            A.pop()
    return A[0]

inf = 10**20
if f(inf) < Y:
    print(-1)
    exit()

if f(0) >= Y:
    if f(0) == Y:
        print(0)
    else:
        print(-1)
    exit()

ng, ok = 0, inf
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    if f(mid) >= Y:
        ok = mid
    else:
        ng = mid

if f(ok) == Y:
    print(ok)
else:
    print(-1)
0