Fair Photography

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John's NN cows (1N100,0001 \le N \le 100{,}000) stand at distinct positions xix_i (0xi1090 \le x_i \le 10^9) along a long fence. Cow ii has breed bib_i, either Guernsey G or Holstein H.

FJ wants a photo of a contiguous group of cows along the sorted line. Every breed that appears in the photo must appear the same number of times. A photo with only one breed is allowed.

Find the maximum size of a valid photo. The size is the difference between the largest and smallest positions among cows in the photo. A photo of a single cow has size 00.

Input

Line 1 contains NN. Each of the next NN lines contains xix_i and bib_i.

Output

Print one integer, the maximum photo size.

Hint

After sorting by position, map G to +1+1 and H to 1-1. Equal prefix sums mark equal breed counts between two indices. Any contiguous block with only one breed is also valid. Take the maximum span from both cases.