CF1743C.Save the Magazines

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between nn boxes, arranged in a row. The ii -th box contains aia_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 ii -th box was covered with a lid initially, he can either move the lid from the ii -th box to the box (i1)(i-1) (if it exists), or keep the lid on the ii -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 tt ( 1t1041 \le t \le 10^4 ) — the number of the testcases.

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

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

The third line contains a sequence of integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai1041 \le a_i \le 10^4 ), where aia_i is the number of magazines in the ii -th box.

The sum of nn over all testcases doesn't exceed 21052 \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

说明/提示

In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes 11 , 33 and 44 are covered, and 10+8+9=2710 + 8 + 9 = 27 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 11 , 22 , 44 and 55 will be covered, so 20+10+30+20=8020 + 10 + 30 + 20 = 80 magazines can be saved.

There are no lids in the third testcase, so it's impossible to save even a single magazine.

首页