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.CodeDom;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;

namespace AtCoder
{
    public static class Ex
    {
        public static bool IsNullOrEmpty(this string s) { return string.IsNullOrEmpty(s); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); }
        public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); }
        public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); }
        public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); }
        public static int PopCount(this int bits)
        {
            bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
            bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
            bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
            bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
            return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
        }
    }



    partial class AtCoder
    {
        public string GetStr() { return Console.ReadLine().Trim(); }
        public char GetChar() { return Console.ReadLine().Trim()[0]; }
        public int GetInt() { return int.Parse(Console.ReadLine().Trim()); }
        public long GetLong() { return long.Parse(Console.ReadLine().Trim()); }
        public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); }
        public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); }
        public string[][] GetStrArray(int N)
        {
            var res = new string[N][];
            for (int i = 0; i < N; i++) res[i] = Console.ReadLine().Trim().Split(' ');
            return res;
        }
        public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); }
        public int[][] GetIntArray(int N)
        {
            var res = new int[N][];
            for (int i = 0; i < N; i++) res[i] = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
            return res;
        }
        public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); }
        public long[][] GetLongArray(int N)
        {
            var res = new long[N][];
            for (int i = 0; i < N; i++) res[i] = Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
            return res;
        }
        public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); }
        public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); }
        public double[][] GetDoubleArray(int N)
        {
            var res = new double[N][];
            for (int i = 0; i < N; i++) res[i] = Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray();
            return res;
        }
        public char[][] GetGrid(int H)
        {
            var res = new char[H][];
            for (int i = 0; i < H; i++) res[i] = Console.ReadLine().Trim().ToCharArray();
            return res;
        }
        public T[] CreateArray<T>(int N, T value)
        {
            var res = new T[N];
            for (int i = 0; i < N; i++) res[i] = value;
            return res;
        }
        public T[][] CreateArray<T>(int H, int W, T value)
        {
            var res = new T[H][];
            for (int i = 0; i < H; i++)
            {
                res[i] = new T[W];
                for (int j = 0; j < W; j++) res[i][j] = value;
            }
            return res;
        }
        public T[][][] CreateArray<T>(int H, int W, int R, T value)
        {
            var res = new T[H][][];
            for (int i = 0; i < H; i++)
            {
                res[i] = new T[W][];
                for (int j = 0; j < W; j++)
                {
                    res[i][j] = new T[R];
                    for (int k = 0; k < R; k++) res[i][j][k] = value;
                }
            }
            return res;
        }

        Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<int>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                dic[a].Add(b);
                if (isDirected == false) dic[b].Add(a);
            }
            return dic;
        }
        Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<Edge>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                var c = input[2];
                dic[a].Add(new Edge(b, c));
                if (isDirected == false) dic[b].Add(new Edge(a, c));
            }
            return dic;
        }

        bool eq<T, U>() => typeof(T).Equals(typeof(U));
        T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        void Multi<T>(out T a) => a = cv<T>(GetStr());
        void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
        void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]);
        }
        void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]);
        }
        void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]);
        }

        int[] dx = new int[] { 1, 0, -1, 0, 1, 1, -1, -1 };
        int[] dy = new int[] { 0, 1, 0, -1, 1, -1, 1, -1 };
    }

    class Pair : IComparable<Pair>
    {
        public Pair(int node, long cost)
        {
            Node = node; Cost = cost;
        }
        public int Node { get; set; }
        public long Cost { get; set; }
        public int CompareTo(Pair p)
        {
            return p.Cost > Cost ? 1 : -1;
        }
    }

    class Edge
    {
        public Edge(int to, long length)
        {
            To = to;
            Length = length;
        }
        public int To { get; set; }
        public long Length { get; set; }
    }

    class Comparable : IComparable<Comparable>
    {
        public Comparable(int value)
        {
            Value = value;
        }

        public int Value { get; set; }
        public int CompareTo(Comparable c)
        {
            var a = Math.Abs(Value);
            var b = Math.Abs(c.Value);
            return a > b ? 1 : a == b ? 0 : -1;
        }
    }

    class Comparer<T> : IComparer<T>
    {
        public int Compare(T t1, T t2)
        {
            return 1;
        }
    }



    partial class Program
    {
        static void Main()
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            new AtCoder().Solve();
            Console.Out.Flush();
            Console.Read();
        }
    }

    public partial class AtCoder
    {
        public void Solve()
        {
            int H, W, K;
            Multi(out H, out W, out K);
            var item = CreateArray(H, W, 0L);
            for (int i = 0; i < K; i++)
            {
                int y, x; long v;
                Multi(out y, out x, out v);
                y--; x--;
                item[y][x] = v;
            }

            var dp = CreateArray(4, H + 1, W + 1, long.MinValue);
            dp[0][0][1] = 0;

            for (int y = 0; y < H; y++)
            {
                for (int x = 0; x < W; x++)
                {
                    var value = item[y][x];
                    var max = Math.Max(Math.Max(dp[0][y][x + 1], dp[1][y][x + 1]), Math.Max(dp[2][y][x + 1], dp[3][y][x + 1]));
                    dp[0][y + 1][x + 1] = Math.Max(max, dp[0][y + 1][x]);
                    dp[1][y + 1][x + 1] = Math.Max(Math.Max(dp[1][y + 1][x], dp[0][y + 1][x] + value), max + value);
                    dp[2][y + 1][x + 1] = Math.Max(dp[2][y + 1][x], dp[1][y + 1][x] + value);
                    dp[3][y + 1][x + 1] = Math.Max(dp[3][y + 1][x], dp[2][y + 1][x] + value);
                }
            }
            Console.WriteLine(Enumerable.Range(0, 4).Select(i => dp[i][H][W]).Max());
        }
    }
}