-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe_Combat_of_Ages.cpp
57 lines (52 loc) · 1.35 KB
/
The_Combat_of_Ages.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
57
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fastIO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
ll query(ll idx, ll BIT[]) {
ll result = 0;
while(idx) {
result = max(BIT[idx], result);
idx -= (idx & -idx);
}
return result;
}
void add(ll idx, ll BIT[], ll value, ll n) {
while(idx <= n) {
BIT[idx] = max(BIT[idx], value);
idx += (idx & -idx);
}
}
void solve(ll t) {
ll n;
cin>>n;
vector<ll> strength(n), age(n);
for(ll i = 0; i < n; i++)
cin>>strength[i];
for(ll i = 0; i < n; i++)
cin>>age[i];
ll BIT[n + 1];
memset(BIT, 0, sizeof BIT);
vector<pair<ll, ll>> pp(n);
for(ll i = 0; i < n; i++) {
pp[i] = {strength[i], age[i]};
}
sort(pp.begin(), pp.end(), [&](const pair<ll, ll> &a, const pair<ll, ll> b) {
if(a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
for(ll i = 0; i < n; i++) {
ll idx = pp[i].second;
ll value = query(idx - 1, BIT) + pp[i].first;
add(idx, BIT, value , n);
}
cout<<query(n, BIT)<<endl;
}
int main() {
fastIO;// fast input output
ll t;
t=1;
cin>>t;
for(ll i = 1; i <= t; i++) solve(i);
return 0;
}