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

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter17: Markov Chains
Section: Chapter Questions
Problem 12RP
icon
Related questions
Question

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 artificial intelligence is the strength of the I-th legend. 

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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole