Buggy python submission (verdict: Time Limit Exceeded):
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
r_list = [False for r in range(R+1)]
c_list = [False for c in range(C+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
	r_list[r] = True
	c_list[c] = True


r_map = {}
cnt = 0
for i in range(R+1):
	if r_list[i]:	
		cnt += 1
		r_map[i] = cnt
r_len = cnt

c_map = {}
cnt = 0
for i in range(C+1):
	if c_list[i]:
		cnt += 1
		c_map[i] = cnt
c_len = cnt

new_v = [[0 for i in range(c_len+1)] for j in range(r_len+1)]
for r in range(R+1):
	for c in range(C+1):
		if V[r][c] > 0:
			new_v[r_map[r]][c_map[c]] = V[r][c]

V = new_v
R = r_len
C = c_len

dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		if v > 0:
			dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
			dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
			dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
		else:
			dp[r][c][1] = dp[r][c-1][1]
			dp[r][c][2] = dp[r][c-1][2]
			dp[r][c][3] = dp[r][c-1][3]
print(max(dp[-1][-1]))

Fixed submission:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v

dp = [[0 for i in range(4)] for j in range(C+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[c])
		dp[c][0] = max(under_val, dp[c-1][0])
		if v > 0:
			dp[c][1] = max(under_val + v, dp[c-1][0] + v, dp[c-1][1])
			dp[c][2] = max(dp[c-1][1] + v, dp[c-1][2])
			dp[c][3] = max(dp[c-1][2] + v, dp[c-1][3])
		else:
			dp[c][1] = dp[c-1][1]
			dp[c][2] = dp[c-1][2]
			dp[c][3] = dp[c-1][3]
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
r_list = [False for r in range(R+1)]
c_list = [False for c in range(C+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
	r_list[r] = True
	c_list[c] = True


r_map = {}
cnt = 0
for i in range(R+1):
	if r_list[i]:	
		cnt += 1
		r_map[i] = cnt
r_len = cnt

c_map = {}
cnt = 0
for i in range(C+1):
	if c_list[i]:
		cnt += 1
		c_map[i] = cnt
c_len = cnt

new_v = [[0 for i in range(c_len+1)] for j in range(r_len+1)]
for r in range(R+1):
	for c in range(C+1):
		if V[r][c] > 0:
			new_v[r_map[r]][c_map[c]] = V[r][c]

V = new_v
R = r_len
C = c_len

dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		if v > 0:
			dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
			dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
			dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
		else:
			dp[r][c][1] = dp[r][c-1][1]
			dp[r][c][2] = dp[r][c-1][2]
			dp[r][c][3] = dp[r][c-1][3]
print(max(dp[-1][-1]))

Fixed submission:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v

dp = [[0 for i in range(4)] for j in range(C+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[c])
		dp[c][0] = max(under_val, dp[c-1][0])
		if v > 0:
			dp[c][1] = max(under_val + v, dp[c-1][0] + v, dp[c-1][1])
			dp[c][2] = max(dp[c-1][1] + v, dp[c-1][2])
			dp[c][3] = max(dp[c-1][2] + v, dp[c-1][3])
		else:
			dp[c][1] = dp[c-1][1]
			dp[c][2] = dp[c-1][2]
			dp[c][3] = dp[c-1][3]
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		if v > 0:
			dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
			dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
			dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
		else:
			dp[r][c][1] = dp[r][c-1][1]
			dp[r][c][2] = dp[r][c-1][2]
			dp[r][c][3] = dp[r][c-1][3]
print(max(dp[-1][-1]))

Fixed submission:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v

dp = [[0 for i in range(4)] for j in range(C+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[c])
		dp[c][0] = max(under_val, dp[c-1][0])
		if v > 0:
			dp[c][1] = max(under_val + v, dp[c-1][0] + v, dp[c-1][1])
			dp[c][2] = max(dp[c-1][1] + v, dp[c-1][2])
			dp[c][3] = max(dp[c-1][2] + v, dp[c-1][3])
		else:
			dp[c][1] = dp[c-1][1]
			dp[c][2] = dp[c-1][2]
			dp[c][3] = dp[c-1][3]
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
		dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
		dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
print(max(dp[-1][-1]))

Fixed submission:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v

dp = [[0 for i in range(4)] for j in range(C+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[c])
		dp[c][0] = max(under_val, dp[c-1][0])
		if v > 0:
			dp[c][1] = max(under_val + v, dp[c-1][0] + v, dp[c-1][1])
			dp[c][2] = max(dp[c-1][1] + v, dp[c-1][2])
			dp[c][3] = max(dp[c-1][2] + v, dp[c-1][3])
		else:
			dp[c][1] = dp[c-1][1]
			dp[c][2] = dp[c-1][2]
			dp[c][3] = dp[c-1][3]
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
		dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
		dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
print(max(dp[-1][-1]))

Fixed submission:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v

dp = [[0 for i in range(4)] for j in range(C+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[c])
		dp[c][0] = max(under_val, dp[c-1][0])
		if v > 0:
			dp[c][1] = max(under_val + v, dp[c-1][0] + v, dp[c-1][1])
			dp[c][2] = max(dp[c-1][1] + v, dp[c-1][2])
			dp[c][3] = max(dp[c-1][2] + v, dp[c-1][3])
		else:
			dp[c][1] = dp[c-1][1]
			dp[c][2] = dp[c-1][2]
			dp[c][3] = dp[c-1][3]
print(max(dp[-1]))