-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodejam_2022_q_d.cpp
237 lines (155 loc) · 5.06 KB
/
codejam_2022_q_d.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Author: Chaudhary Hamdan (https://chaudharyhamdan.me/)
*
* Generated at : Sat Apr 2 19:16:13 2022
*
* C++ Template for Competitive Programming
* Github link: https://github.com/hamdan-codes
*
* Template available at my Github
* Repository Link:-
* https://github.com/hamdan-codes/templates-for-CP/
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ff first
#define ss second
#define int long long
#define amax(a,b) if(b>a) swap(a, b)
#define amin(a,b) if(b<a) swap(a, b)
#define pb push_back
#define mp make_pair
#define ii int, int
#define pii pair<ii>
#define vi vector<int>
#define vvi vector<vi>
#define si set<int>
#define pi print<int>
#define pc print<char>
#define ps print<string>
#define vpii vector<pii>
#define vs vector<string>
#define vc vector<char>
#define gi greater<int>
#define umii unordered_map<ii>
#define mii map<ii>
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vi, gi>
#define all(x) x.begin(), x.end()
#define rev(x) reverse(all(x))
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define deb(x) cout << #x << "= " << x << endl;
#define py cout<<"YES"<<endl
#define pn cout<<"NO"<<endl
#define pans cout<<ans<<endl
#define gcd __gcd
#define mod 1000000007
#define PI 3.14159265358979323846264338
#define inf LONG_LONG_MAX
#define pcs(x,y) fixed << setprecision(y) << x
#define endl '\n'
#define maxelem(a) *max_element(all(a))
#define minelem(a) *min_element(all(a))
#define lower(s) transform(all(s), s.begin(), ::tolower)
#define upper(s) transform(all(s), s.begin(), ::toupper)
#define mk(t,arr,n) t *arr = new t[n]
#define FI(i,x,y,inc) for(int i = x; i < y; i += inc)
#define F0(i,x) FI(i, 0, x, 1)
#define F(i,x,y) FI(i, x, y, 1)
#define RF(i,x,y) for(int i = x; i >= y; i--)
#define rep(i,a) for(auto &i : a)
#define W(x) int zyx; cin >> zyx; F(x, 1, zyx + 1)
#define W1(x) F(x, 1, 2)
#define take(a,n) vi a(n); F0(ari, n) cin >> a[ari];
#define printa(arr,n) F0(i_ar, n) cout << arr[i_ar] << ' '; cout << endl;
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void i_o_from_file() {
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\KIIT\\input", "r", stdin);
freopen("C:\\Users\\KIIT\\output", "w", stdout);
#endif
}
template<typename T>
void print(int n, ...) {
va_list ap;
va_start(ap, n);
F(i, 1, n + 1) {
T a = va_arg(ap, T);
cout << a << ' ';
}
cout << endl;
va_end(ap);
return;
}
/* ********************* Your Functions and Classes Below ********************** */
/* ********************* Your Functions and Classes Above ********************** */
void solve_tests() {
int ans = 0;
int n, x; cin >> n;
unordered_map<int, int> point;
unordered_map<int, int> value;
vector<bool> initial(n + 1, true);
vvi paths;
F(i, 1, n + 1) {
cin >> x;
value[i] = x;
}
F(i, 1, n + 1) {
cin >> x;
point[i] = x;
initial[x] = false;
}
F(i, 1, n + 1) {
int node = i;
if (initial[node]) {
vi path;
path.pb(node);
while (point[node]) {
path.pb(point[node]);
node = point[node];
}
paths.pb(path);
}
}
vi range(paths.size());
F0(i, range.size()) range[i] = i;
do {
int anss = 0;
vector<bool> visited(n + 1, false);
rep(i, range) {
int mx = -inf;
rep(j, paths[i]) {
if (!visited[j]) mx = max(mx, value[j]);
visited[j] = true;
}
anss += mx;
}
amax(ans, anss);
}
while (next_permutation(all(range)));
pans;
return;
}
int32_t main() {
FAST;
i_o_from_file();
/* ******************* Your main function Code Below ******************* */
W(tc) {
cout << "Case #" << tc << ": ";
solve_tests();
}
/* **************** Your main function Code Above ****************** */
return 0;
}