// Approach: Dynamic Programming
// Language: C#
using System;
using System.Linq;
class GfG {
// Function to find the maximum path sum
static int MaximumPath(int[,] mat) {
int n = mat.GetLength(0), m = mat.GetLength(1);
// Initialize result with the maximum value in the first row
int res = mat.Cast<int>().Take(m).Max();
// Traverse the matrix row by row
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
// Get max value from possible previous row positions
int up = mat[i - 1, j];
int left = (j > 0) ? mat[i - 1, j - 1] : 0;
int right = (j < m - 1) ? mat[i - 1, j + 1] : 0;
// Update current cell with max path sum
mat[i, j] += Math.Max(Math.Max(up, left), right);
// Update result if current cell has a greater value
res = Math.Max(res, mat[i, j]);
}
}
return res;
}
static void Main() {
// Input matrix
int[,] mat = {
{10, 10, 2, 0, 20, 4},
{1, 0, 0, 30, 2, 5},
{0, 10, 4, 0, 2, 0},
{1, 0, 2, 20, 0, 4}
};
// Output the maximum path sum
Console.WriteLine(MaximumPath(mat));
}
}