Farming Sesh

Time limit: 4.5s Memory limit: 1024MB Input: Output:

Erin loves farming PP (performance points) in osu!, and he wants to maximize his gains. He has an assortment of NN beatmaps which he has played before, numbered from 00 to N1N-1, where each beatmap has an assigned PP value. He also has MM other beatmaps that he hasn't played yet, which are likewise numbered from 00 to M1M-1 and each one of the N+MN+M beatmaps has an assigned PP value.

Erin playing osu!.\text{Erin playing osu!}.

Task

In order to quench his addiction, he decides to take a look at QQ scenarios of the type: "What if my top plays are only made up of the already played maps from [l1,r1][l_1, r_1], and I can take any number, KK, of unplayed maps from [l2,r2][l_2, r_2], and overwrite any KK of my top plays; what is the maximum possible PP sum that I can obtain, if I choose KK and the KK beatmaps optimally?"

For each of these scenarios, you have to find the maximum PP sum that can be obtained, choosing KK optimally.

Input data

The first line contains the space-separated integers NN, MM and QQ.
The second line contains NN space-separated integers, A0,A1,,AN1A_0, A_1, \dots, A_{N-1}.
The third line contains MM space-separated integers, B0,B1,,BM1B_0, B_1, \dots, B_{M-1}.
Each of the next QQ lines contain four space-separated integers, l1,r1,l2,r2l_1, r_1, l_2, r_2.

Output data

The output file must contain QQ lines corresponding to the test cases, each consisting of 64-bit integer ansi\mathtt{ans}_{i}.

Constraints and clarifications

  • 1N500 0001 \le N \le 500 \ 000.
  • 1M500 0001 \le M \le 500 \ 000.
  • 1Q500 0001 \le Q \le 500 \ 000.
  • 1Ai1 000 000 0001 \le A_i \le 1\ 000\ 000\ 000 for each i=0N1i=0\ldots N-1.
  • 1Bi1 000 000 0001 \le B_i \le 1\ 000\ 000\ 000 for each i=0M1i=0\ldots M-1.
  • 0l1ir1i<N0 \le {l_1}_i \le {r_1}_i < N, for each i=0Q1i=0\ldots Q-1.
  • 0l2ir2i<M0 \le {l_2}_i \le {r_2}_i < M, for each i=0Q1i=0\ldots Q-1.
# Score Constraints
1 0 Examples
2 10 N,M,Q100N, M, Q \leq 100
3 12 N,M,Q1 000N, M, Q \leq 1 \ 000
4 8 N,M,Q100 000N, M, Q \leq 100 \ 000, Bi=1B_i = 1
5 9 N,M,Q100 000N, M, Q \leq 100 \ 000, l1i=0,r1i=N1,l2i=0{l_1}_i = 0, {r_1}_i = N-1, {l_2}_i = 0
6 10 N,M,Q100 000N, M, Q \leq 100 \ 000, l1i=0,r1i=N1{l_1}_i = 0, {r_1}_i = N-1
7 20 N,M,Q100 000N, M, Q \leq 100 \ 000
8 31 No additional restrictions

Example

stdin

5 4 5
50 40 26 100 72
500 999 1 2
0 4 0 3
1 3 0 2
3 4 2 3
3 4 1 2
1 4 0 1

stdout

1721
1599
172
1099
1671

Explanation

In the first scenario, your played maps are {50,40,26,100,72}\{50, 40, 26, 100, 72\}, and your unplayed maps are {500,999,1,2}\{500, 999, 1, 2\}. Choosing to play the beatmaps with values 500500 and 999999, and overwriting the already played ones with values 2626 and 4040, you obtain the optimal sum of 50+999+500+100+72=172150 + 999 + 500 + 100 + 72 = 1721.

In the second scenario, your played maps are {40,26,100}\{40, 26, 100\}, and your unplayed maps are {500,999,1}\{500, 999, 1\}. Choosing to play the beatmaps with values 500500 and 999999, and overwriting the already played ones with values 2626 and 4040, you obtain the optimal sum of 999+500+100=1599999 + 500 + 100 = 1599.

In the third scenario, your played maps are {100,72}\{100, 72\}, and your unplayed maps are {1,2}\{1, 2\}. Choosing to keep all of your current top plays, you obtain the optimal sum of 100+72=172100 + 72 = 172.

In the fourth scenario, your played maps are {100,72}\{100, 72\}, and your unplayed maps are {999,1}\{999, 1\}. Choosing to play the beatmap with value 999999 and overwriting the already played one with value 7272, you obtain the optimal sum of 999+100=1099999 + 100 = 1099.

In the fifth scenario, your played maps are {40,26,100,72}\{40, 26, 100, 72\}, and your unplayed maps are {500,999}\{500, 999\}. Choosing to play the beatmaps with values 500500 and 999999 and overwriting the already played ones with values 2626 and 4040, you obtain the optimal sum of 999+500+100+72=1671999 + 500 + 100 + 72 = 1671.

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