CF BUDDY
← Problems·

1742E · Scuza

1200 · binary search, greedy, math

Problem: Timur has a stairway with nn steps. The ii-th step is aia_i meters higher than its predecessor. The first step is a1a_1 meters higher than the ground, and the ground starts at 00 meters.

The stairs for the first test case.

Timur has qq questions, each denoted by an integer k1,,kqk_1, \dots, k_q. For each question kik_i, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length kik_i. Timur can only climb the jj-th step if his legs are of length at least aja_j. In other words, kiajk_i \geq a_j for each step jj climbed.

Note that you should answer each question independently.

Input Format: The first line contains a single integer tt (1t1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains two integers n,qn, q (1n,q21051 \leq n, q \leq 2\cdot10^5) — the number of steps and the number of questions, respectively.

The second line of each test case contains nn integers (1ai1091 \leq a_i \leq 10^9) — the height of the steps.

The third line of each test case contains qq integers (0ki1090 \leq k_i \leq 10^9) — the numbers for each question.

It is guaranteed that the sum of nn does not exceed 21052\cdot10^5, and the sum of qq does not exceed 21052\cdot10^5.

Output Format: For each test case, output a single line containing qq integers, the answer for each question.

Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Note: Consider the first test case, pictured in the statement.

  • If Timur's legs have length 11, then he can only climb stair 11, so the highest he can reach is 11 meter.
  • If Timur's legs have length 22 or 44, then he can only climb stairs 11, 22, and 33, so the highest he can reach is 1+2+1=41+2+1=4 meters.
  • If Timur's legs have length 99 or 1010, then he can climb the whole staircase, so the highest he can reach is 1+2+1+5=91+2+1+5=9 meters.

Sample Cases

Case 1

Input

3
4 5
1 2 1 5
1 2 4 9 10
2 2
1 1
0 1
3 1
1000000000 1000000000 1000000000
1000000000

Output

1 4 4 9 9 
0 2 
3000000000

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback