#FT0000105. Tổng lớn nhất của dãy con cân bằng (Maximum Balanced Subsequence Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng lớn nhất của dãy con cân bằng (Maximum Balanced Subsequence Sum)
Maximum Balanced Subsequence Sum
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an integer sequence whose positions are numbered through . A nonempty subsequence selects indices .
It is called balanced if, for every two consecutive selected indices , the following inequality holds:
Every one-element subsequence is considered balanced. The sum of a subsequence is the sum of the values at its selected positions. Find the maximum sum of any nonempty balanced subsequence. Values may be negative, so the answer may also be negative.
Input
- The first line contains integer , the sequence length.
- The second line contains integers in order.
Output
Print one integer: the maximum possible sum of a nonempty balanced subsequence.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Example 1
Input
4
3 3 5 6
Output
14
Explanation
For , select indices , giving values . From positions to , ; from positions to , . The sum is . Indices also achieve . The two values cannot both be selected since their difference is smaller than their index gap . The maximum is 14.
Example 2
Input
2
-2 -1
Output
-1
Explanation
Selecting position alone gives sum , and position alone gives sum . Selecting both is balanced because , but its sum is . The maximum of is , so print -1.