Buggy python submission (verdict: Time Limit Exceeded):
import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

R, C, K = [int(x) for x in input().split()]
grid = [[0 for c in range(C)] for r in range(R)]
for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
    grid[r - 1][c - 1] = v


dp = [[[0 for i in range(3 + 1 + 1)] for c in range(C + 1)] for r in range(R + 1)]
dp2 = [[0 for c in range(C + 1)] for r in range(R + 1)]

inf = float("inf")
for r in range(R + 1):
    for c in range(C + 1):
        dp[r][c][-1] = -inf
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                dp[r][c - 1][numTakenThisRow],  # came from left
                dp2[r - 1][c],  # came from top, didn't take curr
                dp[r][c - 1][numTakenThisRow - 1]
                + grid[r][c],  # came from left, took curr
                dp2[r - 1][c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            dp[r][c][numTakenThisRow] = best
        dp2[r][c] = max(dp[r][c][i] for i in range(3 + 1))
print(dp2[R - 1][C - 1])


Fixed submission:
import io
import os
from collections import defaultdict

DEBUG = False
if not DEBUG:

    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    R, C, K = [int(x) for x in input().split()]
    grid = [[0 for c in range(C)] for r in range(R)]
    for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
        grid[r - 1][c - 1] = v
else:
    import random

    random.seed(0)
    R, C = 3000, 3000
    # 2 * 10**5 / 90 * 10**5 full
    grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ]
    print(sum(bool(grid[r][c]) for r in range(R) for c in range(C)))
    print("done gen")

    from timeit import default_timer as timer

    startTime = timer()


currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)]
prevRow = [0 for c in range(C + 1)]
nextPrevRow = [0 for c in range(C + 1)]

inf = float("inf")
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                currRow[c - 1][numTakenThisRow],  # came from left
                prevRow[c],  # came from top, didn't take curr
                currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0,  # came from left, took curr
                prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            currRow[c][numTakenThisRow] = best
        nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1))
    prevRow, nextPrevRow = nextPrevRow, prevRow
print(prevRow[C - 1])

if DEBUG:
    print(timer() - startTime)

Buggy python submission (verdict: Time Limit Exceeded):
import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

R, C, K = [int(x) for x in input().split()]
grid = [[0 for c in range(C)] for r in range(R)]
for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
    grid[r - 1][c - 1] = v


dp = [[[0 for i in range(3 + 1)] for c in range(C + 1)] for r in range(R + 1)]
dp2 = [[0 for c in range(C + 1)] for r in range(R + 1)]
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = dp[r][c - 1][numTakenThisRow]  # came from left
            if not numTakenThisRow:
                # came from top, didn't take curr
                best = max(best, dp2[r - 1][c])
            else:
                # came from left, took curr
                best = max(best, dp[r][c - 1][numTakenThisRow - 1] + grid[r][c])
                # came from top, took curr
                if numTakenThisRow == 1:
                    best = max(best, dp2[r - 1][c] + grid[r][c])
            dp[r][c][numTakenThisRow] = best
        dp2[r][c] = max(dp[r][c][i] for i in range(3 + 1))
print(dp2[R - 1][C - 1])


Fixed submission:
import io
import os
from collections import defaultdict

DEBUG = False
if not DEBUG:

    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    R, C, K = [int(x) for x in input().split()]
    grid = [[0 for c in range(C)] for r in range(R)]
    for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
        grid[r - 1][c - 1] = v
else:
    import random

    random.seed(0)
    R, C = 3000, 3000
    # 2 * 10**5 / 90 * 10**5 full
    grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ]
    print(sum(bool(grid[r][c]) for r in range(R) for c in range(C)))
    print("done gen")

    from timeit import default_timer as timer

    startTime = timer()


currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)]
prevRow = [0 for c in range(C + 1)]
nextPrevRow = [0 for c in range(C + 1)]

inf = float("inf")
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                currRow[c - 1][numTakenThisRow],  # came from left
                prevRow[c],  # came from top, didn't take curr
                currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0,  # came from left, took curr
                prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            currRow[c][numTakenThisRow] = best
        nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1))
    prevRow, nextPrevRow = nextPrevRow, prevRow
print(prevRow[C - 1])

if DEBUG:
    print(timer() - startTime)

Buggy python submission (verdict: Time Limit Exceeded):
import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

R, C, K = [int(x) for x in input().split()]
grid = [[0 for c in range(C)] for r in range(R)]
for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
    grid[r - 1][c - 1] = v


dp = [[[0 for i in range(3 + 1)] for c in range(C + 1)] for r in range(R + 1)]
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):

            best = dp[r][c - 1][numTakenThisRow]  # came from left
            if not numTakenThisRow:
                # came from top, didn't take curr
                for i in range(3 + 1):
                    best = max(best, dp[r - 1][c][i])
            else:
                # came from left, took curr
                best = max(best, dp[r][c - 1][numTakenThisRow - 1] + grid[r][c])
                # came from top, took curr
                if numTakenThisRow == 1:
                    for i in range(3 + 1):
                        best = max(best, dp[r - 1][c][i] + grid[r][c])
            dp[r][c][numTakenThisRow] = best
print(max(dp[R - 1][C - 1][i] for i in range(3 + 1)))


Fixed submission:
import io
import os
from collections import defaultdict

DEBUG = False
if not DEBUG:

    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    R, C, K = [int(x) for x in input().split()]
    grid = [[0 for c in range(C)] for r in range(R)]
    for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
        grid[r - 1][c - 1] = v
else:
    import random

    random.seed(0)
    R, C = 3000, 3000
    # 2 * 10**5 / 90 * 10**5 full
    grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ]
    print(sum(bool(grid[r][c]) for r in range(R) for c in range(C)))
    print("done gen")

    from timeit import default_timer as timer

    startTime = timer()


currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)]
prevRow = [0 for c in range(C + 1)]
nextPrevRow = [0 for c in range(C + 1)]

inf = float("inf")
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                currRow[c - 1][numTakenThisRow],  # came from left
                prevRow[c],  # came from top, didn't take curr
                currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0,  # came from left, took curr
                prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            currRow[c][numTakenThisRow] = best
        nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1))
    prevRow, nextPrevRow = nextPrevRow, prevRow
print(prevRow[C - 1])

if DEBUG:
    print(timer() - startTime)

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

sys.setrecursionlimit(10 ** 6)


R, C, K = [int(x) for x in input().split()]
grid = [[0 for c in range(C)] for r in range(R)]
for r, c, v in [[int(x) for x in input().split()] for i in range(K)]:
    grid[r - 1][c - 1] = v


'''
    def best3(r, c1, c2):  # c1 inclusive, c2 exclusive
        return sum(sorted(grid[r][c1:c2], reverse=True)[:3])

    @lru_cache(maxsize=None)
    def dp(r, c):  # inclusive
        if r < 0 or c < 0:
            return 0
        best = 0
        for c1 in range(c + 1):
            best = max(best, dp(r - 1, c1) + best3(r, c1, c + 1))
        return best

    print(dp(R - 1, C - 1))

'''
inf = float("inf")

@lru_cache(maxsize=None)
def dp(r, c, numTakenThisRow):
    if numTakenThisRow < 0:
        return -inf
    if r < 0 or c < 0:
        return 0

    best = max(
        dp(r, c - 1, numTakenThisRow),
        dp(r, c - 1, numTakenThisRow - 1) + grid[r][c],
    )
    if numTakenThisRow == 0:
        best = max(best, max(dp(r - 1, c, i) for i in range(3 + 1)),)
    if numTakenThisRow == 1:
        best = max(best, max(dp(r - 1, c, i) + grid[r][c] for i in range(3 + 1)),)

    return best

print(max(dp(R - 1, C - 1, i) for i in range(3 + 1)))


Fixed submission:
import io
import os
from collections import defaultdict

DEBUG = False
if not DEBUG:

    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    R, C, K = [int(x) for x in input().split()]
    grid = [[0 for c in range(C)] for r in range(R)]
    for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
        grid[r - 1][c - 1] = v
else:
    import random

    random.seed(0)
    R, C = 3000, 3000
    # 2 * 10**5 / 90 * 10**5 full
    grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ]
    print(sum(bool(grid[r][c]) for r in range(R) for c in range(C)))
    print("done gen")

    from timeit import default_timer as timer

    startTime = timer()


currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)]
prevRow = [0 for c in range(C + 1)]
nextPrevRow = [0 for c in range(C + 1)]

inf = float("inf")
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                currRow[c - 1][numTakenThisRow],  # came from left
                prevRow[c],  # came from top, didn't take curr
                currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0,  # came from left, took curr
                prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            currRow[c][numTakenThisRow] = best
        nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1))
    prevRow, nextPrevRow = nextPrevRow, prevRow
print(prevRow[C - 1])

if DEBUG:
    print(timer() - startTime)

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

# This TLEs, just checking correctness

R, C, K = [int(x) for x in input().split()]
grid = [[0 for c in range(C)] for r in range(R)]
for r, c, v in [[int(x) for x in input().split()] for i in range(K)]:
    grid[r - 1][c - 1] = v

# for row in grid:
#    print(row)


def best3(r, c1, c2):  # c1 inclusive, c2 exclusive
    return sum(sorted(grid[r][c1:c2], reverse=True)[:3])


@lru_cache(maxsize=None)
def dp(r, c):  # inclusive
    if r < 0 or c < 0:
        return 0
    best = 0
    for c1 in range(c + 1):
        best = max(best, dp(r - 1, c1) + best3(r, c1, c + 1))
    return best


print(dp(R - 1, C - 1))


Fixed submission:
import io
import os
from collections import defaultdict

DEBUG = False
if not DEBUG:

    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    R, C, K = [int(x) for x in input().split()]
    grid = [[0 for c in range(C)] for r in range(R)]
    for r, c, v in ((int(x) for x in input().split()) for i in range(K)):
        grid[r - 1][c - 1] = v
else:
    import random

    random.seed(0)
    R, C = 3000, 3000
    # 2 * 10**5 / 90 * 10**5 full
    grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ]
    print(sum(bool(grid[r][c]) for r in range(R) for c in range(C)))
    print("done gen")

    from timeit import default_timer as timer

    startTime = timer()


currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)]
prevRow = [0 for c in range(C + 1)]
nextPrevRow = [0 for c in range(C + 1)]

inf = float("inf")
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = max(
                currRow[c - 1][numTakenThisRow],  # came from left
                prevRow[c],  # came from top, didn't take curr
                currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0,  # came from left, took curr
                prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0,  # came from top, took curr
            )
            currRow[c][numTakenThisRow] = best
        nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1))
    prevRow, nextPrevRow = nextPrevRow, prevRow
print(prevRow[C - 1])

if DEBUG:
    print(timer() - startTime)