#C11888. Smallest Overlapping Segment

    ID: 41253 Type: Default 1000ms 256MiB

Smallest Overlapping Segment

Smallest Overlapping Segment

You are given \(N\) segments on a number line. Each segment is represented by two integers \(a_i\) and \(b_i\) indicating the start and end of the segment. Your task is to determine the smallest segment that lies in the intersection of all the given segments.

More formally, let the segments be \([a_1, b_1], [a_2, b_2], \dots, [a_N, b_N]\). Compute \(S = \max\{a_1, a_2, \dots, a_N\}\) and \(E = \min\{b_1, b_2, \dots, b_N\}\). If \(S \le E\), then the overlapping segment exists and its boundaries are \(S\) and \(E\); otherwise, output \(-1\) for both values to indicate that no common overlapping segment exists.

inputFormat

The input is read from stdin and has the following format:

N
a1 b1
a2 b2
... 
aN bN

Where \(N\) is the number of segments, and each of the next \(N\) lines contains two space-separated integers representing the start and end of a segment.

outputFormat

Output to stdout a single line with two space-separated integers. If there is an overlapping segment among all segments, output its start and end. Otherwise, output "-1 -1".

## sample
3
1 5
2 6
4 7
4 5