結果

問題 No.2927 Reverse Polish Equation
ユーザー 👑 loop0919
提出日時 2024-07-17 00:19:13
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 799 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 128,984 KB
最終ジャッジ日時 2024-10-16 00:20:31
合計ジャッジ時間 13,134 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

assert 1 <= Q <= 2 * 10**5
assert 1 <= Y <= 10**14
assert len(S) == Q

def rev_poland(x):
    A = []
    for s in S:
        if s in ('+', 'min', 'max'):
            a = A.pop()
            b = A.pop()
            
            if s == '+':
                A.append(a + b)
            if s == 'min':
                A.append(min(a, b))
            if s == 'max':
                A.append(max(a, b))
        
        else:
            if s == 'X':
                A.append(x)
            else:
                A.append(int(s))
    
    return A[0]

ok, ng = 10**14, -1

while ok - ng > 1:
    mid = (ok + ng) // 2
    if rev_poland(mid) >= Y:
        ok = mid
    else:
        ng = mid

if rev_poland(ok) == Y:
    print(ok)
else:
    print(-1)

0