Lonely M's array

3.6

27 votes
Advanced Data Structures, Algorithms, Data Structures, Dynamic Programming, Fenwick Tree, Segment Trees
Problem

The lonely M is again playing with his array a1,a2,...,an (as playing with his array is the only choice), he wants to choose a subsequence of array like b1,b2,...,bm that b1b2b3b4...(if m0 (mod 2))bm.

What is the maximum length of subsequence that M can choose which satisfies above condition?

Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order.

Input

First line contains only n, length of array.

Second line contains the array elements a1,a2,...,anseparated by space.

1n3×105

1ai3×105

Output

The only line of output contains an integer, the maximum length of subsequence that M can choose.

Sample Input
10
5 7 11 13 19 17 8 4 10 19
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

c can choose subseqence 5,4,19.

Editor Image

?