B - Saving the City

cf--1443B

Bertown is a city with nn buildings in a straight line.

The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length nn, where the ii-th character is "1" if there is a mine under the building number ii and "0" otherwise.

Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered xx is activated, it explodes and activates two adjacent mines under the buildings numbered x−1x−1 and x+1x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes aa coins. He can repeat this operation as many times as you want.

Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes bb coins. He can also repeat this operation as many times as you want.

The sapper can carry out operations in any order.

You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.

Input

The first line contains one positive integer tt (1≤t≤1051≤t≤105) — the number of test cases. Then tt test cases follow.

Each test case begins with a line containing two integers aaand bb (1≤a,b≤10001≤a,b≤1000) — the cost of activating and placing one mine, respectively.

The next line contains a map of mines in the city — a string consisting of zeros and ones.

The sum of the string lengths for all test cases does not exceed 105105.

Output

For each test case, output one integer — the minimum number of coins that the sapper will have to pay.

Example

Input
2
1 1
01000010
5 1
01101110
Output
2
6

Note

In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1b=1 coin for placing a mine and a=5a=5 coins for activating.

题意:给出长为n的字符串,由1和0组成,1代表有地雷,0代表无地雷,放置 1 即地雷需花费 b 个硬币,激活 1 需花费 a 个硬币,且可以把相邻的地雷激活不需花费硬币,求最小的花费数将字符串全变为0。

思路:至少要有一个引爆的费用,即第一段连续的地雷 1 需要 a硬币,sum=a,再看后面不相连的两段相连后的费用和不相连的费用的比较,取花费最小的,相连后即sum+=ct*b+a, 不相连的 sum+=a;

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
getchar();
string s;
cin>>s;
int x[100005]={0};
int m=s.size(),k=0;
for(int i=0;i<m;i++)
{
if(s[i]=='1')
{
x[k++]=i;
}
}
if(k==0)cout<<"0"<<endl;
else{
int sum=a;
for(int i=0;i<k-1;i++)
{
if(x[i+1]-x[i]<=1)
continue;
else
sum+=min((x[i+1]-x[i]-1)*b,a);
}
cout<<sum<<endl;
} }
}

C - The Delivery Dilemma

cf--1443C

Petya is preparing for his birthday. He decided that there would be nn different dishes on the dinner table, numbered from 11 to nn. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from nn different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:

  • the dish will be delivered by a courier from the restaurant ii, in this case the courier will arrive in aiaiminutes,
  • Petya goes to the restaurant ii on his own and picks up the dish, he will spend bibi minutes on this.

Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.

For example, if Petya wants to order n=4n=4 dishes and a=[3,7,4,5]a=[3,7,4,5], and b=[2,1,2,4]b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 33 minutes, the courier of the fourth restaurant will bring the order in 55minutes, and Petya will pick up the remaining dishes in 1+2=31+2=3 minutes. Thus, in 55 minutes all the dishes will be at Petya's house.

Find the minimum time after which all the dishes can be at Petya's home.

Input

The first line contains one positive integer tt (1≤t≤2⋅1051≤t≤2⋅105) — the number of test cases. Then tt test cases follow.

Each test case begins with a line containing one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of dishes that Petya wants to order.

The second line of each test case contains nn integers a1…ana1…an (1≤ai≤1091≤ai≤109) — the time of courier delivery of the dish with the number ii.

The third line of each test case contains nn integers b1…bnb1…bn (1≤bi≤1091≤bi≤109) — the time during which Petya will pick up the dish with the number ii.

The sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case output one integer — the minimum time after which all dishes can be at Petya's home.

Example

Input
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
Output
5
3
2
3

题意:

要点有n个菜,点外卖或者自己去拿,点外卖需时间ai,自己拿需 bi,求最短时间

思路:

二分法。 记下自己去拿菜的总时间,二分,如果 ai 时间<mid ,就点外卖,>mid,就自己去拿, 然后自己去拿的时间再和mid 比较

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+100;
ll a[N],b[N];
ll n;
bool show(ll x)
{
ll sum=0;
for(int i=0;i<n;i++)
{
if(a[i]>x)
{
sum+=b[i];
}
}
return sum<=x;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
ll l=0,r=0;
for(int j=0;j<n;j++)
{
cin>>b[j];
r+=b[j];
}
while(l<r)
{
ll mid=(l+r)/2;
if(show(mid)==1)r=mid;
else l=mid+1;
}
cout<<r<<endl;
}
}

Petya is preparing for his birthday. He decided that there would be nn different dishes on the dinner table, numbered from 11 to nn. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from nn different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:

  • the dish will be delivered by a courier from the restaurant ii, in this case the courier will arrive in aiaiminutes,
  • Petya goes to the restaurant ii on his own and picks up the dish, he will spend bibi minutes on this.

Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.

For example, if Petya wants to order n=4n=4 dishes and a=[3,7,4,5]a=[3,7,4,5], and b=[2,1,2,4]b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 33 minutes, the courier of the fourth restaurant will bring the order in 55minutes, and Petya will pick up the remaining dishes in 1+2=31+2=3 minutes. Thus, in 55 minutes all the dishes will be at Petya's house.

Find the minimum time after which all the dishes can be at Petya's home.

Input

The first line contains one positive integer tt (1≤t≤2⋅1051≤t≤2⋅105) — the number of test cases. Then tt test cases follow.

Each test case begins with a line containing one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of dishes that Petya wants to order.

The second line of each test case contains nn integers a1…ana1…an (1≤ai≤1091≤ai≤109) — the time of courier delivery of the dish with the number ii.

The third line of each test case contains nn integers b1…bnb1…bn (1≤bi≤1091≤bi≤109) — the time during which Petya will pick up the dish with the number ii.

The sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case output one integer — the minimum time after which all dishes can be at Petya's home.

Example

Input
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
Output
5
3
2
3

2021.3.10--vj补题的更多相关文章

  1. 2021.5.22 vj补题

    A - Marks CodeForces - 152A 题意:给出一个学生人数n,每个学生的m个学科成绩(成绩从1到9)没有空格排列给出.在每科中都有成绩最好的人或者并列,求出最好成绩的人数 思路:求 ...

  2. 2021-5-15 vj补题

    C - Win or Freeze CodeForces - 151C 题目内容: You can't possibly imagine how cold our friends are this w ...

  3. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  4. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  5. ICPC南京补题

    由于缺的题目比较多,竟然高达3题,所以再写一篇补题的博客 Lpl and Energy-saving Lamps During tea-drinking, princess, amongst othe ...

  6. 【cf补题记录】Codeforces Round #607 (Div. 2)

    比赛传送门 这里推荐一位dalao的博客-- https://www.cnblogs.com/KisekiPurin2019/ A:字符串 B:贪心 A // https://codeforces.c ...

  7. Codeforces VP/补题小记 (持续填坑)

    Codeforces VP/补题小记 1149 C. Tree Generator 给你一棵树的括号序列,每次交换两个括号,维护每次交换之后的直径. ​ 考虑括号序列维护树的路径信息和,是将左括号看做 ...

  8. CSP-S2019 赛前补题

    前言 该打的比赛也打完了,每一场打得并不是很理想,所以就没写赛后总结了.最后再把每一场的比赛补一下,也算给自己一个交代吧. 牛客CSP-S提高组赛前集训营6 考试 100 + 30 + 0 = 130 ...

  9. 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集

    春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...

  10. 【补题记录】ZJU-ICPC Summer Training 2020 部分补题记录

    补题地址:https://zjusummer.contest.codeforces.com/ Contents ZJU-ICPC Summer 2020 Contest 1 by Group A Pr ...

随机推荐

  1. zigzag走线原理及应用

    电路板上弯弯扭扭的走线有什么用 往期文章: 一文读懂高速互联的阻抗及反射(上) 一文读懂高速互联的阻抗及反射(中) 前面几篇文章有部分读者反馈太深奥,不好懂,要求来一点轻松易懂的.这不,它来了!本期文 ...

  2. 为开源项目 go-gin-api 增加后台任务模块

    目录 任务管理界面 (WEB) 任务调度器 任务执行器 小结 推荐阅读 任务管理界面 (WEB) 支持在 WEB 界面 中对任务进行管理,例如:新增任务.编辑任务.启用/禁用任务.手动执行任务 等. ...

  3. 小白2分钟学会Visual Studio将引用包打包到NuGet上

    前言 之前我们说 10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#),过程有些许繁琐,并且需要有一定的基础. 怎么办呢,不慌,看法宝. 两 ...

  4. 针对Autocad 2014 第二次安装不上的问题

    针对Autocad 2014 第二次安装不上的问题 1. 以下为卸载过程,不用管. 2. 卸载完之后,右击"开始",点击"运行",得到下图:   并输入:&qu ...

  5. 斐波那契数(Java)

    斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 .该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0,F(1) = 1 F(n) = F(n ...

  6. 菜狗、《灵笼》、《时光代理人》,重新审视Z世代的电商逻辑

    来源:懂懂笔记 B站还有多少潜力可以挖掘? 虽然B站的最新财报依然还是亏损,但同时也让人看到更多的可能性. 从财报数据的亮点来看,一是营收增长,B站二季度营收为44.95亿元,同比增长72%.营收上B ...

  7. Xilinx约束学习笔记(三)—— 时序概念

    3. 时序概念 发现对于时序基础的介绍这一块,Intel 的文档竟然要比 Xilinx 的详细,因此引用了很多 Intel 的文档内容. 3.1 术语 发送沿(launch edge),指用来发送数据 ...

  8. 学习Tomcat(二)之容器概览

    Tomcat容器的Server模块有管理容器的启动和关闭.管理了容器内的服务组件Service.管理了全局JNDI资源的功能,对Tomcat容器的生命周期管理有重要意义.Tomcat的服务组件则是To ...

  9. etcd学习(10)-etcd对比Consul和zooKeeper如何选型

    etcd选型对比 前言 基本架构和原理 etcd Consul ZooKeeper 选型对比 总结 参考 etcd选型对比 前言 对比 Consul, ZooKeeper.选型etcd有那些好处呢? ...

  10. Python增强下git那长长的指令

    场景 现如今有点规模的公司都使用GitFlow模式进行分支管理.虽然插件给我们带来了非常大的方便,但切换分支.找分支.起分支还是那么的麻烦 需求 在社会主次国家,每个生活在底层的劳动人民,他们默默的工 ...