Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.
According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student’s record book on the day of the actual exam and write down the date of the mark as number ai.
Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.
Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.
Output
Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.
Sample Input
Input
3
5 2
3 1
4 2
Output
2
Input
3
6 1
5 2
4 3
Output
6
有n场考试,这个学生可以在规定时间之前进行考试,但是不可以改变原来的考试顺序。运算符重载写一个sort,对原来时间排序,然后判断现在的提前时间是否大于之前的时间,如果能,就用之前时间替代,否则就用原来时间。
include
include
include
include
using namespace std;
struct Exam{
int a;
int b;
}exam[5005];
bool cmp(Exam& a, Exam& b)
{
if (a.a == b.a)return a.b < b.b;
else return a.a < b.a;
}
int main()
{
int i, j, m, n, ans;
cin >> n;
for (i = 1; i <= n; i++)
cin >> exam[i].a >> exam[i].b;
sort(exam + 1, exam + 1 + n, cmp);
ans = exam[1].b;
for (i = 2; i <= n; i++)
{
if (exam[i].b >= ans)
ans = exam[i].b;
else ans = exam[i].a;
}
cout << ans << endl;
return 0;
}