-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal.cpp
88 lines (73 loc) · 1.73 KB
/
equal.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
vi dist(1e3+1, 1e9);
void solve() {
ll n, k;
cin >> n >> k;
vi a(n);
for(ll i = 0; i < n; i++) {
cin >> a[i];
}
vi c(n);
for(ll i = 0; i < n; i++) {
cin >> c[i];
}
map<ll, ll> dp1;
dp1[0] = 0;
map<ll, ll> dp2;
ll best = 0;
for (ll i = 0; i <= n; i++) {
for (pi dp : dp1) {
ll w = dp.first;
ll v = dp.second;
best = max(best, v);
if (i == n) continue;
// cost is dist[a[i]]
// not use
dp2[w] = max((dp2.find(w) != dp2.end() ? dp2[w] : 0), v);
// use
if (w + dist[a[i]] <= k) dp2[w + dist[a[i]]] = max((dp2.find(w + dist[a[i]]) != dp2.end() ? dp2[w + dist[a[i]]] : 0), v + c[i]);
}
dp1 = dp2;
dp2.clear();
}
cout << best << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll mx = 1e3 + 1;
vector<set<ll>> ed(1e3 + 1);
for (ll i = 1; i < mx; i++) {
for (ll x = 1; x < mx; x++) {
if ((i + (i / x)) < mx) ed[i].insert(i + (i / x));
}
}
vb vis(mx, false);
queue<pi> q;
q.push({1, 0});
vis[1] = true;
dist[1] = 0;
while(!q.empty()) {
pi top = q.front();
q.pop();
ll v = top.first;
ll d = top.second;
for(ll ch : ed[v]) {
if(!vis[ch]) {
vis[ch] = true;
dist[ch] = d + 1;
q.push({ch, dist[ch]});
}
}
}
ll t;
cin >> t;
while(t--) solve();
return 0;
}