#P1178. Longest Free Time in the Celestial Palace

    ID: 13875 Type: Default 1000ms 256MiB

Longest Free Time in the Celestial Palace

Longest Free Time in the Celestial Palace

One day, I had a dream. In this dream, I was honored to receive an invitation from Zhu Bajie to accompany him for a feast in the Celestial Palace. However, I hesitated because time flows differently in heaven and on earth: one day in heaven is equivalent to one year on earth. Since I am a free spirit and only a few days of the year are absolutely required to be spent on earth, I want to choose the longest continuous free period during which I am not obligated on earth, so that my time in heaven is maximized.

Note that this year is $4000$ years long. The busy (obligated) time intervals on earth are provided as input, and these intervals may overlap. The entire time span is from year 1 to year 4000 (inclusive). Each busy interval is given by two integers indicating the starting and ending years (both inclusive) during which you must be on earth.

Your task is to compute the maximum free period (in years) available. Then, using the fact that one free year on earth translates to one day in heaven and that one day in heaven corresponds to 24 \times 60 \times 60 = 86400 seconds, output the maximum free time in seconds.

Input begins with an integer n denoting the number of busy intervals, followed by n lines each with two integers start and end (1 ≤ start ≤ end ≤ 4000). If n is 0, there are no busy intervals and the whole period is free.

Note: Busy intervals may overlap. You must merge them first before determining the free time segments.

inputFormat

The first line contains an integer n indicating the number of busy intervals.

Each of the next n lines contains two space-separated integers start and end (both inclusive), indicating a busy interval within the years 1 to 4000.

outputFormat

Output a single integer, the maximum free time in seconds that you can enjoy in the Celestial Palace.

sample

0
345600000