C# Max Slice Sum
static long MaxSliceSum(int[] a)
{
long tmp = long.MinValue;
long max = long.MinValue;
foreach (var v in a)
{
tmp = Math.Max(tmp + v, v);
max = Math.Max(max, tmp);
}
return max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.