A. Helmets in Night Light

time limit per test

1 second

memory limit per test

256 megabytes

Pak Chanek is the chief of a village named Khuntien. On one night filled with lights, Pak Chanek has a sudden and important announcement that needs to be notified to all of the n residents in Khuntien.

First, Pak Chanek shares the announcement directly to one or more residents with a cost of p for each person. After that, the residents can share the announcement to other residents using a magical helmet-shaped device. However, there is a cost for using the helmet-shaped device. For each i, if the i-th resident has got the announcement at least once (either directly from Pak Chanek or from another resident), he/she can share the announcement to at most ai other residents with a cost of bi for each share.

If Pak Chanek can also control how the residents share the announcement to other residents, what is the minimum cost for Pak Chanek to notify all n residents of Khuntien about the announcement?

Input

Each test contains multiple test cases. The first line contains an integer t (1≤t≤104) --- the number of test cases. The following lines contain the description of each test case.

The first line contains two integers n and p (1≤n≤105; 1≤p≤105) --- the number of residents and the cost for Pak Chanek to share the announcement directly to one resident.

The second line contains n integers a1,a2,a3,...,an (1≤ai≤105) --- the maximum number of residents that each resident can share the announcement to.

The third line contains n integers b1,b2,b3,...,bn (1≤bi≤105) --- the cost for each resident to share the announcement to one other resident.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output

For each test case, output a line containing an integer representing the minimum cost to notify all n residents of Khuntien about the announcement.

Example

Input

Copy

复制代码

3

6 3

2 3 2 1 1 3

4 3 2 6 3 6

1 100000

100000

1

4 94

1 4 2 3

103 96 86 57

Output

Copy

复制代码
16
100000
265

Note

In the first test case, the following is a possible optimal strategy:

  1. Pak Chanek shares the announcement directly to the 3-rd, 5-th, and 6-th resident. This requires a cost of p+p+p=3+3+3=9.
  2. The 3-rd resident shares the announcement to the 1-st and 2-nd resident. This requires a cost of b3+b3=2+2=4.
  3. The 2-nd resident shares the announcement to the 4-th resident. This requires a cost of b2=3.

The total cost is 9+4+3=16. It can be shown that there is no other strategy with a smaller cost.

解题说明:此题采用贪心算法,可以按花费 进行排序一下,如果b<p 就让他用 b 的花费告诉别人,剩下的人一开始用 p 的花费进行通知。

cpp 复制代码
#include <bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int64_t i, n, p, c = 1, ans;
		cin >> n >> p;
		ans = p;
		vector<pair<int64_t, int64_t>> l(n);
		for (i = 0; i < n; i++)
		{
			cin >> l[i].second;
		}
		for (i = 0; i < n; i++)
		{
			cin >> l[i].first;
		}
		sort(l.begin(), l.end());
		for (auto [f, s] : l) 
		{
			if (f > p)
			{
				break;
			}
			ans += f * min(s, n - c);
			c += min(s, n - c);
		}
		ans += (n - c) * p;
		cout << ans << endl;
	}
	return 0;
}
相关推荐
Tingjct32 分钟前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
飞机和胖和黄1 小时前
考研之王道C语言第三周
c语言·数据结构·考研
醉颜凉2 小时前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
一匹电信狗2 小时前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
卢锡荣5 小时前
Type-c OTG数据与充电如何进行交互使用应用讲解
c语言·开发语言·计算机外设·电脑·音视频
v_for_van5 小时前
力扣刷题记录2(无算法背景,纯C语言)
c语言·算法·leetcode
二年级程序员5 小时前
动态内存管理
c语言
我能坚持多久5 小时前
D20—C语言文件操作详解:从基础到高级应用
c语言·开发语言
(❁´◡`❁)Jimmy(❁´◡`❁)6 小时前
CF2188 C. Restricted Sorting
c语言·开发语言·算法
想放学的刺客6 小时前
单片机嵌入式试题(第27期)设计可移植、可配置的外设驱动框架的关键要点
c语言·stm32·单片机·嵌入式硬件·物联网