C# Max Double Slice Sum
static long MaxDoubleSliceSum(int[] a)
{
var size = a.Length;
if (size < 3)
{
return 0;
}
var p1 = new long[size];
var p2 = new long[size];
p1[1] = 0;
p2[size - 2] = 0;
for (int i = 2; i < size - 1; i++)
{
p1[i] = Math.Max(0, p1[i - 1] + a[i - 1]);
p2[size - i - 1] = Math.Max(0, p2[size - i] + a[size - i]);
}
var sum = p1[1] + p2[1];
for (int i = 1; i < size - 1; i++)
{
sum = Math.Max(sum, p1[i] + p2[i]);
}
return sum;
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.