#GD0000197. Dãy con luân phiên dấu (Alternating Subsequence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con luân phiên dấu (Alternating Subsequence)
Alternating Subsequence
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of integers with no zero elements. A subsequence is obtained by removing some entries without changing the relative order of the remaining entries.
A subsequence is alternating if any two consecutive selected elements have opposite signs. A single-element subsequence is also alternating.
First maximize the number of selected elements. Among all alternating subsequences having this maximum length, find the largest possible sum of their elements. Length is the primary criterion, not sum; the answer can be negative.
Input
- The first line contains an integer , the array length.
- The second line contains exactly nonzero integers in their original order.
Output
Print a single integer: the maximum sum among all longest alternating subsequences.
Subtasks
- Subtask 1 (20 points): ; , for every .
- Subtask 2 (30 points): ; , for every .
- Subtask 3 (50 points): ; , for every .
Examples
Input
5
1 2 3 -1 -2
Output
2
Explanation
The signs form two consecutive blocks: positive and negative . There is only one sign change, so the maximum alternating length is . Choosing from the positive block and from the negative block gives , the largest sum at that length.