eolymp
bolt
Try our new interface for solving problems
Problems

It’s Time for a Montage

It’s Time for a Montage

The heroes of your favorite action TV show are preparing for the final confrontation with the villains. Fundamentally, there are two rivals who will fight each other: a very important main hero who wants to save the universe and an equally important main villain who wants to destroy it. However, through countless recursive spin-offs, they may have slightly less important sidekicks (a hero and a villain who are rivals themselves), who in turn may also have their own (even less important) sidekicks, and so on. Note that there is an equal number of heroes and villains, and each rival pair has at most one sidekick pair. Initially, every character will fight their rival, with the winner being determined by who has the higher Power Level. If a hero and their corresponding villain have the same Power Level, their battle will be determined by their sidekicks’ battle, as the winning sidekick can help as a sort of tiebreaker (If rivals of equal Power Level do not have sidekicks, the hero character will win with the help of random passersby). However, whenever a battle is won by either side, there is nothing the sidekicks can do about it --- this is because the people behind the show believe some fans might get upset if a character were to get defeated by a bunch of less important characters, so they would lose regardless of the Power Levels. After the battles between rivals (and possible tiebreakers) are done, the most important character remaining will defeat the rest of the opposing side and determine the fate of the universe. Fortunately, the heroes can ensure victory through hard, rigorous training. For each day they spend training, the Power Level of each hero increases by $1$, while the villains’ Power Levels remain constant. But you already knew all this. The question plaguing your mind is how long the training is going to take. \InputFile The first line contains the number of rival pairs $n~(1 \le n \le 1000)$. The second line contains $n$ integers $h_1, ..., h_n~(1 \le h_i \le 1000)$. Here $h_i$ is the Power Level of the $i$-th most important hero. The third line contains $n$ integers $v_1, ..., v_n~(1 \le v_i \le 1000)$. Here $v_i$ is the Power Level of the $i$-th most important villain. \OutputFile Output the minimum number of days the heroes need to spend training in order for their side to win.
Time limit 1 second
Memory limit 128 MiB
Input example #1
4
5 3 1 1
8 6 9 1
Output example #1
4
Input example #2
1
2
1
Output example #2
0
Input example #3
2
4 2
7 5
Output example #3
3
Source 2018 German Collegiate Programming Contest (GCPC), Problem I