C++ Max Slice Sum
#include <algorithm>
#include <limits>
#include <vector>
long long maxSliceSum(const std::vector<int>& a)
{
long long tmp = std::numeric_limits<long long>::min();
long long max = std::numeric_limits<long long>::min();
for (int v : a) {
tmp = std::max(tmp + v, static_cast<long long>(v));
max = std::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.