题库练习 Save the Magazines
← 上一题 下一题 →

A15382 | Save the Magazines

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between $n$ boxes, arranged in a row. The $i$ -th box contains $a_i$ 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 $i$ -th box was covered with a lid initially, he can either move the lid from the $i$ -th box to the box $(i-1)$ (if it exists), or keep the lid on the $i$ -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.

输入格式

The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of the testcases.

The first line of each testcase contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of boxes.

The second line contains a string of $n$ characters 0 and/or 1. If the $i$ -th character is 1, the $i$ -th box is initially covered with a lid. If the $i$ -th character is 0, the $i$ -th box is initially not covered.

The third line contains a sequence of integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^4$ ), where $a_i$ is the number of magazines in the $i$ -th box.

The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$ .

输出格式

For each testcase, print one integer — the maximum number of magazines Monocarp can save from the rain.

输入输出样例

输入 #1
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
输出 #1
27
80
0
14
C++ 编辑器
输入
输出