Information
- ID
- 6636
- Time
- 1000ms
- Memory
- 20MiB
- Difficulty
- 8
- Tags
- # Submissions
- 48
- Accepted
- 11
- Uploaded By
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,a[N],pre[N];
int main(){
int T;
cin>>T;
while(T--){
int ans=0;
string s;
cin>>n;
cin>>s;
for(int i=0;i<n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
int L=0;
while(s[L]=='1'&&L<n) ans+=a[L],L++;
while(s[L+1]!='1'&&L<n) L++;
int R=L+1;
while(L<R&&R<n&&L<n){
int minn=a[L];
while(s[R+1]=='1'&&R<n) minn=min(minn,a[R]),R++;
minn=min(minn,a[R]);
ans+=pre[R]-pre[L-1]-minn;
L=R+1;
while(s[L+1]!='1'&&L<n) L++;
R=L+1;
}
cout<<ans<<"\n";
}
}
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.