#PS0000202. Tổng đoạn lớn nhất sau đúng một phép xóa (Maximum Subarray Sum After Exactly One Deletion)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 integers, choose a consecutive interval containing at least two elements and delete exactly one element with . All remaining elements of the interval are kept, even if the deletion is internal. Maximize . No additional deletions or empty remaining intervals are allowed; deletion at either endpoint is permitted.
Input
The first line contains . The second line contains exactly integers .
Output
Print the maximum sum after exactly one deletion.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; .
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.