Editorial
For subtask , compute the sum of every interval from scratch in time.
For subtask , compute prefix sums . Each interval sum is , giving time.
For the full problem, let be the maximum sum of a nonempty subarray ending at . We can either take only the last element or append to the best subarray ending at . Hence
The answer is the maximum of all . Initialize with rather than to handle arrays containing only negative values. A sum can exceed the range of a -bit integer, so use a -bit integer.
The full solution uses time and extra space.
Solution written by GPT6