#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], 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;
}
vector <ll> dfs (int pos) {
vector <ll> dp(n + 1);
vis[pos] = 1;
for (int i = 1; i <= a[pos]; i++) dp[i] = inf;
for (auto j : adj[pos]) {
auto z = dfs(j);
ll mn = inf;
for (int u = 1; u <= n; u++) {
mn = min(mn, f[u] + z[u]);
}
for (ll i = a[pos] + 1; i <= n; i++) {
if (a[pos] != i - 1 && a[j] == 0) {
dp[i] += mn;
} else {
dp[i] += min(mn, z[i]);
}
}
}
return dp;
}
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]) {
auto z = dfs(i);
ll mn = inf;
for (int j = 1; j <= n; j++) {
mn = min(mn, f[j] + z[j]);
}
sum += mn;
}
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
197096 KB |
Output is correct |
2 |
Correct |
139 ms |
197232 KB |
Output is correct |
3 |
Correct |
148 ms |
197312 KB |
Output is correct |
4 |
Correct |
142 ms |
197196 KB |
Output is correct |
5 |
Correct |
126 ms |
25212 KB |
Output is correct |
6 |
Correct |
115 ms |
7060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
1016 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
197096 KB |
Output is correct |
2 |
Correct |
139 ms |
197232 KB |
Output is correct |
3 |
Correct |
148 ms |
197312 KB |
Output is correct |
4 |
Correct |
142 ms |
197196 KB |
Output is correct |
5 |
Correct |
126 ms |
25212 KB |
Output is correct |
6 |
Correct |
115 ms |
7060 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
69 ms |
1512 KB |
Output is correct |
15 |
Correct |
67 ms |
1500 KB |
Output is correct |
16 |
Correct |
75 ms |
1540 KB |
Output is correct |
17 |
Correct |
69 ms |
2064 KB |
Output is correct |
18 |
Correct |
68 ms |
2152 KB |
Output is correct |
19 |
Correct |
82 ms |
2544 KB |
Output is correct |
20 |
Correct |
36 ms |
1052 KB |
Output is correct |
21 |
Correct |
38 ms |
1036 KB |
Output is correct |
22 |
Correct |
38 ms |
1064 KB |
Output is correct |
23 |
Correct |
42 ms |
1404 KB |
Output is correct |
24 |
Correct |
59 ms |
25412 KB |
Output is correct |
25 |
Correct |
59 ms |
27868 KB |
Output is correct |
26 |
Correct |
65 ms |
33644 KB |
Output is correct |
27 |
Correct |
69 ms |
40380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
197096 KB |
Output is correct |
2 |
Correct |
139 ms |
197232 KB |
Output is correct |
3 |
Correct |
148 ms |
197312 KB |
Output is correct |
4 |
Correct |
142 ms |
197196 KB |
Output is correct |
5 |
Correct |
126 ms |
25212 KB |
Output is correct |
6 |
Correct |
115 ms |
7060 KB |
Output is correct |
7 |
Runtime error |
1 ms |
1016 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |