GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Monday 17 October 2022

[Solution] Save the Magazines Codeforces Solution



C. Save the Magazines
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Monocarp has been collecting rare magazines for quite a while, and now he has decided to cell them. He distributed the magazines between n boxes, arranged in a row. The i-th box contains ai 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 (i1) (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.

Input

The first line contains a single integer t (1t104) — the number of the testcases.

The first line of each testcase contains a single integer n (1n2105) — 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 a1,a2,,an (1ai104), where ai is the number of magazines in the i-th box.

The sum of n over all testcases doesn't exceed 2105.

Output

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

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇


Note

In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes 13 and 4 are covered, and 10+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 124 and 5 will be covered, so 20+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.

No comments:

Post a Comment