Petya found out with regards to another game "Kill the Dragon". As the name recommends, the player should battle with mythical beasts. To overcome a mythical serpent, you need to kill it and shield your palace. To do this, the player has a crew of n legends, the strength of the I-th saint is equivalent to man-made intelligence. As per the principles of the game, precisely one saint should go kill the mythical serpent, all the others will shield the
Correct answer will upvoted else downvoted
Petya found out with regards to another game "Kill the Dragon". As the name recommends, the player should battle with mythical beasts. To overcome a mythical serpent, you need to kill it and shield your palace. To do this, the player has a crew of n legends, the strength of the I-th saint is equivalent to man-made intelligence. As per the principles of the game, precisely one saint should go kill the mythical serpent, all the others will shield the palace. On the off chance that the mythical serpent's protection is equivalent to x, you need to send a saint with a strength of essentially x to kill it. Assuming the winged serpent's assault power is y, the absolute strength of the saints protecting the palace ought to be ssentially y. The player can build the strength of any legend by 1 for one gold coin. This activity should be possible quite a few times.
There are m winged serpents in the game, the I-th of them has protection equivalent to xi and assault power equivalent to yi. Petya was considering what is the base number of coins he needs to spend to overcome the I-th mythical beast. Note that the assignment is addressed autonomously for every winged serpent (upgrades are not saved).
Input :The main line contains a solitary integer n (2≤n≤2⋅105) — number of legends.
The subsequent line contains n integers a1,a2,… ,an (1≤ai≤1012), where
The third line contains a solitary integer m (1≤m≤2⋅105) — the number of winged serpents. The following m lines contain two integers every, xi and yi (1≤xi≤1012;1≤yi≤1018) — guard and assault force of the I-th mythical serpent.
Output :Print m lines, I-th of which contains a solitary integer — the base number of coins that ought to be spent to overcome the I-th winged serpent.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images