#K1046. Calculate Overlap Time

    ID: 23251 Type: Default 1000ms 256MiB

Calculate Overlap Time

Calculate Overlap Time

Given a list of time intervals representing the on and off times for multiple devices, your task is to calculate the total time duration during which all devices are simultaneously on.

Each time interval is represented as a tuple \((t_{on}, t_{off})\). The overall overlap time is computed as \(\max(0, \min_{i}(t_{off}^{(i)}) - \max_{i}(t_{on}^{(i)}) )\). If there is no interval where all devices are on simultaneously, output 0.

inputFormat

Input is read from standard input. The first line contains an integer (n) representing the number of devices. Each of the next (n) lines contains two space-separated integers representing the turn on time and turn off time of a device. Note that (n) may be zero.

outputFormat

Output a single integer to standard output, denoting the total overlap time during which all devices are simultaneously on.## sample

3
1 5
2 6
4 8
1

</p>