Educational Codeforces Round 137 (Rated for Div. 2)

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 sell them. He distributed the magazines between nn boxes, arranged in a row. The ii-th box contains aiai 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 ii-th box to the box (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.

Input

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

The first line of each testcase contains a single integer nn (1≤n≤2⋅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 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,…,an (1≤ai≤10^4), where aiai is the number of magazines in the i-th box.

The sum of nn over all testcases doesn't exceed 2⋅10^5.

Output

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

Example

input

 

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

output

27
80
0
14

Note

In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes 1, 3 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 1, 2, 4 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.

 

考虑01dp

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int a[N],dp[N][2];
void solve()
{
   int n;
   cin>>n;
   string s;
   cin>>s;
   s=" "+s;
   for(int i=1;i<=n;i++)
   {
       cin>>a[i];
   }
   for(int i=1;i<=n;i++)
   {
       for(int j=0;j<=1;j++)
       {
           dp[i][j]=0;
       }
   }
   for(int i=1;i<=n;i++)
   {
       if(s[i]=='1')
       {
           dp[i][0]=max(dp[i][0],dp[i-1][0]+a[i-1]);
           dp[i][1]=max(dp[i-1][0],dp[i-1][1])+a[i];
       }
       else
       {
           dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
       }
   }
   cout<<max(dp[n][0],dp[n][1])<<"\n";

}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值