The following languages are only available languages for the problems from the contest

Kotlin Heroes: Episode 6:

- Kotlin 1.4.0

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

The problem statement has recently been changed. View the changes.

×
F. Dogecoin

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputRecently, cryptocurrencies have become increasingly popular. Ann decided that it was about time she started mining the Dogecoin cryptocurrency. Ann's computer is not very powerful, so Ann mines exactly $$$1$$$ Dogecoin per day. On the Internet, there are forecasts of the cost of $$$1$$$ dogecoin for the next $$$n$$$ days. Every day, Ann can sell any amount of dogecoin that she currently has. Note that if Ann mined Dogecoin on the $$$i$$$-th day, then she can sell it on the same day.

Ann has not yet decided when to start mining. She has prepared $$$q$$$ possible plans and for each of them wants to know the maximum profit that she can get. Each of the plans is described by two numbers $$$l$$$ and $$$r$$$ — the first and last days of mining. Note that Ann should not have any Dogecoin left after the $$$r$$$-th day.

Input

The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^6$$$), where $$$c_i$$$ is the cost of Dogecoin on the $$$i$$$-th day.

The third line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of Ann's plans.

The following $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the first and last days of mining.

Output

For each Ann's plan print one integer — the maximum profit that she can get using this plan.

Example

Input

5 4 1 2 3 2 4 1 5 2 4 3 5 5 5

Output

15 9 8 2

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/15/2021 03:02:56 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|