A. Strange Birthday Party- Codeforces Round #694 (Div. 1)

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值