#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3 + 25;
typedef long long ll;
const ll inf = 1e14;
ll a[MAXN], f[MAXN], dp[MAXN][MAXN], n;
vector <int> adj[MAXN];
bool vis[MAXN];
vector <int> ind;
void fix (int pos, int par) {
ind.push_back(pos);
for (int i = 0; i < (int)adj[pos].size(); i++) {
if (adj[pos][i] == par) {
adj[pos].erase(adj[pos].begin() + i);
}
}
vector <int> g;
for (auto j : adj[pos]) {
fix(j, pos);
if (a[pos] + 1 == a[j] || a[j] == 0) {
g.push_back(j);
}
}
adj[pos] = g;
}
void dfs (int pos) {
vis[pos] = 1;
for (auto j : adj[pos]) dfs(j);
for (int i = 1; i <= a[pos]; i++) dp[pos][i] = inf;
for (auto j : adj[pos]) {
ll mn = inf;
for (int u = 1; u <= n; u++) {
mn = min(mn, f[u] + dp[j][u]);
}
for (ll i = a[pos] + 1; i <= n; i++) {
if (a[pos] != i - 1 && a[j] == 0) {
dp[pos][i] += mn;
} else {
dp[pos][i] += min(mn, dp[j][i]);
}
}
}
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> f[i];
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
fix(1, -1);
for (int i = 1; i <= n; i++) {
for (auto j : adj[i]) {
assert(a[j] == 0 || a[i] + 1 == a[j]);
}
}
ll sum = 0;
for (auto i : ind) {
if (!vis[i]) {
dfs(i);
ll mn = inf;
for (int j = 1; j <= n; j++) {
mn = min(mn, f[j] + dp[i][j]);
}
sum += mn;
}
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
198648 KB |
Output is correct |
2 |
Correct |
104 ms |
198672 KB |
Output is correct |
3 |
Correct |
91 ms |
198484 KB |
Output is correct |
4 |
Correct |
107 ms |
198516 KB |
Output is correct |
5 |
Correct |
97 ms |
198420 KB |
Output is correct |
6 |
Correct |
97 ms |
196932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
840 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
198648 KB |
Output is correct |
2 |
Correct |
104 ms |
198672 KB |
Output is correct |
3 |
Correct |
91 ms |
198484 KB |
Output is correct |
4 |
Correct |
107 ms |
198516 KB |
Output is correct |
5 |
Correct |
97 ms |
198420 KB |
Output is correct |
6 |
Correct |
97 ms |
196932 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
108 ms |
168536 KB |
Output is correct |
15 |
Correct |
83 ms |
182356 KB |
Output is correct |
16 |
Correct |
88 ms |
195624 KB |
Output is correct |
17 |
Correct |
87 ms |
169760 KB |
Output is correct |
18 |
Correct |
88 ms |
154092 KB |
Output is correct |
19 |
Correct |
84 ms |
164720 KB |
Output is correct |
20 |
Correct |
58 ms |
171348 KB |
Output is correct |
21 |
Correct |
63 ms |
161936 KB |
Output is correct |
22 |
Correct |
62 ms |
155700 KB |
Output is correct |
23 |
Correct |
72 ms |
165396 KB |
Output is correct |
24 |
Correct |
78 ms |
105324 KB |
Output is correct |
25 |
Correct |
76 ms |
119120 KB |
Output is correct |
26 |
Correct |
82 ms |
125148 KB |
Output is correct |
27 |
Correct |
89 ms |
126608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
198648 KB |
Output is correct |
2 |
Correct |
104 ms |
198672 KB |
Output is correct |
3 |
Correct |
91 ms |
198484 KB |
Output is correct |
4 |
Correct |
107 ms |
198516 KB |
Output is correct |
5 |
Correct |
97 ms |
198420 KB |
Output is correct |
6 |
Correct |
97 ms |
196932 KB |
Output is correct |
7 |
Runtime error |
6 ms |
840 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |