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.42.0) (judge verdict: Wrong Answer)

use proconio::{fastout, input};
use std::io::*;
use std::str::FromStr;

use std::cmp::{max, min};

fn main() {
    input! {
        n: usize,
        a: [i64; n],
    }
    let mut bt = BinaryTree::new();
    let mut cnt = 0;

    for i in (0..n).rev() {
        let ai = a[i];
        if bt.max() == None {
            bt.insert(ai);
            cnt += 1;
            continue;
        }
        if bt.max().unwrap() <= ai {
            bt.insert(ai);
            cnt += 1;
            continue;
        } else {
            // println!("max:{}, ai:{}", bt.max().unwrap(), ai);
            // println!("bt:{:?}", bt);
            let curr_top = bt.min_more_than(ai).unwrap();
            bt.remove(curr_top);
            bt.insert(ai);
        }
    }
    println!("{}", cnt);
}

use std::fmt::Debug;

type Link<T> = Option<Box<Node<T>>>;

#[derive(Debug, PartialEq, Eq, Clone)]
struct Node<T> {
    value: T,
    left: Link<T>,
    right: Link<T>,
}

fn delete_link<T: Ord + Copy + Debug>(link: &mut Link<T>, value: T) {
    match link {
        Some(s) if s.value == value && s.has_no_child() => *link = None,
        Some(s) if s.value == value && s.has_both_child() => {
            let maxv = s.max();
            s.delete(maxv);
            s.value = maxv;
        }
        Some(s) if s.value == value && s.has_right_child() => {
            *link = s.right.clone();
        }
        Some(s) if s.value == value && s.has_left_child() => {
            *link = s.left.clone();
        }
        Some(s) => s.remove(value),
        None => (),
    }
}

fn insert_link<T: Ord + Copy + Debug>(link: &mut Link<T>, value: T) {
    match link {
        Some(s) => s.insert(value),
        None => {
            let new_node = Node {
                value: value,
                right: None,
                left: None,
            };
            *link = Some(Box::new(new_node));
        }
    }
}

impl<T: Ord + Copy + Debug> Node<T> {
    fn next_node(&mut self, value: T) -> &mut Link<T> {
        if value < self.value {
            &mut self.left
        } else {
            &mut self.right
        }
    }
    fn insert(&mut self, value: T) {
        if self.value == value {
            return;
        }
        let nn = self.next_node(value);

        insert_link(nn, value);
    }

    fn remove(&mut self, value: T) {
        let nn = self.next_node(value);
        delete_link(nn, value);
    }

    fn delete(&mut self, value: T) {
        let nn = self.next_node(value);

        match nn {
            Some(s) if s.value == value => *nn = None,
            Some(s) => s.delete(value),
            None => (),
        }
    }

    fn has_both_child(&self) -> bool {
        return self.has_left_child() && self.has_right_child();
    }

    fn has_no_child(&self) -> bool {
        return self.right.is_none() && self.left.is_none();
    }

    fn has_left_child(&self) -> bool {
        return !self.left.is_none();
    }

    fn has_right_child(&self) -> bool {
        return !self.right.is_none();
    }

    fn max(&self) -> T {
        let nn = &self.right;
        match nn {
            Some(s) => s.max(),
            None => self.value,
        }
    }
    fn min(&self) -> T {
        let nn = &self.left;
        match nn {
            Some(s) => s.min(),
            None => self.value,
        }
    }

    fn max_less_than_and_equal(&self, n: T) -> Option<T> {
        if self.value > n {
            let nn = &self.left;
            return match nn {
                Some(s) => s.max_less_than_and_equal(n),
                _ => None,
            };
        }
        let nn = &self.right;
        match nn {
            Some(s) if s.value <= n => s.max_less_than_and_equal(n),
            _ => Some(self.value),
        }
    }
    fn max_less_than(&self, n: T, parent: Option<T>) -> Option<T> {
        if self.value >= n {
            let nn = &self.left;
            return match nn {
                Some(s) => s.max_less_than(n, Some(self.value)),
                _ => parent,
            };
        }
        let nn = &self.right;
        match nn {
            Some(s) if s.value <= n => s.max_less_than(n, Some(self.value)),
            _ => Some(self.value),
        }
    }
    fn min_more_than_and_equal(&self, n: T) -> Option<T> {
        if self.value < n {
            let nn = &self.right;
            return match nn {
                Some(s) => s.min_more_than_and_equal(n),
                _ => None,
            };
        }
        let nn = &self.left;
        match nn {
            Some(s) if s.value >= n => s.min_more_than_and_equal(n),
            _ => Some(self.value),
        }
    }
    fn min_more_than(&self, n: T, parent: Option<T>) -> Option<T> {
        if self.value <= n {
            let nn = &self.right;
            return match nn {
                Some(s) => s.min_more_than(n, Some(self.value)),
                _ => parent,
            };
        }
        let nn = &self.left;
        match nn {
            Some(s) if s.value >= n => s.min_more_than(n, Some(self.value)),
            _ => Some(self.value),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
struct BinaryTree<T> {
    root: Link<T>,
}

impl<T: Ord + Copy + Debug> BinaryTree<T> {
    fn new() -> Self {
        BinaryTree { root: None }
    }

    fn insert(&mut self, value: T) {
        insert_link(&mut self.root, value)
    }

    fn remove(&mut self, value: T) {
        delete_link(&mut self.root, value)
    }

    fn max(&self) -> Option<T> {
        match &self.root {
            Some(s) => Some(s.max()),
            None => None,
        }
    }
    fn min(&self) -> Option<T> {
        match &self.root {
            Some(s) => Some(s.min()),
            None => None,
        }
    }

    fn max_less_than_and_equal(&self, n: T) -> Option<T> {
        match &self.root {
            Some(s) => s.max_less_than_and_equal(n),
            None => None,
        }
    }

    fn max_less_than(&self, n: T) -> Option<T> {
        match &self.root {
            Some(s) => s.max_less_than(n, None),
            None => None,
        }
    }
    fn min_more_than_and_equal(&self, n: T) -> Option<T> {
        match &self.root {
            Some(s) => s.min_more_than_and_equal(n),
            None => None,
        }
    }
    fn min_more_than(&self, n: T) -> Option<T> {
        match &self.root {
            Some(s) => s.min_more_than(n, None),
            None => None,
        }
    }
}