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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education