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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		public static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush =
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			int R = cin.ReadInt, C = cin.ReadInt, K = cin.ReadInt;
			var V = new int[R, C];
			for(int i = 0; i < K; i++) {
				var d = cin.ReadIntArray(3);
				V[d[0] - 1, d[1] - 1] = d[2];
			}
			var P = new long[R, C, 4];
			P[0, 0, 0] = 0;
			P[0, 0, 1] = V[0, 0];
			for(int j = 1; j < C; j++) {
				P[0, j, 0] = 0;
				P[0, j, 1] = Max(P[0, j - 1, 1], V[0, j]);
				for(int k = 2; k < 4; k++) {
					P[0, j, k] = Max(P[0, j - 1, k], P[0, j - 1, k - 1] + V[0, j]);
				}
			}
			for(int i = 1; i < R; i++) {
				P[i, 0, 0] = maxK(P, i - 1, 0);
				P[i, 0, 1] = P[i, 0, 0] + V[i, 0];
			}
			for(int i = 1; i < R; i++) {
				for(int j = 1; j < C; j++) {
					P[i, j, 0] = Max(maxK(P, i - 1, j), P[i, j - 1, 0]);
					P[i, j, 1] = Max(P[i, j, 0] + V[i, j], P[i, j - 1, 1]);
					for(int k = 2; k < 4; k++) {
						P[i, j, k] = Max(P[i, j - 1, k], P[i, j - 1, k - 1] + V[i, j]);
					}
				}
			}
			Console.WriteLine(maxK(P, R - 1, C - 1));
		}

		public long maxK(long[,,] X, int i, int j) {
			return Enumerable.Range(0, 4).Max(k => X[i, j, k]);
		}

	}

	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ")
			=> string.Join(separator, X);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator = separator;
			this._stream = stream;
			inputStream = new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count != 0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i < tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i < N; ++i)
				ret[i] = Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek() == -1; }
		}
	}

	class SegmentTree<T> {
		//UI is 0-indexed, internal is 1-indexed
		public readonly int rank;
		public T[] Nodes { get; }
		public Func<T, T, T> OPER { get; }
		public T unit { get; set; }
		protected int width { get { return 1 << (rank - 1); } }
		private T raise(int c) {
			return (c & 1) == 0
			? OPER(Nodes[c - 1], Nodes[(c - 1) + 1]) //$c is even
			: OPER(Nodes[(c - 1) - 1], Nodes[c - 1]); //odd
		}
		public IEnumerable<T> leafs() => Nodes.Skip(width - 1);
		protected int leafIndex(int x) => (1 << (rank - 1)) + (x - 1);
		public SegmentTree(int count, Func<T, T, T> _OPER, T _unit) {
			if(count == 0) throw new ArgumentNullException();
			var last = count - 1;
			rank = 0;
			if(last > 0) {
				while((last >> rank) > 1) rank++;
				rank += 2;
			} else rank = 1;
			Nodes = Enumerable.Repeat(_unit, (1 << rank) - 1).ToArray();
			OPER = _OPER;
			unit = _unit;
		}
		public virtual void SetImmediate(int x, T value) {
			var r = rank - 1;
			var jc = (1 << r) + x;
			Nodes[jc - 1] = value;
			for(r--; r >= 0; r--) {
				var parentIndex = (jc >> 1) - 1;
				var oldValue = Nodes[parentIndex];
				Nodes[parentIndex] = raise(jc);
				if(oldValue.Equals(Nodes[parentIndex])) break;
				jc >>= 1;
			}
		}
		public virtual void ImmediateOperate<S>(int x, S value, Func<S, T, T> LeftOP) {
			SetImmediate(x, LeftOP(value, Nodes[leafIndex(x)]));
		}
		public virtual T GetValue(int start, int end) => GV(start, end, 1, 0, width);
		private T GV(int start, int end, int count, int focusStart, int focusEnd) {
			if(focusEnd <= start || end <= focusStart) return unit;
			if(start <= focusStart && focusEnd <= end) return Nodes[count - 1];
			var mid = focusStart + (focusEnd - focusStart) / 2;
			return OPER(
				GV(start, end, count * 2, focusStart, mid),
				GV(start, end, count * 2 + 1, mid, focusEnd)
			);
		}
		public IEnumerable<T> GetLayer(int c) => Nodes.Skip((1 << (c - 1)) - 1).Take(1 << (c - 1));
		public IEnumerable<T> GetLeaves() => Nodes.Skip((1 << (rank - 1)) - 1).Take(1 << (rank - 1));
	}
}