-
Notifications
You must be signed in to change notification settings - Fork 1
/
C.cpp
56 lines (53 loc) · 1.02 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
vector<int> arr;
int ans = 0;
for (int i = 0; i < n; i++)
{
int inp;
cin >> inp;
arr.push_back(inp);
}
int mini;
for(int i = n-1;i>=0;){
if(s[i]=='1'){
mini = arr[i];
while(i>=0){
mini = min(mini,arr[i]);
if(s[i]=='1'){
ans+=arr[i];
i--;
}else{
ans+=arr[i];
i--;
ans-=mini;
break;
}
}
}else{
i--;
}
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.precision(20);
cout.setf(ios::fixed);
ll testcases;
cin >> testcases;
while (testcases--)
{
solve();
}
}