【动态规划】
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 2e5+10;
int a[N] , dp[N][2];
int n;
string str;
void solve()
{
cin >> n >> str;
str = "0" + str;
for(int i = 1;i <= n;i ++)
cin >> a[i];
dp[0][0] = 0;
dp[0][1] = 0;
for(int i = 1;i <= n;i ++)
{
if(str[i] == '0')
dp[i][0] = dp[i][1] = max(dp[i - 1][0] , dp[i - 1][1]);
else
{
dp[i][0] = dp[i - 1][0] + a[i - 1];
dp[i][1] = max(dp[i - 1][0] , dp[i - 1][1]) + a[i];
}
}
cout << max(dp[n][0] , dp[n][1]) << endl;
}
int main()
{
int T;
cin >> T;
while(T --)
solve();
return 0;
}
标签:Save,int,Magazines,str,solve,include,dp
From: https://www.cnblogs.com/heystar/p/16814590.html