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 $ 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 t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ) — the number of the testcases.

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

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

The third line contains a sequence of integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 4 1 \le a_i \le 10^4 1ai104 ), where a i a_i ai is the number of magazines in the i i i -th box.

The sum of $ n $ over all testcases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .

输出格式

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

样例 #1

样例输入 #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 $ 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.

题面翻译

n n n 个箱子,第 i i i 个箱子里面装着 a i a_i ai 个杂志。现在正在下雨,有一些箱子上面盖着盖板(如果一个箱子上盖了盖板,那么这个箱子里的杂志就不会被雨淋),并且可以将盖在第 i i i 个箱子上的盖板移到第 i − 1 i-1 i1 个箱子上(同一个盖板至多移动一次)。求不会被雨淋的杂志数量的最大值。

思路:

设定 d p i , 0 / 1 dp_{i,0/1} dpi,0/1 表示前 i i i 个盒子的最大值,以及第 i i i 个盒子的盖子是否移动。

下面进行分类讨论即可。

如果没有盖子,那就直接继承上一个的最大值。

如果有盖子,那就考虑移动和不移动两种情况,不移动也是直接继承上一个的最大值。

否则就讨论第 i − 1 i - 1 i1 个是否有盖子,没有不需要移动第 i − 1 i - 1 i1 个,否则就需要移动才能加上第 i i i 个。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int dp[N][2],a[N],vis[N],T,n;
int main(){
	scanf("%d",&T);
	while(T--){
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%1d",&vis[i]);
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(vis[i]){
				dp[i][0]=a[i];
			}
		}
		for(int i=2;i<=n;i++){
			if(vis[i]){ 
				dp[i][0]=max(dp[i-1][0]+a[i],dp[i-1][1]+a[i]);
				if(!vis[i-1]){
					dp[i][1]=dp[i-1][0]+a[i-1];
				}else{
					dp[i][1]=dp[i-1][1]+a[i-1];
				}
			}else{
				dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
			}
		}
		printf("%d\n",max(dp[n][0],dp[n][1]));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值