# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal


from numba import jit

@jit
def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
    return dp[len(Rs), len(Cs)].max()

def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    ans= run_dp(MAP, dp, Rs, Cs)

    print(ans)

if __name__ == "__main__":
    run() import sys sys sysread = sys.stdin.buffer.readline sysread sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 import bisect bisect import numpy as np numpy from numba import jit def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
    return dp[len(Rs), len(Cs)].max() run_dp for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
     i range(1, len(Rs)+1) range 1 len(Rs)+1 len(Rs) len Rs 1 for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
     j range(1, len(Cs)+1) range 1 len(Cs)+1 len(Cs) len Cs 1 for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
     k range(4) range 4 dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k]) dp[i,j,0] dp i max(dp[i,j,0], dp[i-1,j,k]) max dp[i,j,0] dp i dp[i-1,j,k] dp i-1 i 1 if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                 MAP[i,j] MAP i dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j]) dp[i,j,1] dp i max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j]) max dp[i,j,1] dp i dp[i-1,j,k]+MAP[i,j] dp[i-1,j,k] dp i-1 i 1 MAP[i,j] MAP i dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k]) dp[i,j,k] dp i max(dp[i,j,k], dp[i,j-1,k]) max dp[i,j,k] dp i dp[i,j-1,k] dp i if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
     MAP[i][j] and k < 3 MAP[i][j] [i] MAP i j k < 3 k 3 dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j]) dp[i,j,k+1] dp i max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j]) max dp[i,j,k+1] dp i dp[i,j-1,k] + MAP[i,j] dp[i,j-1,k] dp i MAP[i,j] MAP i return dp[len(Rs), len(Cs)].max() dp[len(Rs), len(Cs)].max() [len(Rs), len(Cs)].max [len(Rs), len(Cs)] dp len(Rs) len Rs max MAP MAP dp dp Rs Rs Cs Cs @jit run_dp jit run_dp def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    ans= run_dp(MAP, dp, Rs, Cs)

    print(ans) run R,C,K = map(int, sysread().split()) R map(int, sysread().split()) map int sysread().split() ().split () sysread split C K Rs = set() Rs set() set Cs = set() Cs set() set vals = [] vals [] for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
     i range(K) range K r, c, v = map(int, sysread().split()) r map(int, sysread().split()) map int sysread().split() ().split () sysread split c v vals.append((r, c, v)) vals.append vals append (r, c, v) r c v Rs.add(r) Rs.add Rs add r Cs.add(c) Cs.add Cs add c Rs = np.array(sorted(list(Rs))) Rs np.array(sorted(list(Rs))) np.array np array sorted(list(Rs)) sorted list(Rs) list Rs Cs = np.array(sorted(list(Cs))) Cs np.array(sorted(list(Cs))) np.array np array sorted(list(Cs)) sorted list(Cs) list Cs r:i+1 for i, r in enumerate(Rs) for i, r in enumerate(Rs) i r enumerate(Rs) enumerate Rs for i, r in enumerate(Rs) r i+1 i 1 r_idx = {r:i+1 for i, r in enumerate(Rs)} r_idx {r:i+1 for i, r in enumerate(Rs)} c:i+1 for i, c in enumerate(Cs) for i, c in enumerate(Cs) i c enumerate(Cs) enumerate Cs for i, c in enumerate(Cs) c i+1 i 1 c_idx = {c:i+1 for i, c in enumerate(Cs)} c_idx {c:i+1 for i, c in enumerate(Cs)} MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64) MAP np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64) np.zeros np zeros (len(Rs)+1, len(Cs)+1) len(Rs)+1 len(Rs) len Rs 1 len(Cs)+1 len(Cs) len Cs 1 dtype= np.int64 dtype np.int64 np int64 for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
     r c v vals rn = r_idx[r] rn r_idx[r] r_idx r cn = c_idx[c] cn c_idx[c] c_idx c MAP[rn][cn] = v MAP[rn][cn] [rn] MAP rn cn v dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64) dp np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64) np.zeros np zeros (len(Rs)+1, len(Cs)+1, 4) len(Rs)+1 len(Rs) len Rs 1 len(Cs)+1 len(Cs) len Cs 1 4 dtype= np.int64 dtype np.int64 np int64 run_dp(MAP, dp, Rs, Cs) run_dp MAP dp Rs Cs ans= run_dp(MAP, dp, Rs, Cs) ans run_dp(MAP, dp, Rs, Cs) run_dp MAP dp Rs Cs print(ans) print ans if __name__ == "__main__":
    run() __name__ == "__main__" __name__ "__main__" run() run def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    ans= run_dp(MAP, dp, Rs, Cs)

    print(ans) def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    ans= run_dp(MAP, dp, Rs, Cs)

    print(ans) run read = sys.stdin.buffer.read sys.stdin.buffer.read read def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
    return dp[len(Rs), len(Cs)].max() def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
    return dp[len(Rs), len(Cs)].max() run_dp sysread = sys.stdin.buffer.readline sys.stdin.buffer.readline sysread
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal


from numba import jit

@jit
def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i,j,0] = max(dp[i,j,0], dp[i-1,j,k])
                if MAP[i,j]:
                    dp[i,j,1] = max(dp[i,j,1], dp[i-1,j,k]+MAP[i,j])

                dp[i,j,k] = max(dp[i,j,k], dp[i,j-1,k])
                if MAP[i][j] and k < 3:
                    dp[i,j,k+1] = max(dp[i,j,k+1], dp[i,j-1,k] + MAP[i,j])
    return dp[len(Rs), len(Cs)].max()

def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int64)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int64)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    ans= run_dp(MAP, dp, Rs, Cs)

    print(ans)

if __name__ == "__main__":
    run()