Post Office
시간 제한2초메모리 제한2048 MB
각 우체국이 한 번에 패키지 하나만 보내는 함수형 그래프에서 모든 패키지를 목적지로 보낼 수 있는지 판정하고, 마지막 도착 시간의 최솟값을 구한다.
문제
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.
제한
- .
- .
- ().
- ().
- ().
- Given values are all integers.