Kiloforces

Time limit: 2s Memory limit: 512MB Input: Output:

Vadim is a very talented and passionate young man about computer science. Filled with the Christmas spirit, he decides to spend his winter vacation participating in as many contests as possible on his favorite platform, Kiloforces\text{Kiloforces}. There, the scoring system is a bit different. If the contest has KK problems, with the ii-th problem worth pip_i points, then when he solves problem jj, pjp_j points are added to his total score, and tjt_j points are subtracted from the scores of the unsolved problems. The platform rules allow for there to be problems with negative scores at any given time (which Vadim will need to solve).

Task

Vadim receives NN problems, knowing the values pip_i and tit_i for each. Over the next QQ days, he chooses an interval [l,r][l, r] and wonders what the maximum score he can achieve is if he participates in a contest consisting of problems l,l+1,l+2,,r1,rl, l+1, l+2, \dots, r-1, r. Help Vadim answer these QQ questions, or else you will end up on Santa's naughty list.

Input data

The first line contains NN and QQ. The next 2 lines will each contain NN values (pip_i and tit_i), and the last QQ lines contain the questions posed by Vadim.

Output data

Each of the next QQ lines will contain the answers to Vadim's questions.

Constraints and clarifications

  • 1N,Q,ti1051 \leq N,Q, t_i \leq 10^5
  • This link might be helpful in solving the problem.
  • 1pi1091 \leq p_i \leq 10^9
  • The values in array tt are pairwise distinct!!
# Points Constraints
1 11 1N1001 \leq N \leq 100
2 9 1N1 0001 \leq N \leq 1 \ 000
3 12 1N10 0001 \leq N \leq 10 \ 000
4 10 For each question, the optimal order in which Vadim solves the problems is given, in other words, starting from problem ll and ending with rr.
5 20 1N50 0001 \leq N \leq 50 \ 000
6 38 No additional constraints

Example 1

stdin

4 3
12 5 4 10
6 7 1 2
1 3
2 4
3 4

stdout

13
15
13

Example 2

stdin

5 5
2 4 6 5 9 
4 9 5 2 8 
2 5
1 2
2 3
2 2
3 3

stdout

0
2
5

Log in or sign up to be able to send submissions!