Beauty of the sequence

Given N trees with independent uniform height ranges, find the expected maximum beauty of a zigzag subsequence.

Hard8Dynamic programmingProbabilityNo attempts yetTime limit2sMemory limit512 MB

Problem

Yeongseon planted NN trees in a row. The trees are numbered 00 through N1N-1. When tree ii is fully grown its height is an integer between lowilow_i and highihigh_i, inclusive, and every integer in that range is equally likely. The heights of different trees are chosen independently.

Yeongseon likes zigzag sequences. A zigzag sequence is defined as follows.

  • Every sequence of length 11 is a zigzag sequence.
  • A sequence (A,B)(A, B) of length 22 is a zigzag sequence if ABA \ne B.
  • A sequence (A,B,C)(A, B, C) of length 33 is a zigzag sequence if A<BA < B and B>CB > C, or if A>BA > B and B<CB < C.
  • A sequence (A0,A1,,AL1)(A_0, A_1, \dots, A_{L-1}) of length L>3L > 3 is a zigzag sequence if each of the triples (A0,A1,A2)(A_0, A_1, A_2), (A1,A2,A3)(A_1, A_2, A_3), \dots, (AL3,AL2,AL1)(A_{L-3}, A_{L-2}, A_{L-1}) is a zigzag sequence.

The beauty of a sequence is the sum of the absolute differences of adjacent elements, so the beauty of (A0,A1,,AL1)(A_0, A_1, \dots, A_{L-1}) is A0A1+A1A2++AL2AL1|A_0 - A_1| + |A_1 - A_2| + \dots + |A_{L-2} - A_{L-1}|. A sequence of length 11 has beauty 00.

Once the trees are fully grown, Yeongseon writes down the heights of trees 00 through N1N-1 in order. If the written sequence is a zigzag sequence she leaves it alone. Otherwise she erases some of the numbers so that what remains is a zigzag sequence. The remaining numbers keep their original order. When several ways of erasing produce a zigzag sequence, she picks one whose beauty is largest. The sequence she ends up with is called the result sequence.

Write a program that computes the expected beauty of the result sequence.

Input

The first line contains the number of trees NN. (1N501 \le N \le 50)

Each of the next NN lines contains lowilow_i and highihigh_i for tree ii, separated by a space. (1lowihighi1000001 \le low_i \le high_i \le 100000)

Output

Print the expected beauty of the result sequence with exactly six digits after the decimal point. Round at the seventh digit after the decimal point, rounding a half up, and pad with zeros so that six digits are always present. For example, if the answer is 88, print 8.000000.

Hint

Suppose there are 33 trees and all three height ranges are 11 to 22. Then there are 88 possible outcomes.

  • (1,1,1)(1, 1, 1) and (2,2,2)(2, 2, 2) need two numbers erased before they become zigzag sequences, and the beauty is 00.
  • (1,1,2)(1, 1, 2), (2,2,1)(2, 2, 1), (1,2,2)(1, 2, 2) and (2,1,1)(2, 1, 1) only need the middle number erased, and the beauty is 11.
  • (1,2,1)(1, 2, 1) and (2,1,2)(2, 1, 2) need nothing erased, and the beauty is 22.

The expected value is therefore 48×1+28×2=1\frac{4}{8} \times 1 + \frac{2}{8} \times 2 = 1.