Problem:
Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::stdin;
use std::time::SystemTime;

#[allow(dead_code)]
fn read_line() -> String {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn read<T>() -> T
where T: std::str::FromStr
{
    read_line().parse::<T>().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T>() -> Vec<T>
where T: std::str::FromStr
{
    read_line().split(" ").map(|n| n.parse::<T>().ok().unwrap()).collect()
}

#[allow(dead_code)]
fn read_chars() -> Vec<char>
{
    read_line().chars().collect()
}

#[allow(dead_code)]
fn print_time() -> () {
    println!("{:?}", SystemTime::now());
}

#[allow(unused_variables)]
fn main() {
    let n: Vec<usize> = read_vec();
    let mut v: Vec<usize> = vec![];
    for _ in 0..n[0] {
        v.push(read());
    }
    
    let c = v[0];
    if v.iter().all(|p| p == &c) {
        println!("{}", v.len());
        std::process::exit(0);
    }
    
    let mut data = vec![];
    data.push(v[0]);
    
    for i in 1..n[0] {
        let result = data.binary_search_left(&v[i]);
        let index = match result {
            Ok(index) => index,
            Err(index) => index,
        };
        
        if data.get(index) == None {
            if data[index - 1] < v[i] {
                data[index - 1] = v[i];
            } else {
                data.insert(index, v[i]);
            }
        } else {
            if data[index] < v[i] {
                data[index] = v[i];
            } else if index > 0 && data.get(index - 1).map(|n| n < &v[i]).unwrap_or(false) {
                data[index - 1] = v[i];
            } else {
                data.insert(index, v[i]);
            }
        }
    }
    
    println!("{}", data.len());
}

trait BinarySearchLeft<T> {
    fn binary_search_left(&self, x: &T) -> Result<usize, usize>;
}

impl<T> BinarySearchLeft<T> for Vec<T>
where T: std::cmp::Ord + std::fmt::Debug
{
    fn binary_search_left(&self, x: &T) -> Result<usize, usize> {
        let index = self.binary_search(x);
        match index {
            Ok(index) => {
                for i in (1..index+1).rev() {
                    if self[i] != self[i - 1] {
                        return Ok(i - 1)
                    }
                }
                
                Ok(index)
            },
            _ => index,
        }
    }
}