当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
CSDN外链解决方法 (2022-07-28测试可用)

测试/开发程序员面试该如何谈薪资待遇呢?突破这个坎......

零代码工具推荐---HiFlow

浏览器缓存机制

Detailed explanation of carousel picture 2 - carousel pictures through left positioning

实现批量导出功能

org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)

超详细的MySQL三万字总结

Dell's first pure soft product redefines next-generation object storage

fluttter学习之ButtonStyle 、MaterialStateProperty
随机推荐
多线程---初阶
ESP8266 +0.96“ I2C OLED 表盘时钟
零代码工具推荐---HiFlow
RAII Technology Learning
Google浏览器打开axure产品原型的解决方案
绘图问题记录
go 双向流模式
JS Bom window innerWidth clientWidth onresize 窗口滚动偏移量 返回顶部
Dell's first pure soft product redefines next-generation object storage
matlab用dde23求解带有固定时滞的时滞微分方程
The role of interface testing
go jwt use
Docker一键安装MySQL
FL Studio官方20.9中文版无需汉化补丁,正确安装并设置切换
consul operation
共享内存-内存映射-共享文件对象
一个塑料瓶的海洋“奇幻漂流”
【C语言刷LeetCode】1331. 数组序号转换(E)
win11 自带远程桌面使用(包含非局域网使用以及win11升级为专业版)
English语法_不定代词 -some & any