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)

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 = RangeBIT::new(b.len());
	let mut ans = 0;
	for a in a.iter() {
		let i = b.binary_search(a).unwrap();
		if bit.query(i + 1) - bit.query(i) == 0 {
			bit.range_add(i + 1, n, 1);
			ans += 1;
		} else {
			bit.range_add(i, i + 1, -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 struct RangeBIT<T> {
	data0: BIT<T>,
	data1: BIT<T>,
}

impl<T> RangeBIT<T>
where
	T: Clone
		+ Default
		+ std::ops::Add<Output = T>
		+ std::ops::Sub<Output = T>
		+ std::ops::Mul<Output = T>
		+ std::ops::Neg<Output = T>
		+ std::convert::TryFrom<usize>,
	<T as std::convert::TryFrom<usize>>::Error: std::fmt::Debug,
{
	pub fn new(n: usize) -> Self {
		Self {
			data0: BIT::new(n),
			data1: BIT::new(n),
		}
	}

	pub fn range_add(&mut self, l: usize, r: usize, value: T) {
		self.data0.add(l, -value.clone() * T::try_from(l).unwrap());
		self.data0.add(r, value.clone() * T::try_from(r).unwrap());
		self.data1.add(l, value.clone());
		self.data1.add(r, -value);
	}

	pub fn range_sum(&self, l: usize, r: usize) -> T {
		self.query(r) - self.query(l)
	}

	pub fn query(&self, r: usize) -> T {
		self.data0.sum(r) + T::try_from(r).unwrap() * self.data1.sum(r)
	}
}