Problem:
Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
	static ArrayList<Integer> xs, ys;
	static int x, y;
	static Boolean[][] mem1, mem2;
	static int offset = 8001;

	static boolean solve(int idx, int sum) {
		if (idx == xs.size())
			return sum == x;
		if (mem1[idx][sum + offset] != null)
			return mem1[idx][sum + offset];
		return mem1[idx][sum + offset] = solve(idx + 1, sum + xs.get(idx)) || solve(idx + 1, sum - xs.get(idx));
	}

	static boolean solve2(int idx, int sum) {
		if (idx == ys.size())
			return sum == y;
		if (mem2[idx][sum + offset] != null)
			return mem2[idx][sum + offset];
		return mem2[idx][sum + offset] = solve2(idx + 1, sum + ys.get(idx)) || solve2(idx + 1, sum - ys.get(idx));
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		x = sc.nextInt();
		y = sc.nextInt();

		xs = new ArrayList<>();
		ys = new ArrayList<>();

		int start = 0;

		while (start < s.length() && s.charAt(start) == 'F') {
			x--;
			start++;
		}

		boolean inX = true;
		for (int i = start; i < s.length(); i++) {
			if (s.charAt(i) == 'T') {
				inX = !inX;
			}
			else {
				int cnt = 0;
				boolean in = false;
				while (i < s.length() && s.charAt(i) == 'F') {
					cnt++;
					i++;
					in = true;
				}
				if (in)
					i--;
				if (inX)
					xs.add(cnt);
				else
					ys.add(cnt);
			}
		}
		mem1 = new Boolean[xs.size() + 1][8002 * 2];
		mem2 = new Boolean[ys.size() + 1][8002 * 2];
		if (solve(0, 0) && solve2(0, 0))
			System.out.println("Yes");
		else
			System.out.println("No");

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}
	}
}