Oree's White Van
You've discovered a magical candy van that has a single, long display of candies in a row, found in the back of Oree's van. Each candy has a "flavour value" which can be a positive (sweet), negative (sour), or zero (tasteless).
Oree, a friendly wizard, offers you a special deal. You can choose any continuous section of candies from a starting position to an ending position
. The total flavour of your chosen section is the sum of the flavour values of all the candies from
to
.
However, Oree adds a little magic bonus! If the total flavour of your chosen section is positive, you get an extra bonus flavour points. If the total flavour is negative, you get a
point penalty (subtracting
from your total). If the total flavour is exactly zero, Oree will be so impressed that he gives you a
point bonus. You'll also get to ride in the back of his van as he gives you some of his special candies!
You are given different sections that you're interested in. For each section, your task is to calculate the final flavour value, including Oree's magical bonus.
Input
The first line contains an integer
, the number of candies that Oree has in his van.
The second line contains space-separated integers
, where
is the flavour value of the
-th candy.
The third line contains an integer
, the number of sections you want to evaluate.
The next lines each contain two integers
and
, representing the start and end indices of a section for each query.
Output
For each query, print the final flavour value of the section after the magical bonus is applied
Example
Input 1
5
-5 3 1 1 12
3
0 2
0 3
0 4
Output 1
-11
100
22
- For the first section you add
,
and
which results in
. You then also have a
-point deduction so you output
.
- For the second section, you calculate that
. You then get the big bonus from Oree and he awards you
points. So you print out
.
- For the third section, you calculate that
. You then get a
-point award so your total becomes
.
Comments