ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
R,C,K = ma()
V = [[0 for i in range(R+1)] for j in range(C+1)]
for i in range(K):
    r,c,v = ma()
    V[c][r] = v
dp_prev = [[0]*4 for i in range(C+1)]
dp_nex = [[0]*4 for i in range(C+1)] #dp[h][k] h列目でmax kこまで選んだ時の最大値
for r in range(1,R+1):
    for c in range(1,C+1):
        v = V[c][r]
        for k in range(4):
            if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
    dp_prev,dp_nex = dp_nex,dp_prev
    #print(dp_prev)


print(dp_prev[C][3]) ma = lambda :map(int,input().split()) ma lambda :map(int,input().split()) map(int,input().split()) map int input().split() ().split () input split lma = lambda :list(map(int,input().split())) lma lambda :list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split tma = lambda :tuple(map(int,input().split())) tma lambda :tuple(map(int,input().split())) tuple(map(int,input().split())) tuple map(int,input().split()) map int input().split() ().split () input split ni = lambda:int(input()) ni lambda:int(input()) int(input()) int input() input yn = lambda fl:print("Yes") if fl else print("No") yn lambda fl:print("Yes") if fl else print("No") print("Yes") if fl else print("No") fl print("Yes") print "Yes" print("No") print "No" fl import collections collections import math math import itertools itertools import heapq as hq heapq R,C,K = ma() R ma() ma C K [0 for i in range(R+1)] for j in range(C+1) for j in range(C+1) j range(C+1) range C+1 C 1 for j in range(C+1) [0 for i in range(R+1)] V = [[0 for i in range(R+1)] for j in range(C+1)] V [[0 for i in range(R+1)] for j in range(C+1)] for i in range(K):
    r,c,v = ma()
    V[c][r] = v i range(K) range K r,c,v = ma() r ma() ma c v V[c][r] = v V[c][r] [c] V c r v [0]*4 for i in range(C+1) for i in range(C+1) i range(C+1) range C+1 C 1 for i in range(C+1) [0]*4 [0] 0 4 dp_prev = [[0]*4 for i in range(C+1)] dp_prev [[0]*4 for i in range(C+1)] [0]*4 for i in range(C+1) for i in range(C+1) i range(C+1) range C+1 C 1 for i in range(C+1) [0]*4 [0] 0 4 dp_nex = [[0]*4 for i in range(C+1)] dp_nex [[0]*4 for i in range(C+1)] for r in range(1,R+1):
    for c in range(1,C+1):
        v = V[c][r]
        for k in range(4):
            if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
    dp_prev,dp_nex = dp_nex,dp_prev
    #print(dp_prev) r range(1,R+1) range 1 R+1 R 1 for c in range(1,C+1):
        v = V[c][r]
        for k in range(4):
            if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
     c range(1,C+1) range 1 C+1 C 1 v = V[c][r] v V[c][r] [c] V c r for k in range(4):
            if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
     k range(4) range 4 if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
     k==0 k 0 dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0]) dp_nex[c][0] [c] dp_nex c 0 max(dp_prev[c][3],dp_nex[c-1][0]) max dp_prev[c][3] [c] dp_prev c 3 dp_nex[c-1][0] [c-1] dp_nex c-1 c 1 0 elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
     k >0 k 0 dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v) dp_nex[c][k] [c] dp_nex c k max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v) max dp_nex[c-1][k] [c-1] dp_nex c-1 c 1 k dp_nex[c-1][k-1]+v dp_nex[c-1][k-1] [c-1] dp_nex c-1 c 1 k-1 k 1 v dp_prev[c][3]+v dp_prev[c][3] [c] dp_prev c 3 v dp_prev,dp_nex = dp_nex,dp_prev dp_prev dp_nex dp_nex dp_prev print(dp_prev[C][3]) print dp_prev[C][3] [C] dp_prev C 3 tma = lambda :tuple(map(int,input().split())) lambda :tuple(map(int,input().split())) tma lma = lambda :list(map(int,input().split())) lambda :list(map(int,input().split())) lma C,K = ma() ma() C dp_prev = [[0]*4 for i in range(C+1)] [[0]*4 for i in range(C+1)] dp_prev v = ma() ma() v yn = lambda fl:print("Yes") if fl else print("No") lambda fl:print("Yes") if fl else print("No") yn V = [[0 for i in range(R+1)] for j in range(C+1)] [[0 for i in range(R+1)] for j in range(C+1)] V R,C,K = ma() ma() R dp_prev,dp_nex = dp_nex,dp_prev dp_nex dp_prev dp_nex = dp_nex,dp_prev dp_prev dp_nex v = V[c][r] V[c][r] v ni = lambda:int(input()) lambda:int(input()) ni K = ma() ma() K r,c,v = ma() ma() r dp_nex = [[0]*4 for i in range(C+1)] [[0]*4 for i in range(C+1)] dp_nex c,v = ma() ma() c ma = lambda :map(int,input().split()) lambda :map(int,input().split()) ma
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
R,C,K = ma()
V = [[0 for i in range(R+1)] for j in range(C+1)]
for i in range(K):
    r,c,v = ma()
    V[c][r] = v
dp_prev = [[0]*4 for i in range(C+1)]
dp_nex = [[0]*4 for i in range(C+1)] #dp[h][k] h列目でmax kこまで選んだ時の最大値
for r in range(1,R+1):
    for c in range(1,C+1):
        v = V[c][r]
        for k in range(4):
            if k==0:
                dp_nex[c][0] = max(dp_prev[c][3],dp_nex[c-1][0])
            elif k >0:
                dp_nex[c][k] = max(dp_nex[c-1][k],dp_nex[c-1][k-1]+v,dp_prev[c][3]+v)
    dp_prev,dp_nex = dp_nex,dp_prev
    #print(dp_prev)


print(dp_prev[C][3])
