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) import io io import os os from collections import defaultdict DEBUG = False 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() not DEBUG DEBUG input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline input io.BytesIO(os.read(0, os.fstat(0).st_size)).readline io.BytesIO(os.read(0, os.fstat(0).st_size)) io.BytesIO io BytesIO os.read(0, os.fstat(0).st_size) os.read os read 0 os.fstat(0).st_size os.fstat(0) os.fstat os fstat 0 st_size readline int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x R, C, K = [int(x) for x in input().split()] R [int(x) for x in input().split()] C K [0 for c in range(C)] for r in range(R) for r in range(R) r range(R) range R for r in range(R) [0 for c in range(C)] grid = [[0 for c in range(C)] for r in range(R)] 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 r c v ((int(x) for x in input().split()) for i in range(K)) grid[r - 1][c - 1] = v grid[r - 1][c - 1] [r - 1] grid r - 1 r 1 c - 1 c 1 v import random random random.seed(0) random.seed random seed 0 R, C = 3000, 3000 R 3000 C 3000 [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R) for r in range(R) r range(R) range R for r in range(R) [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)] grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ] 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 sum(bool(grid[r][c]) for r in range(R) for c in range(C)) sum bool(grid[r][c]) bool grid[r][c] [r] grid r c print("done gen") print "done gen" from timeit import default_timer as timer startTime = timer() startTime timer() timer [0 for i in range(3 + 1)] for c in range(C + 1) for c in range(C + 1) c range(C + 1) range C + 1 C 1 for c in range(C + 1) [0 for i in range(3 + 1)] currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)] currRow [[0 for i in range(3 + 1)] for c in range(C + 1)] 0 for c in range(C + 1) for c in range(C + 1) c range(C + 1) range C + 1 C 1 for c in range(C + 1) 0 prevRow = [0 for c in range(C + 1)] prevRow [0 for c in range(C + 1)] 0 for c in range(C + 1) for c in range(C + 1) c range(C + 1) range C + 1 C 1 for c in range(C + 1) 0 nextPrevRow = [0 for c in range(C + 1)] nextPrevRow [0 for c in range(C + 1)] inf = float("inf") inf float("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 r range(R) 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))
     c range(C) 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
         numTakenThisRow range(3 + 1) range 3 + 1 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
            ) 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
            ) max currRow[c - 1][numTakenThisRow] [c - 1] currRow c - 1 c 1 numTakenThisRow prevRow[c] prevRow c currRow[c - 1][numTakenThisRow - 1] + grid[r][c]
                if numTakenThisRow
                else 0 numTakenThisRow currRow[c - 1][numTakenThisRow - 1] + grid[r][c] currRow[c - 1][numTakenThisRow - 1] [c - 1] currRow c - 1 c 1 numTakenThisRow - 1 numTakenThisRow 1 grid[r][c] [r] grid r c 0 prevRow[c] + grid[r][c]
                if numTakenThisRow == 1
                else 0 numTakenThisRow == 1 numTakenThisRow 1 prevRow[c] + grid[r][c] prevRow[c] prevRow c grid[r][c] [r] grid r c 0 currRow[c][numTakenThisRow] = best currRow[c][numTakenThisRow] [c] currRow c numTakenThisRow best nextPrevRow[c] = max(currRow[c][i] for i in range(3 + 1)) nextPrevRow[c] nextPrevRow c max(currRow[c][i] for i in range(3 + 1)) max currRow[c][i] [c] currRow c i prevRow, nextPrevRow = nextPrevRow, prevRow prevRow nextPrevRow nextPrevRow prevRow print(prevRow[C - 1]) print prevRow[C - 1] prevRow C - 1 C 1 if DEBUG:
    print(timer() - startTime) DEBUG print(timer() - startTime) print timer() - startTime timer() timer startTime R, C, K = [int(x) for x in input().split()] [int(x) for x in input().split()] R currRow = [[0 for i in range(3 + 1)] for c in range(C + 1)] [[0 for i in range(3 + 1)] for c in range(C + 1)] currRow nextPrevRow = [0 for c in range(C + 1)] [0 for c in range(C + 1)] nextPrevRow input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline io.BytesIO(os.read(0, os.fstat(0).st_size)).readline input C, K = [int(x) for x in input().split()] [int(x) for x in input().split()] C prevRow = [0 for c in range(C + 1)] [0 for c in range(C + 1)] prevRow DEBUG = False False DEBUG 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
            ) 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
            ) best prevRow, nextPrevRow = nextPrevRow, prevRow nextPrevRow prevRow nextPrevRow = nextPrevRow, prevRow prevRow nextPrevRow grid = [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ] [
        [int(random.randint(1, 90) <= 2) * random.randint(0, 10 ** 9) for c in range(C)]
        for r in range(R)
    ] grid grid = [[0 for c in range(C)] for r in range(R)] [[0 for c in range(C)] for r in range(R)] grid inf = float("inf") float("inf") inf R, C = 3000, 3000 3000 R C = 3000, 3000 3000 C K = [int(x) for x in input().split()] [int(x) for x in input().split()] K startTime = timer() timer() startTime
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)
