結果

問題 No.505 カードの数式2
ユーザー lam6er
提出日時 2025-03-20 21:12:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,288 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 54,228 KB
最終ジャッジ日時 2025-03-20 21:13:56
合計ジャッジ時間 2,600 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

def compute_op(a, b, op):
    if op == '+':
        return a + b
    elif op == '-':
        return a - b
    elif op == '*':
        return a * b
    elif op == '/':
        if b == 0:
            return None
        # Compute division with truncation towards zero
        q = a // b
        # If the signs are different and there's a remainder, adjust the quotient
        if (a ^ b) < 0 and a % b != 0:
            q += 1
        return q
    else:
        return None

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    a = list(map(int, input[1:N+1]))
    
    if N == 0:
        print(0)
        return
    
    current_max = a[0]
    current_min = a[0]
    
    for i in range(N-1):
        next_num = a[i+1]
        candidates = []
        for op in ['+', '-', '*', '/']:
            if op == '/' and next_num == 0:
                continue
            for val in [current_max, current_min]:
                res = compute_op(val, next_num, op)
                if res is not None:
                    candidates.append(res)
        if not candidates:
            print(0)
            return
        
        current_max = max(candidates)
        current_min = min(candidates)
    
    print(current_max)

if __name__ == '__main__':
    main()
0