Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created January 17, 2026 07:55
Show Gist options
  • Select an option

  • Save maehrm/4f81c1e1591eaa841046ea0a3b9526f6 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/4f81c1e1591eaa841046ea0a3b9526f6 to your computer and use it in GitHub Desktop.
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
P = list(map(int, input().split()))
dp = [[float("inf")] * W for _ in range(H)]
dp[H - 1][W - 1] = 0
for i in range(H - 1, -1, -1):
for j in range(W - 1, -1, -1):
if i + 1 < H:
dp[i][j] = min(dp[i][j], dp[i + 1][j])
if j + 1 < W:
dp[i][j] = min(dp[i][j], dp[i][j + 1])
dp[i][j] += P[i + j] - A[i][j]
dp[i][j] = max(dp[i][j], 0)
print(dp[0][0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment