Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

V = [[] for i in range(R)]
for i in range(K):
    r, c, v = list(map(int, input().split()))
    V[r-1].append([c, v])


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])


Fixed submission:
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

V = [[] for i in range(R)]
for i in range(K):
    r, c, v = list(map(int, input().split()))
    V[r-1].append([c, v])


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

rck = {}
for i in range(K):
    tmp = list(map(int, input().split()))
    rck[(tmp[0], tmp[1])] = tmp[2]


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r_i in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    dp_new = [0]*(C+1)
    for c_i in range(1, C+1):
        tmp0 = max(dp[c_i], tmp0)
        tmp1 = max(dp[c_i], tmp1)
        tmp2 = max(dp[c_i], tmp2)
        tmp3 = max(dp[c_i], tmp3)

        index = (r_i, c_i)
        if index in rck:
            tmp3 = max(tmp3, tmp2+rck[index])
            tmp2 = max(tmp2, tmp1+rck[index])
            tmp1 = max(tmp1, tmp0+rck[index])
        dp_new[c_i] = max(tmp0, tmp1, tmp2, tmp3)
    # print(dp_new)
    dp = dp_new
print(dp[-1])


Fixed submission:
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

V = [[] for i in range(R)]
for i in range(K):
    r, c, v = list(map(int, input().split()))
    V[r-1].append([c, v])


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

rck = {}
for i in range(K):
    tmp = list(map(int, input().split()))
    rck[(tmp[0], tmp[1])] = tmp[2]


dp = [[[0 for i in range(4)] for j in range(R+1)] for i in range(C+1)]

# print(rck)
for r_i in range(1, R+1):
    for c_i in range(1, C+1):

        tmp0 = max(dp[c_i-1][r_i][0], dp[c_i][r_i-1][3])
        tmp1 = max(dp[c_i-1][r_i][1], dp[c_i][r_i-1][3])
        tmp2 = max(dp[c_i-1][r_i][2], dp[c_i][r_i-1][3])
        tmp3 = max(dp[c_i-1][r_i][3], dp[c_i][r_i-1][3])
        # print(c_i, r_i)
        # print(tmp0, tmp1, tmp2, tmp3)

        index = (r_i, c_i)

        if index in rck:
            dp[c_i][r_i][3] = max(tmp3, tmp2+rck[index])
            dp[c_i][r_i][2] = max(tmp2, tmp1+rck[index])
            dp[c_i][r_i][1] = max(tmp1, tmp0+rck[index])
            dp[c_i][r_i][0] = tmp0
        else:
            dp[c_i][r_i][3] = tmp3
            dp[c_i][r_i][2] = tmp2
            dp[c_i][r_i][1] = tmp1
            dp[c_i][r_i][0] = tmp0

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


Fixed submission:
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

V = [[] for i in range(R)]
for i in range(K):
    r, c, v = list(map(int, input().split()))
    V[r-1].append([c, v])


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

rck = {}
for i in range(K):
    tmp = list(map(int, input().split()))
    rck[(tmp[0], tmp[1])] = tmp[2]


dp = [[[0 for i in range(4)] for j in range(R+1)] for i in range(C+1)]

# print(rck)
for r_i in range(1, R+1):
    for c_i in range(1, C+1):

        tmp0 = max(dp[c_i-1][r_i][0], dp[c_i][r_i-1][3])
        tmp1 = max(dp[c_i-1][r_i][1], dp[c_i][r_i-1][3])
        tmp2 = max(dp[c_i-1][r_i][2], dp[c_i][r_i-1][3])
        tmp3 = max(dp[c_i-1][r_i][3], dp[c_i][r_i-1][3])
        # print(c_i, r_i)
        # print(tmp0, tmp1, tmp2, tmp3)

        index = (r_i, c_i)

        if index in rck:
            dp[c_i][r_i][3] = max(tmp3, tmp2+rck[index])
            dp[c_i][r_i][2] = max(tmp2, tmp1+rck[index])
            dp[c_i][r_i][1] = max(tmp1, tmp0+rck[index])
            dp[c_i][r_i][0] = tmp0
        else:
            dp[c_i][r_i][3] = tmp3
            dp[c_i][r_i][2] = tmp2
            dp[c_i][r_i][1] = tmp1
            dp[c_i][r_i][0] = tmp0

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


Fixed submission:
#!/usr/bin/env python3

R, C, K = list(map(int, input().split()))

V = [[] for i in range(R)]
for i in range(K):
    r, c, v = list(map(int, input().split()))
    V[r-1].append([c, v])


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])