import sys

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data)

shift = int(10**9 + 1)
rows, cols, k = inp[ii:ii+3]; ii += 3
items = [[] for _ in range(rows+1)]
best = [0]*(cols+1)
for _ in range(k):
	r, c, v = inp[ii:ii+3]; ii += 3
	items[r].append(c*shift+v)
for i in range(rows+1):
	items[i].sort()
S = SegmentTree(best)
ans = 0
for r in range(1, rows+1):
	dp = []
	best1, best2 = 0, 0
	Row = items[r]
	for X in Row:
		c, v = X//shift, X%shift
		v1 = S.query(1, c+1) + v
		v2 = best1 + v
		v3 = best2 + v
		V = v1
		if V < v2:
			V = v2
		if V < v3:
			V = v3
		dp.append(V)
		if v1 > best1:
			best1 = v1
		if v2 > best2:
			best2 = v2
		if ans < V:
			ans = V
	for j in range(len(dp)):
		c = Row[j]//shift
		if S[c] < dp[j]:
			S[c] = dp[j]
print(ans) import sys sys int(x) for x in sys.stdin.read().split() for x in sys.stdin.read().split() x sys.stdin.read().split() sys.stdin.read().split sys.stdin.read() sys.stdin.read sys.stdin sys stdin read split for x in sys.stdin.read().split() int(x) int x inp = [int(x) for x in sys.stdin.read().split()] inp [int(x) for x in sys.stdin.read().split()] ii = 0 ii 0 class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data) SegmentTree def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

     __init__ """initialize the segment tree with data""" self._default = default self._default self _default default self._func = func self._func self _func func self._len = len(data) self._len self _len len(data) len data self._size = _size = 1 << (self._len - 1).bit_length() self._size self _size 1 << (self._len - 1).bit_length() 1 (self._len - 1).bit_length() self._len - 1).bit_length self._len - 1 self._len self _len 1 bit_length = _size = 1 << (self._len - 1).bit_length() _size 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data self data [default] * (2 * _size) [default] default 2 * _size 2 _size self.data[_size:_size + self._len] = data self.data[_size:_size + self._len] self.data self data _size:_size + self._len _size _size + self._len _size self._len self _len data for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

     i reversed(range(_size)) reversed range(_size) range _size self.data[i] = func(self.data[i + i], self.data[i + i + 1]) self.data[i] self.data self data i func(self.data[i + i], self.data[i + i + 1]) func self.data[i + i] self.data self data i + i i i self.data[i + i + 1] self.data self data i + i + 1 i + i i i 1 self self data data default=0 default 0 func=max func max def __delitem__(self, idx):
        self[idx] = self._default

     __delitem__ self[idx] = self._default self[idx] self idx self._default self _default self self idx idx def __getitem__(self, idx):
        return self.data[idx + self._size]

     __getitem__ return self.data[idx + self._size] self.data[idx + self._size] self.data self data idx + self._size idx self._size self _size self self idx idx def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

     __setitem__ idx += self._size idx self._size self _size self.data[idx] = value self.data[idx] self.data self data idx value idx >>= 1 idx 1 while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

     idx self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) self.data[idx] self.data self data idx self._func(self.data[2 * idx], self.data[2 * idx + 1]) self._func self _func self.data[2 * idx] self.data self data 2 * idx 2 idx self.data[2 * idx + 1] self.data self data 2 * idx + 1 2 * idx 2 idx 1 idx >>= 1 idx 1 self self idx idx value value def __len__(self):
        return self._len

     __len__ return self._len self._len self _len self self def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

     query """func of data[start, stop)""" start += self._size start self._size self _size stop += self._size stop self._size self _size res_left = res_right = self._default res_left self._default self _default = res_right = self._default res_right self._default while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

         start < stop start stop if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
             start & 1 start 1 res_left = self._func(res_left, self.data[start]) res_left self._func(res_left, self.data[start]) self._func self _func res_left self.data[start] self.data self data start start += 1 start 1 if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
             stop & 1 stop 1 stop -= 1 stop 1 res_right = self._func(self.data[stop], res_right) res_right self._func(self.data[stop], res_right) self._func self _func self.data[stop] self.data self data stop res_right start >>= 1 start 1 stop >>= 1 stop 1 return self._func(res_left, res_right) self._func(res_left, res_right) self._func self _func res_left res_right self self start start stop stop def __repr__(self):
        return "SegmentTree({0})".format(self.data) __repr__ return "SegmentTree({0})".format(self.data) "SegmentTree({0})".format(self.data) "SegmentTree({0})".format "SegmentTree({0})" format self.data self data self self shift = int(10**9 + 1) shift int(10**9 + 1) int 10**9 + 1 10**9 10 9 1 rows, cols, k = inp[ii:ii+3] rows inp[ii:ii+3] inp ii:ii+3 ii ii+3 ii 3 cols k ii += 3 ii 3 [] for _ in range(rows+1) for _ in range(rows+1) _ range(rows+1) range rows+1 rows 1 for _ in range(rows+1) [] items = [[] for _ in range(rows+1)] items [[] for _ in range(rows+1)] best = [0]*(cols+1) best [0]*(cols+1) [0] 0 cols+1 cols 1 for _ in range(k):
	r, c, v = inp[ii:ii+3]; ii += 3
	items[r].append(c*shift+v) _ range(k) range k r, c, v = inp[ii:ii+3] r inp[ii:ii+3] inp ii:ii+3 ii ii+3 ii 3 c v ii += 3 ii 3 items[r].append(c*shift+v) [r].append [r] items r append c*shift+v c*shift c shift v for i in range(rows+1):
	items[i].sort() i range(rows+1) range rows+1 rows 1 items[i].sort() [i].sort [i] items i sort S = SegmentTree(best) S SegmentTree(best) SegmentTree best ans = 0 ans 0 for r in range(1, rows+1):
	dp = []
	best1, best2 = 0, 0
	Row = items[r]
	for X in Row:
		c, v = X//shift, X%shift
		v1 = S.query(1, c+1) + v
		v2 = best1 + v
		v3 = best2 + v
		V = v1
		if V < v2:
			V = v2
		if V < v3:
			V = v3
		dp.append(V)
		if v1 > best1:
			best1 = v1
		if v2 > best2:
			best2 = v2
		if ans < V:
			ans = V
	for j in range(len(dp)):
		c = Row[j]//shift
		if S[c] < dp[j]:
			S[c] = dp[j] r range(1, rows+1) range 1 rows+1 rows 1 dp = [] dp [] best1, best2 = 0, 0 best1 0 best2 0 Row = items[r] Row items[r] items r for X in Row:
		c, v = X//shift, X%shift
		v1 = S.query(1, c+1) + v
		v2 = best1 + v
		v3 = best2 + v
		V = v1
		if V < v2:
			V = v2
		if V < v3:
			V = v3
		dp.append(V)
		if v1 > best1:
			best1 = v1
		if v2 > best2:
			best2 = v2
		if ans < V:
			ans = V
	 X Row c, v = X//shift, X%shift c X//shift X shift v X%shift X shift v1 = S.query(1, c+1) + v v1 S.query(1, c+1) + v S.query(1, c+1) S.query S query 1 c+1 c 1 v v2 = best1 + v v2 best1 + v best1 v v3 = best2 + v v3 best2 + v best2 v V = v1 V v1 if V < v2:
			V = v2
		 V < v2 V v2 V = v2 V v2 if V < v3:
			V = v3
		 V < v3 V v3 V = v3 V v3 dp.append(V) dp.append dp append V if v1 > best1:
			best1 = v1
		 v1 > best1 v1 best1 best1 = v1 best1 v1 if v2 > best2:
			best2 = v2
		 v2 > best2 v2 best2 best2 = v2 best2 v2 if ans < V:
			ans = V
	 ans < V ans V ans = V ans V for j in range(len(dp)):
		c = Row[j]//shift
		if S[c] < dp[j]:
			S[c] = dp[j] j range(len(dp)) range len(dp) len dp c = Row[j]//shift c Row[j]//shift Row[j] Row j shift if S[c] < dp[j]:
			S[c] = dp[j] S[c] < dp[j] S[c] S c dp[j] dp j S[c] = dp[j] S[c] S c dp[j] dp j print(ans) print ans cols, k = inp[ii:ii+3] inp[ii:ii+3] cols v = inp[ii:ii+3] inp[ii:ii+3] v ans = 0 0 ans ii = 0 0 ii Row = items[r] items[r] Row class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data) class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data) SegmentTree V = v1 v1 V ii += 3 3 ii dp = [] [] dp v3 = best2 + v best2 + v v3 V = v2 v2 V best1 = v1 v1 best1 best2 = v2 v2 best2 v1 = S.query(1, c+1) + v S.query(1, c+1) + v v1 v2 = best1 + v best1 + v v2 best = [0]*(cols+1) [0]*(cols+1) best best2 = 0, 0 0 best2 c, v = X//shift, X%shift X//shift c k = inp[ii:ii+3] inp[ii:ii+3] k v = X//shift, X%shift X%shift v c = Row[j]//shift Row[j]//shift c inp = [int(x) for x in sys.stdin.read().split()] [int(x) for x in sys.stdin.read().split()] inp ii += 3 3 ii items = [[] for _ in range(rows+1)] [[] for _ in range(rows+1)] items r, c, v = inp[ii:ii+3] inp[ii:ii+3] r best1, best2 = 0, 0 0 best1 ans = V V ans rows, cols, k = inp[ii:ii+3] inp[ii:ii+3] rows S = SegmentTree(best) SegmentTree(best) S V = v3 v3 V shift = int(10**9 + 1) int(10**9 + 1) shift c, v = inp[ii:ii+3] inp[ii:ii+3] c
import sys

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data)

shift = int(10**9 + 1)
rows, cols, k = inp[ii:ii+3]; ii += 3
items = [[] for _ in range(rows+1)]
best = [0]*(cols+1)
for _ in range(k):
	r, c, v = inp[ii:ii+3]; ii += 3
	items[r].append(c*shift+v)
for i in range(rows+1):
	items[i].sort()
S = SegmentTree(best)
ans = 0
for r in range(1, rows+1):
	dp = []
	best1, best2 = 0, 0
	Row = items[r]
	for X in Row:
		c, v = X//shift, X%shift
		v1 = S.query(1, c+1) + v
		v2 = best1 + v
		v3 = best2 + v
		V = v1
		if V < v2:
			V = v2
		if V < v3:
			V = v3
		dp.append(V)
		if v1 > best1:
			best1 = v1
		if v2 > best2:
			best2 = v2
		if ans < V:
			ans = V
	for j in range(len(dp)):
		c = Row[j]//shift
		if S[c] < dp[j]:
			S[c] = dp[j]
print(ans)
