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: Accepted)

fn main() {
	let mut s = String::new();
	use std::io::Read;
	std::io::stdin().read_to_string(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: usize = s.next().unwrap().parse().unwrap();
	let a: Vec<i32> = s.take(n).map(|a| a.parse().unwrap()).collect();
	let mut b = a.clone();
	b.sort();
	b.dedup();
	let mut bit = BIT::new(b.len());
	let mut ans = 0;
	for a in a.iter() {
		let i = b.binary_search(a).unwrap();
		let s = bit.sum(i);
		if s > 0 {
			let j = bit.lower_bound(&s);
			bit.add(j, -1);
		} else {
			ans += 1;
		}
		bit.add(i, 1);
	}
	println!("{}", ans);
}

pub struct BIT<T> {
	data: Vec<T>,
}

impl<T> BIT<T>
where
	T: Clone + Default + std::ops::Add<Output = T>,
{
	pub fn new(n: usize) -> Self {
		Self {
			data: vec![T::default(); n],
		}
	}

	pub fn construct(a: Vec<T>) -> Self {
		let mut a = a;
		for i in 0..a.len() {
			if i | (i + 1) >= a.len() {
				continue;
			}
			let v = a[i].clone();
			a[i | (i + 1)] = a[i | (i + 1)].clone() + v;
		}
		Self { data: a }
	}

	pub fn sum(&self, i: usize) -> T {
		let mut r = T::default();
		let mut i = i;
		while i > 0 {
			r = r + self.data[i - 1].clone();
			i &= i - 1;
		}
		r
	}

	pub fn add(&mut self, i: usize, value: T) {
		let mut i = i;
		while i < self.data.len() {
			self.data[i] = self.data[i].clone() + value.clone();
			i |= i + 1;
		}
	}

	pub fn lower_bound(&self, value: &T) -> usize
	where
		T: Ord + std::ops::Add<Output = T>,
	{
		let mut s = T::default();
		let msb = 8 * std::mem::size_of::<usize>() - self.data.len().leading_zeros() as usize;
		let mut len = 1 << msb;
		let mut l = 0;
		while len > 0 {
			if let Some(a) = self.data.get(l + len - 1) {
				let t = s.clone() + a.clone();
				match t.cmp(&value) {
					std::cmp::Ordering::Less => {
						l += len;
						s = t;
					}
					_ => (),
				}
			}
			len >>= 1;
		}
		l
	}
}