結果

問題 No.595 登山
ユーザー gew1fw
提出日時 2025-06-12 19:13:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,036 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 82,588 KB
実行使用メモリ 112,676 KB
最終ジャッジ日時 2025-06-12 19:13:48
合計ジャッジ時間 4,569 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    P = int(input[idx])
    idx += 1
    H = list(map(int, input[idx:idx+N]))
    idx += N
    
    if P == 0:
        print(0)
        return
    
    # Compute sum1: sum1[i] is the cost to move from 1 to (i+1)th location (0-based)
    sum1 = [0] * N
    for i in range(1, N):
        sum1[i] = sum1[i-1] + max(0, H[i] - H[i-1])
    
    # Compute sum2: sum2[i] is the cost to move from (i+1)th location to N-1th location (0-based)
    sum2 = [0] * N
    for i in range(N-2, -1, -1):
        sum2[i] = sum2[i+1] + max(0, H[i] - H[i+1])
    
    # Compute the minimal cost when using exactly one teleport
    min_cost = float('inf')
    for k in range(N):
        current = sum1[k] + sum2[k] + P
        if current < min_cost:
            min_cost = current
    
    # Compare with the cost of not teleporting
    no_teleport_cost = sum1[-1]
    print(min(min_cost, no_teleport_cost))

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