Problem:
Score : 500 points

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

Find the maximum possible sum of the values of items he picks up.

Constraints

1 \leq R, C \leq 3000

1 \leq K \leq \min(2 \times 10^5, R \times C)

1 \leq r_i \leq R

1 \leq c_i \leq C

(r_i, c_i) \neq (r_j, c_j) (i \neq j)

1 \leq v_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

Output

Print the maximum possible sum of the values of items Takahashi picks up.

Sample Input 1

2 2 3
1 1 3
2 1 4
1 2 5

Sample Output 1

8

He has two ways to get to the goal:

Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.

Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.

Thus, the maximum possible sum of the values of items he picks up is 8.

Sample Input 2

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2

Sample Output 2

29

We have four items in the 1-st row. The optimal choices are as follows:

Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.

Sample Input 3

4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

Sample Output 3

142

Solution in Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map.Entry;

import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class Main {
	
	public static final int TOPK = 3;
	public static class SegTree {
		int n;
		long[][] max;

		public SegTree(int n_) {
			int n = 1;
			while(n < n_){ n *= 2; }

			this.n = n;
			max = new long[this.n * 2 - 1][TOPK];
		}
		
		public long[] merge(long[] fst, long[] snd) {
			long[] ret = new long[3];
			int fst_index = 0, snd_index = 0, ret_index = 0;
			
			while(ret_index < TOPK) {
				if(fst_index >= TOPK && snd_index >= TOPK) {
					break;
				}else if(fst_index >= TOPK || fst[fst_index] < snd[snd_index]) {
					ret[ret_index++] = snd[snd_index++];
				}else if(snd_index >= TOPK || fst[fst_index] >= snd[snd_index]) {
					ret[ret_index++] = fst[fst_index++];
				}
			}
			
			return ret;
		}
		public void update(int k, long a) {
			k += n - 1;
			max[k][0] = a;

			while(k > 0){
				k = (k - 1) / 2;
				max[k] = merge(max[k * 2 + 1], max[k * 2 + 2]);
			}
		}

		public long[] query(int a, int b, int k, int l, int r) {
			if(r <= a || b <= l){
				return new long[3];
			}else if(a <= l && r <= b){
				return max[k];
			}else{
				return merge(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r));
			}
		}

		public long[] query(int a, int b) {
			return query(a, b, 0, 0, n);
		}
	}
	
	public static void main(String[] args) throws IOException {	
		try(final Scanner sc = new Scanner(System.in)){
			final int R = sc.nextInt();
			final int C = sc.nextInt();
			final int K = sc.nextInt();
			
			ArrayList<HashMap<Integer, Long>> board = new ArrayList<HashMap<Integer, Long>>();
			for(int i = 0; i <= R; i++) { board.add(new HashMap<Integer, Long>()); }
			SegTree[] segs = new SegTree[R + 1];
			for(int i = 0; i <= R; i++) { segs[i] = new SegTree(C); }
			
			for(int i = 0; i < K; i++) {
				final int r = sc.nextInt() - 1;
				final int c = sc.nextInt() - 1;
				final long v = sc.nextLong();
				
				board.get(r).put(c, v);
				segs[r].update(c, v);
			}
			if(!board.get(0).containsKey(0)) {
				board.get(0).put(0, 0l);
			}
			if(!board.get(R - 1).containsKey(C - 1)) {
				board.get(R - 1).put(C - 1, 0l);
			}
			board.get(R).put(C, 0l);
			
			long[][] DP = new long[R + 1][C + 1];
			
			for(int curr_row = 0; curr_row < R; curr_row++) {
				for(Entry<Integer, Long> curr_entry: board.get(curr_row).entrySet()) {
					final int next_row = curr_row + 1;
					final int curr_col = curr_entry.getKey();
					
					for(int next_col = curr_col; next_col <= C; next_col++) {
						long[] max = segs[curr_row].query(curr_col, next_col + 1);
						//System.out.println(Arrays.toString(max));
						DP[next_row][next_col] = Math.max(DP[next_row][next_col], DP[curr_row][curr_col] + (max[0] + max[1] + max[2])); 
					}
				}
			}
			
			//System.out.println(Arrays.deepToString(DP));
			System.out.println(DP[R][C]);
		}
	}

	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while(!hasNext()){
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
		

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int n) throws IOException {
			final int[] ret = new int[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextInt();
			}
			return ret;
		}

		public long[] nextLongArray(int n) throws IOException {
			final long[] ret = new long[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextLong();
			}
			return ret;
		}

		public void close() throws IOException {
			br.close();
		}
	}
}