PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000074. Tham dự nhiều sự kiện nhất (Maximum Number of Events That Can Be Attended)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tham dự nhiều sự kiện nhất (Maximum Number of Events That Can Be Attended)

Maximum Number of Events That Can Be Attended

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn events. Event ii may be attended on one chosen day in the inclusive range [si,ei][s_i,e_i]. You may attend at most one event per day and count each event at most once. Find the maximum number of events that can be attended.

Input

The first line contains integer nn. Each of the next nn lines contains two integers si,eis_i,e_i.

Output

Print the maximum number of events.

Subtasks

  • Subtask 1 (20 points): 1≤n≤241\le n\le 24; 1≤si≤ei≤1051\le s_i\le e_i\le10^5 for every ii.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤si≤ei≤1051\le s_i\le e_i\le10^5 for every ii.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤si≤ei≤1051\le s_i\le e_i\le10^5 for every ii.

Examples

Input

3
1 2
2 3
3 4

Output

3

Explanation

Attend event [1,2][1,2] on day 11, [2,3][2,3] on day 22, and [3,4][3,4] on day 33. Each event is attended on one allowed day, so all three can be attended.