#144Medium
Movie festival
Time: 1 s · Memory: 256 MB
- Sorting
Statement
movies are shown at a festival; movie starts at time and ends at time . You want to watch whole movies and can watch only one movie at a time. A movie that starts exactly when another one ends can be watched. What is the maximum number of movies you can watch entirely?
Input format
The first line contains (). The next lines contain and ().
Output format
A single number — the maximum number of movies.
Examples
Input
3 3 5 4 9 5 8
Output
2
Note
Sort the movies by end time and choose greedily: take every movie that starts no earlier than the end of the last chosen one. The movie that ends first leaves the most time for the rest.
Choosing by start time or by duration can give a wrong answer. In the example and are chosen — the answer is .
Source: CSES, problem 1629.
