Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    def chmax(i, j, c, val):dp[i][j][c]=max(dp[i][j][c],val)

    dp = [[[-1] * 4 for _ in range(w+1)] for _ in range(h+1)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            if aa[i][j]:
                for c in range(2,-1,-1):
                    pre = dp[i][j][c]
                    if pre==-1:continue
                    chmax(i,j,c+1,dp[i][j][c]+aa[i][j])
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -1: continue
                chmax(i+1,j,0,pre)
                chmax(i,j+1,c,pre)

    ans = max(dp[h-1][w-1])
    print(ans)

h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v
solve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp0 = [0] * (w + 1)
    dp1 = [0] * (w + 1)
    dp2 = [0] * (w + 1)
    dp3 = [0] * (w + 1)
    for i in range(h):
        for j in range(w):
            v = vv[i * w + j]
            if v:
                dp3[j] = max(dp3[j], dp2[j] + v)
                dp2[j] = max(dp2[j], dp1[j] + v)
                dp1[j] = max(dp1[j], dp0[j] + v)
            dp3[j + 1] = max(dp3[j + 1], dp3[j])
            dp2[j + 1] = max(dp2[j + 1], dp2[j])
            dp1[j + 1] = max(dp1[j + 1], dp1[j])
            dp0[j] = max(dp1[j], dp2[j], dp3[j])
    print(dp0[-2])

h, w, k = MI()
vv = [0] * h * w
for _ in range(k):
    i, j, v = MI()
    i, j = i - 1, j - 1
    vv[i * w + j] = v
solve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp = [[[-1] * 4 for _ in range(w)] for _ in range(h)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -1: continue
                if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre)
                if j + 1 < w: dp[i][j + 1][c] = max(dp[i][j + 1][c], pre)
                if c<3 and aa[i][j]:
                    if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre + aa[i][j])
                    if j + 1 < w: dp[i][j + 1][c + 1] = max(dp[i][j + 1][c + 1], pre + aa[i][j])

    ans = 0
    for c in range(4):
        if c == 3: ans = max(ans, dp[h - 1][w - 1][c])
        else: ans = max(ans, dp[h - 1][w - 1][c] + aa[h - 1][w - 1])
    print(ans)

h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v
solve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp0 = [0] * (w + 1)
    dp1 = [0] * (w + 1)
    dp2 = [0] * (w + 1)
    dp3 = [0] * (w + 1)
    for i in range(h):
        for j in range(w):
            v = vv[i * w + j]
            if v:
                dp3[j] = max(dp3[j], dp2[j] + v)
                dp2[j] = max(dp2[j], dp1[j] + v)
                dp1[j] = max(dp1[j], dp0[j] + v)
            dp3[j + 1] = max(dp3[j + 1], dp3[j])
            dp2[j + 1] = max(dp2[j + 1], dp2[j])
            dp1[j + 1] = max(dp1[j + 1], dp1[j])
            dp0[j] = max(dp1[j], dp2[j], dp3[j])
    print(dp0[-2])

h, w, k = MI()
vv = [0] * h * w
for _ in range(k):
    i, j, v = MI()
    i, j = i - 1, j - 1
    vv[i * w + j] = v
solve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp = [[[-inf] * 4 for _ in range(w)] for _ in range(h)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -inf: continue
                if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre)
                if j + 1 < w: dp[i][j + 1][c] = max(dp[i][j + 1][c], pre)
                if aa[i][j]:
                    if c < 3 and i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre + aa[i][j])
                    if c < 3 and j + 1 < w: dp[i][j + 1][c + 1] = max(dp[i][j + 1][c + 1], pre + aa[i][j])

    ans = -inf
    for c in range(4):
        if c == 3: ans = max(ans, dp[h - 1][w - 1][c])
        else: ans = max(ans, dp[h - 1][w - 1][c] + aa[h - 1][w - 1])
    print(ans)

inf=10**16
h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v
solve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp0 = [0] * (w + 1)
    dp1 = [0] * (w + 1)
    dp2 = [0] * (w + 1)
    dp3 = [0] * (w + 1)
    for i in range(h):
        for j in range(w):
            v = vv[i * w + j]
            if v:
                dp3[j] = max(dp3[j], dp2[j] + v)
                dp2[j] = max(dp2[j], dp1[j] + v)
                dp1[j] = max(dp1[j], dp0[j] + v)
            dp3[j + 1] = max(dp3[j + 1], dp3[j])
            dp2[j + 1] = max(dp2[j + 1], dp2[j])
            dp1[j + 1] = max(dp1[j + 1], dp1[j])
            dp0[j] = max(dp1[j], dp2[j], dp3[j])
    print(dp0[-2])

h, w, k = MI()
vv = [0] * h * w
for _ in range(k):
    i, j, v = MI()
    i, j = i - 1, j - 1
    vv[i * w + j] = v
solve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

inf=10**16
h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v

dp=[[[-inf]*4 for _ in range(w)] for _ in range(h)]
dp[0][0][0]=0
for i in range(h):
    for j in range(w):
        for c in range(4):
            pre=dp[i][j][c]
            if pre==-inf:continue
            if i+1<h:dp[i+1][j][0]=max(dp[i+1][j][0],pre)
            if j+1<w:dp[i][j+1][c]=max(dp[i][j+1][c],pre)
            if aa[i][j]:
                if c<3 and i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre+aa[i][j])
                if c<3 and j + 1 < w: dp[i][j + 1][c+1] = max(dp[i][j + 1][c+1], pre+aa[i][j])

ans=-inf
for c in range(4):
    if c==3:ans=max(ans,dp[h-1][w-1][c])
    else:ans=max(ans,dp[h-1][w-1][c]+aa[h-1][w-1])
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp0 = [0] * (w + 1)
    dp1 = [0] * (w + 1)
    dp2 = [0] * (w + 1)
    dp3 = [0] * (w + 1)
    for i in range(h):
        for j in range(w):
            v = vv[i * w + j]
            if v:
                dp3[j] = max(dp3[j], dp2[j] + v)
                dp2[j] = max(dp2[j], dp1[j] + v)
                dp1[j] = max(dp1[j], dp0[j] + v)
            dp3[j + 1] = max(dp3[j + 1], dp3[j])
            dp2[j + 1] = max(dp2[j + 1], dp2[j])
            dp1[j + 1] = max(dp1[j + 1], dp1[j])
            dp0[j] = max(dp1[j], dp2[j], dp3[j])
    print(dp0[-2])

h, w, k = MI()
vv = [0] * h * w
for _ in range(k):
    i, j, v = MI()
    i, j = i - 1, j - 1
    vv[i * w + j] = v
solve()