Polish version    English version  
  History of OI -> IV OI 1996/1997 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
IV Olimpiada Informatyczna 1996/1997

Task: REZ
Author: Wojciech Guzicki
Lecture halls reservation

III stage contest  
Source fileREZ.??? (e.g. PAS,C, CPP)
Executable fileREZ.EXE
Input fileREZ.IN
Output fileREZ.OUT

We have a lecture hall at our disposal. Lecturers place orders in which they specify at what time their lecture begins and at what time it ends. We want to make maximal use of the hall. We accept some orders and reject others and our aim is to achieve the longest possible time of lectures in the hall. We assume that at the moment when one lecture ends another can begin.

Task

Write a program that:
  • reads orders of lecturers from the text file REZ.IN,
  • computes the maximal possible time of lectures,
  • writes the result to the file REZ.OUT.

Input

In the first line of the text file REZ.IN there is written one positive integer n, n <= 10000, which is the number of orders.

In each of the following n lines there are written two integers p and k, separated by a single space, 0 <= p < k <= 30000. Such a pair of integers represents an order for the hall. The lecture is to begin at time p and end in time k.

Output

In the first line of the text file REZ.OUT there should be written the maximal possible time of lectures.

Example

For the text file REZ.IN:
12
1 2
3 5
0 4
6 8
7 13
4 6
9 10
9 12
11 14
15 19
14 16
18 20
the correct result is the text file REZ.OUT
16





Print friendly version