Zulu and Games

3.9

8 votes
Problem

Zulu loves to play games. Once he started playing a very unusual game. The game consists of N levels where each level has two parameters Li & Hi which denotes the lowest and highest energy possible for the ith level of the game respectively. Now before the start of the game, Zulu has the opportunity to increase his energy which is by default 0. There is a trick called Magic Combo through which Zulu can increase his energy. In Magic Combo, Zulu is allowed to choose several levels as long as their energy levels are non-intersecting. Along with that, after he has finished creating Magic Combo, there should not be any levels left which are not intersecting with the current set of chosen levels and are not picked.

Now, the energy gained after creating a valid Magic Combo is the maximum possible energy amongst all the selected energy levels. As Zulu is very clever, he figured out that he can create many Magic Combos. Can you tell the total possible energy which can be achieved by Zulu after creating all possible Magic Combos? As the total energy value can be very high, return total energy modulus 1000000007 instead.

Note that two Magic Combos are different if there is at least one level which is different among them. Energy intersection occurs between two levels if they both contain at least one common energy number.

Input:

First line contain an integer N denoting the number of levels.

Second line contains N space separated integers denoting the L array where Li denotes the lowest energy possible for the ith level.

Last line contains N space separated integers denoting the H array where Hi denotes the highest energy possible for the ith level.

Output:

Output in a single line the maximum energy possible modulus 1000000007.

Constraints:

1N5000

1Li,Hi109

LiHi

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Zulu can make 2 magic combos by using combination of level 1,3 and level 2,3. Since, both combos give 12 as the maximum possible energy value, hence answer is 24.

Editor Image

?