ALTSEQ - Alternating Sequences


Given an array a of N integers a1, a2, a3, ... aN you have to find the longest alternating sub-sequence of this array.

An alternating sequence b1, b2 ... bk, k>=1 is a sequence that has the 2 following properties:

  • |b1| < |b2| < |b3| < ... < |bk|
  • The signs alternate between adjacent elements, i.e., if b1 > 0 then b2 < 0, b3 > 0 and so on. Alternatively, if b1 < 0, then b2 > 0, b3 < 0 and so on.

A sub sequence of array a is a sequence obtained by dropping some elements of the array a. Here is the formal definition.

It is guaranteed that the array a contains no element equal to 0.

Constraints

1 <= N <= 5000
|ai| <= 10^9, ai not equal to 0.

Input

The first line contains a single integer N, denoting the size of the array. The next line contains N integers, denoting the array a.

Output

Print a single integer - the length of the longest alternating sub-sequence.

Example

Input:
8
1 2 -2 -3 5 -7 -8 10

Output:
5

Explanation

One of the longest alternating subsequence is

     1  -2  5  -7  10

hide comments
shubham97361: 2019-04-28 11:07:16

checking the opposite signs by multiplying elements works fine . Check this test case:
5
-5 -2 4 2 -1

ks1999: 2019-04-11 21:44:50

For everyone who is getting WA, if you are checking if one is negative and other positive with a*b < 0 that is wrong, it is going to overflow, and if says WA at 15th case it doesnt mean that you are wrong at that case, it just means that compiler or what ever reads every test case and after that he evaluate your code.

mranderson: 2019-04-05 13:00:01

to check that two elements have opposite signs, doing the product between them can lead to overflow error even with long long

sai_chavali: 2019-03-04 10:00:00

I don't why this problem is giving TLE in python but same logic accepted in C

anshuman16423: 2018-09-23 06:42:34

AC in one go!!
DP, Bottom up

codefield: 2018-07-05 19:10:35

if gets stuck on test case 15, check 2 test cases:
5
-5 -2 4 2 -1
expected output: 2

1
2
expected output: 1

liman98: 2018-06-25 01:27:52

lol I missed AC like 5-6 times because of wrong bracket combos in if statement XD

soham_12345: 2018-06-13 11:32:00

My 50th

Last edit: 2018-06-13 11:33:21
aman_sachin200: 2018-06-13 09:59:55

Easy One!!

dsri_99: 2018-06-06 08:04:13

wasted a lot of time at test case 15. Else it's a simple dp problem


Added by:Beer hu !!
Date:2016-07-01
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Hackerrank