当前位置:网站首页>A. Strange Birthday Party- Codeforces Round #694 (Div. 1)
A. Strange Birthday Party- Codeforces Round #694 (Div. 1)
2022-07-30 02:30:00 【秦小咩】
Problem - 1470A - Codeforces
A. Strange Birthday Party
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya organized a strange birthday party. He invited nn friends and assigned an integer kiki to the ii-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are mm unique presents available, the jj-th present costs cjcj dollars (1≤c1≤c2≤…≤cm1≤c1≤c2≤…≤cm). It's not allowed to buy a single present more than once.
For the ii-th friend Petya can either buy them a present j≤kij≤ki, which costs cjcj dollars, or just give them ckicki dollars directly.
Help Petya determine the minimum total cost of hosting his party.
Input
The first input line contains a single integer tt (1≤t≤1031≤t≤103) — the number of test cases.
The first line of each test case contains two integers nn and mm (1≤n,m≤3⋅1051≤n,m≤3⋅105) — the number of friends, and the number of unique presents available.
The following line contains nn integers k1,k2,…,knk1,k2,…,kn (1≤ki≤m1≤ki≤m), assigned by Petya to his friends.
The next line contains mm integers c1,c2,…,cmc1,c2,…,cm (1≤c1≤c2≤…≤cm≤1091≤c1≤c2≤…≤cm≤109) — the prices of the presents.
It is guaranteed that sum of values nn over all test cases does not exceed 3⋅1053⋅105, and the sum of values mm over all test cases does not exceed 3⋅1053⋅105.
Output
For each test case output a single integer — the minimum cost of the party.
Examples
input
Copy
2 5 4 2 3 4 3 2 3 5 12 20 5 5 5 4 3 2 1 10 40 90 160 250
output
Copy
30 190
input
Copy
1 1 1 1 1
output
Copy
1
Note
In the first example, there are two test cases. In the first one, Petya has 55 friends and 44 available presents. Petya can spend only 3030 dollars if he gives
- 55 dollars to the first friend.
- A present that costs 1212 dollars to the second friend.
- A present that costs 55 dollars to the third friend.
- A present that costs 33 dollars to the fourth friend.
- 55 dollars to the fifth friend.
In the second one, Petya has 55 and 55 available presents. Petya can spend only 190190 dollars if he gives
- A present that costs 1010 dollars to the first friend.
- A present that costs 4040 dollars to the second friend.
- 9090 dollars to the third friend.
- 4040 dollars to the fourth friend.
- 1010 dollars to the fifth friend.
=========================================================================
很巧妙的贪心,突破点在于c是递增的,这也就说明了,k大的,它能选便宜的也能选送金额大的钱。k小的,它能选便宜的,也能送金额小的。由于我们尽可能选c小的,尽量多的选小的。如果让k小的先选,那么不仅浪费了它能选小的机会,还挤占了k大的选小的机会。所以先选k大的,充分利用
# include<bits/stdc++.h>
# define mod 10
using namespace std;
typedef long long int ll;
ll c[300000+10];
int k[300000+10];
bool cmp(int a,int b)
{
return a>b;
}
int main ()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>k[i];
}
for(int i=1;i<=m;i++)
{
cin>>c[i];
}
//k小的,买的礼物便宜,送钱少,但之能采用其中一个
//k大的,买的礼物可以贵也可以便宜,送钱多
int now=1;
sort(k+1,k+1+n,cmp);
ll ans=0;
for(int i=1;i<=n;i++)
{
if(now==n+1||c[now]>=c[k[i]])
{
ans+=c[k[i]];
}
else
{
ans+=c[now];
now++;
}
}
cout<<ans<<'\n';
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
【内部资源】冲击年薪30W+的软件测试人员,这份资料必须领取
matlab用dde23求解带有固定时滞的时滞微分方程
Leetcode.24 两两交换链表中的节点(递归)
JS Bom window innerWidth clientWidth onresize 窗口滚动偏移量 返回顶部
3种实现文本复制功能的方法
Not enough information to list load addresses in the image map. (STM32 compilation error)
el-table加合计
Sublime does background transparency and column editor
【C语言刷LeetCode】451. 根据字符出现频率排序(M)
el-table sum total
Postgresql daily operation and maintenance skills, suitable for beginners
【2023海康威视提前批笔试题】~ 题目及参考答案
错误:“filesystem“ 不是 “std“ 的成员
FL Studio官方20.9中文版无需汉化补丁,正确安装并设置切换
Drawing Problem Log
Docker一键安装MySQL
centOS安装MySQL详解
uni-app如何配置APP自定义顶部标题栏
力扣(LeetCode)210. 课程表 II(2022.07.29)
【MySQL】SQL学习









