Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    my_input = sys.stdin.readline
    H, W, K = map(int, my_input().split())
    V = [[0] * W for _ in range(H)]
    for i in range(K):
        h, w, v = map(int, my_input().split())
        h, w = h - 1, w - 1
        V[h][w] = v

    # dp[h][w][x] := h行w列にいて、h行からx個アイテムを拾っている時の価値の総和の最大値
    dp = [[[0] * 4 for j in range(W + 1)] for i in range(H + 1)]
    for h in range(1, H + 1):
        for w in range(1, W + 1):
            for k in range(1, 4):
                dp[h][w][k] = max(
                    dp[h][w][k],
                    dp[h - 1][w][-1],
                    dp[h - 1][w][-1] + V[h - 1][w - 1],
                    dp[h][w - 1][k],
                    dp[h][w - 1][k - 1] + V[h - 1][w - 1])

    print(max(dp[-1][-1]))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    import sys
    my_input = sys.stdin.readline
    H, W, K = map(int, my_input().split())
    V = [[0] * W for _ in range(H)]
    for i in range(K):
        h, w, v = map(int, my_input().split())
        h, w = h - 1, w - 1
        V[h][w] = v

    # dp1 ~ 4[h][w][x] := h行w列にいて、h行からn個アイテムを拾っている時の価値の総和の最大値
    dp0 = [[0] * (W + 1) for _ in range(H + 1)]
    dp1 = [[0] * (W + 1) for _ in range(H + 1)]
    dp2 = [[0] * (W + 1) for _ in range(H + 1)]
    dp3 = [[0] * (W + 1) for _ in range(H + 1)]

    for h in range(1, H + 1):
        for w in range(1, W + 1):
            dp1[h][w] = max(
                dp1[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp1[h][w - 1],
                dp0[h][w - 1] + V[h - 1][w - 1]
            )

            dp2[h][w] = max(
                dp2[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp2[h][w - 1],
                dp1[h][w - 1] + V[h - 1][w - 1]
            )

            dp3[h][w] = max(
                dp3[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp3[h][w - 1],
                dp2[h][w - 1] + V[h - 1][w - 1]
            )

    print(dp3[-1][-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
H, W, K = map(int, input().split())
V = [[0] * W for _ in range(H)]
for i in range(K):
    h, w, v = map(int, input().split())
    h, w = h - 1, w - 1
    V[h][w] = v

# dp[h][w][x] := h行w列にいて、h行からx個アイテムを拾っている時の価値の総和の最大値
dp = [[[0] * 4 for j in range(W + 1)] for i in range(H + 1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        for k in range(1, 4):
            dp[h][w][k] = max(
                dp[h][w][k],
                dp[h - 1][w][-1],
                dp[h - 1][w][-1] + V[h - 1][w - 1],
                dp[h][w - 1][k],
                dp[h][w - 1][k - 1] + V[h - 1][w - 1])

print(max(dp[-1][-1]))


Fixed submission:
def main():
    import sys
    my_input = sys.stdin.readline
    H, W, K = map(int, my_input().split())
    V = [[0] * W for _ in range(H)]
    for i in range(K):
        h, w, v = map(int, my_input().split())
        h, w = h - 1, w - 1
        V[h][w] = v

    # dp1 ~ 4[h][w][x] := h行w列にいて、h行からn個アイテムを拾っている時の価値の総和の最大値
    dp0 = [[0] * (W + 1) for _ in range(H + 1)]
    dp1 = [[0] * (W + 1) for _ in range(H + 1)]
    dp2 = [[0] * (W + 1) for _ in range(H + 1)]
    dp3 = [[0] * (W + 1) for _ in range(H + 1)]

    for h in range(1, H + 1):
        for w in range(1, W + 1):
            dp1[h][w] = max(
                dp1[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp1[h][w - 1],
                dp0[h][w - 1] + V[h - 1][w - 1]
            )

            dp2[h][w] = max(
                dp2[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp2[h][w - 1],
                dp1[h][w - 1] + V[h - 1][w - 1]
            )

            dp3[h][w] = max(
                dp3[h][w],
                dp3[h - 1][w] + V[h - 1][w - 1],
                dp3[h][w - 1],
                dp2[h][w - 1] + V[h - 1][w - 1]
            )

    print(dp3[-1][-1])


if __name__ == "__main__":
    main()