-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path440-save-the-magazines.cpp
More file actions
54 lines (53 loc) · 1.4 KB
/
Copy path440-save-the-magazines.cpp
File metadata and controls
54 lines (53 loc) · 1.4 KB
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
/*
* Save the Magazines [1743C]
* Problem: https://codeforces.com/problemset/problem/1743/C
* Verdict: ACCEPTED Solved: 2024-02-27
* Language: C++20 (GCC 11-64)
* Runtime: 46 ms Memory: 2900 KB
* Tags: constructive algorithms, dp, greedy
* Author: BidoTeima
* Source: https://codeforces.com/contest/1743/submission/248478659
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
char ch;
vector<pair<int, int>>ranges;
int lst = -1;
for(int i = 0; i < n; i++){
cin>>ch;
if(ch == '0'){
if(lst + 1 <= i - 1){
ranges.push_back({lst + 1, i - 1});
//cout<<lst+1<<' '<<i-1<<'\n';
}
lst = i;
}
}
if(ch=='1'){
ranges.push_back({lst + 1, n - 1});
//cout<<lst+1<<' '<<n-1<<'\n';
}
//cout<<'\n';
int a[n],ans=0;
for(auto&i:a)cin>>i;
for(auto [l, r] : ranges){
int mn = 1e9, sum=0;
for(int i = l; i <= r; i++)mn = min(mn, a[i]), sum += a[i];
if(l > 0) ans += max(sum, sum - mn + a[l - 1]);
else ans += sum;
}
cout<<ans<<'\n';
}
return 0;
}