Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Last active October 19, 2022 00:14
Show Gist options
  • Save qjatn0120/77329efe21810d320b366a961286f6ea to your computer and use it in GitHub Desktop.
Save qjatn0120/77329efe21810d320b366a961286f6ea to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int t, n, a[MX];
long long int dp[MX][2];
string s;
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n >> s;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++){
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = 0;
if(s[i - 1] == '1'){
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
dp[i][0] = max(dp[i][0], dp[i - 1][0] + a[i - 1]);
}
}
cout << max(dp[n][0], dp[n][1]) << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment