Post Office
문제
In the JOI country, there are post offices, numbered from to . Each post office has an assigned ”destination,” and the destination of post office is post office . Note that it is possible for . If a package is sent from post office at time , it will arrive at post office at time . However, a post office cannot send another package while it is in the process of sending a package. Each post office can store an unlimited number of packages at any given time.
Now, packages need to be delivered in the JOI country. The -th package arrives at post office at time , and it must eventually be delivered to the assigned post office . Given the information about the post offices and the packages, write a program to determine whether it is possible to deliver all the packages to their assigned post offices, and if so, find the smallest possible time at which the last package arrives at its assigned post office.
입력
Read the following data from the standard input.
출력
Output a single line to the standard output. If it is possible to deliver all the packages to their assigned post offices, output the smallest possible time at which the last package arrives at its assigned post office. Otherwise, output -1 instead.
예제
예제 1
5 1 1 2 3 4 3 3 2 3 1 3 1
3
예제 2
3 2 1 3 1 1 3
-1
예제 3
7 1 1 2 3 4 5 6 6 4 2 5 1 5 3 6 2 7 3 7 6
5
예제 4
4 4 1 2 3 4 4 1 4 1 2 3 2 3
4
예제 5
7 1 1 1 3 3 4 4 5 6 1 6 3 7 1 5 1 5 1
5
예제 6
11 3 1 2 5 6 7 8 4 4 5 10 6 2 1 9 8 11 8 10 4 5 6 5 7
6