結果

問題 No.1077 Noelちゃんと星々4
ユーザー lam6er
提出日時 2025-03-31 17:44:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 82,776 KB
実行使用メモリ 84,096 KB
最終ジャッジ日時 2025-03-31 17:45:51
合計ジャッジ時間 5,546 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
y = list(map(int, input().split()))

max_y_val = max(y) + n  # Maximum possible value after considering increments over N steps

# Initialize previous DP and cumulative minimum
prev_dp = [float('inf')] * (max_y_val + 1)
for current_y in range(max_y_val + 1):
    prev_dp[current_y] = abs(y[0] - current_y)

# Compute cumulative minimum for the first step
min_dp_prev = [float('inf')] * (max_y_val + 1)
current_min = float('inf')
for current_y in range(max_y_val + 1):
    if prev_dp[current_y] < current_min:
        current_min = prev_dp[current_y]
    min_dp_prev[current_y] = current_min

# Process each subsequent point
for i in range(1, n):
    curr_dp = [float('inf')] * (max_y_val + 1)
    for current_y in range(max_y_val + 1):
        if current_y > max_y_val:
            continue
        current_cost = min_dp_prev[current_y] + abs(y[i] - current_y)
        curr_dp[current_y] = current_cost

    # Update cumulative minimum for the current step
    min_dp_curr = [float('inf')] * (max_y_val + 1)
    current_min = float('inf')
    for current_y in range(max_y_val + 1):
        if curr_dp[current_y] < current_min:
            current_min = curr_dp[current_y]
        min_dp_curr[current_y] = current_min

    # Update previous values for next iteration
    prev_dp, min_dp_prev = curr_dp, min_dp_curr

# The minimum value from the last step's cumulative minimum array is the answer
print(min(min_dp_prev))
0