1. Drop a Negative Running Sum, Keep the Best Optimal
Intuition
Every pick of numbers side by side ends at some number, so walk the array once and ask, at each number, for the best pick that ends there. It is this number added to the best pick ending just before it, but only if that pick helps. A run whose sum has gone below 0 only drags down whatever comes after it, so drop it and let the next number start a fresh run from 0. The signal is a negative sum, not a negative number: 2 then -1 still sums to 1, which helps the 4 after it. Keep the best running sum seen beside it, starting at the first number rather than at 0, so an array of only negative numbers returns its largest number. Dropping the run at every negative number is the trap: on [3, -4, 2, -1, 4, -6, 1] it throws the -1 away and finds 4, the 4 alone, where 2, -1, 4 makes 5. Drop the running sum once it goes negative, and remember the best sum seen.
Algorithm
1. Start the best at the first number and the running sum at 0.
2. For each number, add it to the running sum.
3. Compare the running sum with the best, and keep the larger.
4. If the running sum is below 0, set it back to 0.
5. Return the best.
Time & Space
Time O(n): one pass, each number added once. Space O(1): two numbers are kept, the running sum and the best.