import numpy as np
from numba import jit


def test():
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]

    items = np.zeros((R+1, C+1), dtype='int64')
    for item in rcv:
        items[item[0]][item[1]] = item[2]

    dp = np.zeros((R+1, C+1), dtype='int64')

    # まずは3個の制約がない場合での実装
    for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

    print(dp[R][C])


@jit
def main(R, C, items):

    dp = np.zeros((4, R+1, C+1), dtype=np.int64)

    # 3個の制約がある場合の実装
    for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

    ans = 0
    for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

    print(ans)


if __name__ == "__main__":
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]
    items = np.zeros((R+1, C+1), dtype=np.int64)
    for item in rcv:
        items[item[0]][item[1]] = item[2]
    main(R, C, items) import numpy as np numpy from numba import jit def test():
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]

    items = np.zeros((R+1, C+1), dtype='int64')
    for item in rcv:
        items[item[0]][item[1]] = item[2]

    dp = np.zeros((R+1, C+1), dtype='int64')

    # まずは3個の制約がない場合での実装
    for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

    print(dp[R][C]) test R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K list(map(int, input().split())) for _ in range(K) for _ in range(K) _ range(K) range K for _ in range(K) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split rcv = [list(map(int, input().split())) for _ in range(K)] rcv [list(map(int, input().split())) for _ in range(K)] items = np.zeros((R+1, C+1), dtype='int64') items np.zeros((R+1, C+1), dtype='int64') np.zeros np zeros (R+1, C+1) R+1 R 1 C+1 C 1 dtype='int64' dtype 'int64' for item in rcv:
        items[item[0]][item[1]] = item[2]

     item rcv items[item[0]][item[1]] = item[2] items[item[0]][item[1]] [item[0]] items item[0] item 0 item[1] item 1 item[2] item 2 dp = np.zeros((R+1, C+1), dtype='int64') dp np.zeros((R+1, C+1), dtype='int64') np.zeros np zeros (R+1, C+1) R+1 R 1 C+1 C 1 dtype='int64' dtype 'int64' for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

     j range(1, R+1) range 1 R+1 R 1 for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

     i range(1, C+1) range 1 C+1 C 1 dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i]) dp[j][i] [j] dp j i max(dp[j][i], dp[j-1][i] + items[j][i]) max dp[j][i] [j] dp j i dp[j-1][i] + items[j][i] dp[j-1][i] [j-1] dp j-1 j 1 i items[j][i] [j] items j i dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i]) dp[j][i] [j] dp j i max(dp[j][i], dp[j][i-1] + items[j][i]) max dp[j][i] [j] dp j i dp[j][i-1] + items[j][i] dp[j][i-1] [j] dp j i-1 i 1 items[j][i] [j] items j i print(dp[R][C]) print dp[R][C] [R] dp R C def main(R, C, items):

    dp = np.zeros((4, R+1, C+1), dtype=np.int64)

    # 3個の制約がある場合の実装
    for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

    ans = 0
    for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

    print(ans) main dp = np.zeros((4, R+1, C+1), dtype=np.int64) dp np.zeros((4, R+1, C+1), dtype=np.int64) np.zeros np zeros (4, R+1, C+1) 4 R+1 R 1 C+1 C 1 dtype=np.int64 dtype np.int64 np int64 for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

     i range(1, R+1) range 1 R+1 R 1 for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

     j range(1, C+1) range 1 C+1 C 1 for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

     k range(4) range 4 dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j]) dp[0][i][j] [i] [0] dp 0 i j max(dp[0][i][j], dp[k][i-1][j]) max dp[0][i][j] [i] [0] dp 0 i j dp[k][i-1][j] [i-1] [k] dp k i-1 i 1 j dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j]) dp[1][i][j] [i] [1] dp 1 i j max(dp[1][i][j], dp[k][i-1][j] + items[i][j]) max dp[1][i][j] [i] [1] dp 1 i j dp[k][i-1][j] + items[i][j] dp[k][i-1][j] [i-1] [k] dp k i-1 i 1 j items[i][j] [i] items i j dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1]) dp[k][i][j] [i] [k] dp k i j max(dp[k][i][j], dp[k][i][j-1]) max dp[k][i][j] [i] [k] dp k i j dp[k][i][j-1] [i] [k] dp k i j-1 j 1 if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

     k > 0 k 0 dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j]) dp[k][i][j] [i] [k] dp k i j max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j]) max dp[k][i][j] [i] [k] dp k i j dp[k-1]
                                      [i][j-1] + items[i][j] dp[k-1]
                                      [i][j-1] [i] [k-1] dp k-1 k 1 i j-1 j 1 items[i][j] [i] items i j ans = 0 ans 0 for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

     k range(4) range 4 if ans < dp[k][R][C]:
            ans = dp[k][R][C]

     ans < dp[k][R][C] ans dp[k][R][C] [R] [k] dp k R C ans = dp[k][R][C] ans dp[k][R][C] [R] [k] dp k R C print(ans) print ans R R C C items items @jit main jit main if __name__ == "__main__":
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]
    items = np.zeros((R+1, C+1), dtype=np.int64)
    for item in rcv:
        items[item[0]][item[1]] = item[2]
    main(R, C, items) __name__ == "__main__" __name__ "__main__" R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K list(map(int, input().split())) for _ in range(K) for _ in range(K) _ range(K) range K for _ in range(K) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split rcv = [list(map(int, input().split())) for _ in range(K)] rcv [list(map(int, input().split())) for _ in range(K)] items = np.zeros((R+1, C+1), dtype=np.int64) items np.zeros((R+1, C+1), dtype=np.int64) np.zeros np zeros (R+1, C+1) R+1 R 1 C+1 C 1 dtype=np.int64 dtype np.int64 np int64 for item in rcv:
        items[item[0]][item[1]] = item[2]
     item rcv items[item[0]][item[1]] = item[2] items[item[0]][item[1]] [item[0]] items item[0] item 0 item[1] item 1 item[2] item 2 main(R, C, items) main R C items def main(R, C, items):

    dp = np.zeros((4, R+1, C+1), dtype=np.int64)

    # 3個の制約がある場合の実装
    for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

    ans = 0
    for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

    print(ans) def main(R, C, items):

    dp = np.zeros((4, R+1, C+1), dtype=np.int64)

    # 3個の制約がある場合の実装
    for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

    ans = 0
    for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

    print(ans) main items = np.zeros((R+1, C+1), dtype=np.int64) np.zeros((R+1, C+1), dtype=np.int64) items R, C, K = map(int, input().split()) map(int, input().split()) R rcv = [list(map(int, input().split())) for _ in range(K)] [list(map(int, input().split())) for _ in range(K)] rcv K = map(int, input().split()) map(int, input().split()) K C, K = map(int, input().split()) map(int, input().split()) C def test():
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]

    items = np.zeros((R+1, C+1), dtype='int64')
    for item in rcv:
        items[item[0]][item[1]] = item[2]

    dp = np.zeros((R+1, C+1), dtype='int64')

    # まずは3個の制約がない場合での実装
    for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

    print(dp[R][C]) def test():
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]

    items = np.zeros((R+1, C+1), dtype='int64')
    for item in rcv:
        items[item[0]][item[1]] = item[2]

    dp = np.zeros((R+1, C+1), dtype='int64')

    # まずは3個の制約がない場合での実装
    for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

    print(dp[R][C]) test
import numpy as np
from numba import jit


def test():
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]

    items = np.zeros((R+1, C+1), dtype='int64')
    for item in rcv:
        items[item[0]][item[1]] = item[2]

    dp = np.zeros((R+1, C+1), dtype='int64')

    # まずは3個の制約がない場合での実装
    for j in range(1, R+1):
        for i in range(1, C+1):
            # 下に行く遷移
            dp[j][i] = max(dp[j][i], dp[j-1][i] + items[j][i])

            # 右に行く遷移
            dp[j][i] = max(dp[j][i], dp[j][i-1] + items[j][i])

    print(dp[R][C])


@jit
def main(R, C, items):

    dp = np.zeros((4, R+1, C+1), dtype=np.int64)

    # 3個の制約がある場合の実装
    for i in range(1, R+1):
        for j in range(1, C+1):
            for k in range(4):
                # 下に行く遷移
                # 取らない場合
                dp[0][i][j] = max(dp[0][i][j], dp[k][i-1][j])
                # 取る場合
                dp[1][i][j] = max(dp[1][i][j], dp[k][i-1][j] + items[i][j])

                # 右に行く遷移
                # 取らない場合
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                # 取る場合
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1]
                                      [i][j-1] + items[i][j])

    ans = 0
    for k in range(4):
        if ans < dp[k][R][C]:
            ans = dp[k][R][C]

    print(ans)


if __name__ == "__main__":
    R, C, K = map(int, input().split())
    rcv = [list(map(int, input().split())) for _ in range(K)]
    items = np.zeros((R+1, C+1), dtype=np.int64)
    for item in rcv:
        items[item[0]][item[1]] = item[2]
    main(R, C, items)
