luogu#P1115. 最大子段和

最大子段和

Problem Description

Given a sequence aa of length nn, choose a non-empty contiguous subarray whose sum is maximized.

Input Format

The first line contains an integer nn, the length of the sequence. The second line contains nn integers; the ii-th is aia_i.

Output Format

Output one line with a single integer, the answer.

7
2 -4 3 -1 2 -4 3

4

Hint

  • Explanation for Sample 1: Choose the subarray [3,5][3, 5], which is {3,1,2}\{3, -1, 2\}; its sum is 44.
  • Constraints:
    • For 40% of the testdata, it is guaranteed that n2×103n \le 2 \times 10^3.
    • For 100% of the testdata, it is guaranteed that 1n2×1051 \le n \le 2 \times 10^5 and 104ai104-10^4 \le a_i \le 10^4.

Translated by ChatGPT 5