PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000202. Tổng đoạn lớn nhất sau đúng một phép xóa (Maximum Subarray Sum After Exactly One Deletion)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng đoạn lớn nhất sau đúng một phép xóa (Maximum Subarray Sum After Exactly One Deletion)

Maximum Subarray Sum After Exactly One Deletion

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integers, choose a consecutive interval [l,r][l,r] containing at least two elements and delete exactly one element ada_d with l≤d≤rl\le d\le r. All remaining elements of the interval are kept, even if the deletion is internal. Maximize ∑i=lrai−ad\sum_{i=l}^r a_i-a_d. No additional deletions or empty remaining intervals are allowed; deletion at either endpoint is permitted.

Input

The first line contains nn. The second line contains exactly nn integers aia_i.

Output

Print the maximum sum after exactly one deletion.

Subtasks

  • Subtask 1 (20 points): 2≤n≤5002\le n\le 500; −109≤ai≤109-10^9\le a_i\le10^9.
  • Subtask 2 (30 points): 2≤n≤2000002\le n\le 200000; −109≤ai≤109-10^9\le a_i\le10^9; ai≥0a_i\ge0.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; −109≤ai≤109-10^9\le a_i\le10^9.

Example 1

Input

4
1 -2 3 4

Output

8

Explanation

Choose [1,4], delete -2, and keep 1+3+4=8. No sum can exceed the sum of positive elements, 8.

Example 2

Input

3
-5 -2 -7

Output

-2

Explanation

Choose [1,2], delete -5, and retain -2. Any nonempty subset of these negative numbers sums to at most -2.

Example 3

Input

2
4 5

Output

5

Explanation

Choose both elements and delete either 4 or 5; the maximum is 5.