Zulu and Games
Practice
3.9 (8 votes)
Algorithms
Dynamic programming
Medium
Problem
26% Success 1776 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(L_i\) & \(H_i\) which denotes the lowest and highest energy possible for the \(i^{th}\) 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 \(L_i\) denotes the lowest energy possible for the \(i^{th}\) level.

Last line contains N space separated integers denoting the H array where \(H_i\) denotes the highest energy possible for the \(i^{th}\) level.

Output:

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

Constraints:

\(1 \le N \le 5000\)

\(1 \le L_i, H_i \le 10^9\)

\(L_i \le H_i \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
AlgorithmsDynamic ProgrammingMediumStacksString Manipulation
Points:30
4 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen
Points:30
24 votes
Tags:
ApprovedMathMediumOpen