#P8551. Maximizing Subinterval Score

    ID: 21718 Type: Default 1000ms 256MiB

Maximizing Subinterval Score

Maximizing Subinterval Score

Given n intervals, where the i-th interval is represented as \([l_i, r_i]\), we define the interval \([l,r]\) as the set of all integers \(x\) such that \(l \le x \le r\). For example, \([3,3]\) represents \(\{3\}\) and \([3,7]\) represents \(\{3,4,5,6,7\}\).

You are required to select two integers \(x\) and \(y\) with \(x \le y\) such that for every given interval \([l_i, r_i]\) (\(1 \le i \le n\)), one of the following conditions holds:

  1. Either \([x,y]\) is completely contained within \([l_i, r_i]\), i.e. \([x,y] \subseteq [l_i, r_i]\) (formally, \([x,y] \cap [l_i, r_i] = [x,y]\));
  2. or \([x,y]\) and \([l_i, r_i]\) have no intersection, i.e. \([x,y] \cap [l_i, r_i] = \varnothing\).

If there are \(k\) intervals that contain \([x,y]\) (i.e. satisfy condition 1), your score is \(k\times (y-x)\). Find and output the maximum possible score achievable.

inputFormat

The first line contains one integer \(n\), the number of intervals.

Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) representing an interval \([l_i, r_i]\).

outputFormat

Output one integer: the maximum score that can be achieved.

sample

3
1 5
2 3
6 10
2