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

#GD0000197. Dãy con luân phiên dấu (Alternating Subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

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 nn integers a1,a2,…,ana_1,a_2,\ldots,a_n 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 nn, the array length.
  • The second line contains exactly nn nonzero integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Print a single integer: the maximum sum among all longest alternating subsequences.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; −109≤ai≤109-10^9\le a_i\le10^9, ai≠0a_i\ne0 for every ii.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −109≤ai≤109-10^9\le a_i\le10^9, ai≠0a_i\ne0 for every ii.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le 2\cdot10^5; −109≤ai≤109-10^9\le a_i\le10^9, ai≠0a_i\ne0 for every ii.

Examples

Input

5
1 2 3 -1 -2

Output

2

Explanation

The signs form two consecutive blocks: positive (1,2,3)(1,2,3) and negative (−1,−2)(-1,-2). There is only one sign change, so the maximum alternating length is 22. Choosing 33 from the positive block and −1-1 from the negative block gives 3+(−1)=23+(-1)=2, the largest sum at that length.