1 solutions
-
0
#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"; } }
- 1
Information
- ID
- 6636
- Time
- 1000ms
- Memory
- 20MiB
- Difficulty
- 8
- Tags
- # Submissions
- 48
- Accepted
- 11
- Uploaded By