E. Scuza

time limit per test

3 seconds

memory limit per test

256 megabytes

Timur has a stairway with nn steps. The ii-th step is aiai meters higher than its predecessor. The first step is a1a1 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,...,kqk1,...,kq. For each question kiki, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length kiki. Timur can only climb the jj-th step if his legs are of length at least ajaj. In other words, ki≥ajki≥aj for each step jj climbed.

Note that you should answer each question independently.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) --- the number of test cases.

The first line of each test case contains two integers n,qn,q (1≤n,q≤2⋅1051≤n,q≤2⋅105) --- the number of steps and the number of questions, respectively.

The second line of each test case contains nn integers (1≤ai≤1091≤ai≤109) --- the height of the steps.

The third line of each test case contains qq integers (0≤ki≤1090≤ki≤109) --- the numbers for each question.

It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105, and the sum of qq does not exceed 2⋅1052⋅105.

Output

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++).

Example

Input

Copy

复制代码

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

Copy

复制代码
1 4 4 9 9 
0 2 
3000000000 

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.

In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :(

解题说明:此题是一道模拟题,根据题目意思, 需要记录每个台阶相应位置的总高度, 也就是求出前缀和 ,之后还要存储好到达所对应高度坐标中的 Tim所需要的跨越高度是多少,并记录到满足该条件后对应的前缀和下标是多少。这里采用upper_bound(a + 1, a + n + 1, x) - a - 1 在数组 a 中查找第一个大于 x 的元素的位置,然后返回该位置的前一个索引(即最后一个小于等于 x 的元素的位置)

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int a[200005];
long long b[200005];

int main()
{
	int t, i, x, n, q;
	cin >> t;
	while (t--) 
	{
		cin >> n >> q;
		for (i = 1; i <= n; ++i) 
		{
			cin >> x;
			a[i] = max(a[i - 1], x);
			b[i] = b[i - 1] + x;
		}

		while (q--)
		{
			cin >> x;
			cout << b[upper_bound(a + 1, a + n + 1, x) - a - 1] << ' ';
		}
		cout << '\n';
	}
	return 0;
}
相关推荐
码农大叔的博客7 分钟前
golang示例:for九九乘法表
开发语言·算法·golang
星恒随风14 分钟前
C++ 哈希详解(二):开放定址、哈希桶与 C++ 哈希表底层实现
c++·笔记·学习·哈希算法·散列表
手写码匠16 分钟前
华为云Flexus+DeepSeek征文|Dify 多 Agent 故障演练实战:用混沌工程主动“搞破坏“,让智能体系统越炸越稳
人工智能·深度学习·算法·aigc
lingran__43 分钟前
C++ STL unordered系列(哈希) 底层剖析与模拟实现万字详解 | 基于哈希表,复刻 SGI-STL 泛型哈希容器架构
开发语言·c++·后端·哈希算法·哈希表·泛型编程·unordered系列
布莱克6051 小时前
理解B+树:原理、特性与应用场景
数据结构·数据库·mysql
叠层归一研究院1 小时前
如何用程序搭建一个 AGI 种子系统(三):生长如何对接物理与数学宇宙
人工智能·python·算法·机器学习·transformer·agi
鸿芯微控科技1 小时前
MFC设定流量达不到怎么办?上游供气能力、压差与管路阻力排查
c++·mfc·质量流量控制器·流量不足排查·压差测试·气体流量控制
show4331 小时前
2026小程序端AI智能优化技术实践:转文字后自动纠错+润色+分段算法分析
人工智能·算法·小程序
郝学胜_神的一滴1 小时前
C++20 高级编程 003:吃透现代基础语法与标准容器
c++
码匠许师傅1 小时前
【C++ 面试真题】聊聊 C++ 的关联容器
开发语言·c++·面试