欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1743C.Save the Magazines
Save the Magazines
Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between boxes, arranged in a row. The -th box contains magazines. Some of the boxes are covered with lids, others are not.
Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the -th box was covered with a lid initially, he can either move the lid from the -th box to the box (if it exists), or keep the lid on the -th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.
You have to calculate the maximum number of magazines Monocarp can save from the rain.
Input
The first line contains a single integer () — the number of the testcases.
The first line of each testcase contains a single integer () — the number of boxes.
The second line contains a string of characters 0 and/or 1. If the -th character is 1, the -th box is initially covered with a lid. If the -th character is 0, the -th box is initially not covered.
The third line contains a sequence of integers (), where is the number of magazines in the -th box.
The sum of over all testcases doesn't exceed .
Output
For each testcase, print one integer — the maximum number of magazines Monocarp can save from the rain.
Note
In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes , and are covered, and magazines are saved.
In the second testcase, Monocarp can move the lid from the second box to the first box, then from the third box to the second box, then from the fifth box to the fourth box, and then from the sixth box to the fifth box. The boxes , , and will be covered, so magazines can be saved.
There are no lids in the third testcase, so it's impossible to save even a single magazine.
Samples
4
5
01110
10 5 8 9 6
6
011011
20 10 9 30 20 19
4
0000
100 100 100 100
4
0111
5 4 5 1
27
80
0
14
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |