#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 5003;
const long long INF = 1e15;
int n;
int a[N];
int f[N];
long long mn[N];
vector<int> g[N];
long long dp[N][N];
long long dp2[N][N];
void dfs(int v, int p) {
for (int u : g[v]) {
if (u != p) {
dfs(u, v);
}
}
fill(dp[v], dp[v] + N, INF);
fill(dp2[v], dp2[v] + N, INF);
for (int k = a[v] + 1; k <= n; ++k) {
dp[v][k] = dp2[v][k] = f[k];
for (int u : g[v]) {
if (u == p) {
continue;
}
long long val = mn[u];
if (a[v] + 1 == a[u] && a[u] < k) {
val = min(val, dp[u][k] - f[k]);
}
dp[v][k] += val;
if ((a[v] + 1 == a[u] && a[u] < k) || (a[u] == 0 && a[v] == k - 1)) {
val = min(val, dp2[u][k] - f[k]);
}
dp2[v][k] += val;
}
}
mn[v] = INF;
for (int k = 1; k <= n; ++k) {
assert(dp2[v][k] <= dp[v][k]);
mn[v] = min(mn[v], dp2[v][k]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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 = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
cout << mn[1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
393044 KB |
Output is correct |
2 |
Correct |
221 ms |
393296 KB |
Output is correct |
3 |
Correct |
211 ms |
393040 KB |
Output is correct |
4 |
Correct |
224 ms |
392948 KB |
Output is correct |
5 |
Correct |
220 ms |
393044 KB |
Output is correct |
6 |
Correct |
225 ms |
393216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
393044 KB |
Output is correct |
2 |
Correct |
221 ms |
393296 KB |
Output is correct |
3 |
Correct |
211 ms |
393040 KB |
Output is correct |
4 |
Correct |
224 ms |
392948 KB |
Output is correct |
5 |
Correct |
220 ms |
393044 KB |
Output is correct |
6 |
Correct |
225 ms |
393216 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
244 ms |
392572 KB |
Output is correct |
15 |
Correct |
239 ms |
392588 KB |
Output is correct |
16 |
Correct |
248 ms |
392528 KB |
Output is correct |
17 |
Correct |
247 ms |
392484 KB |
Output is correct |
18 |
Correct |
237 ms |
392596 KB |
Output is correct |
19 |
Correct |
229 ms |
392528 KB |
Output is correct |
20 |
Correct |
246 ms |
385872 KB |
Output is correct |
21 |
Correct |
233 ms |
392532 KB |
Output is correct |
22 |
Correct |
221 ms |
392308 KB |
Output is correct |
23 |
Correct |
223 ms |
392312 KB |
Output is correct |
24 |
Correct |
259 ms |
392528 KB |
Output is correct |
25 |
Correct |
216 ms |
384592 KB |
Output is correct |
26 |
Correct |
231 ms |
392528 KB |
Output is correct |
27 |
Correct |
225 ms |
392536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
393044 KB |
Output is correct |
2 |
Correct |
221 ms |
393296 KB |
Output is correct |
3 |
Correct |
211 ms |
393040 KB |
Output is correct |
4 |
Correct |
224 ms |
392948 KB |
Output is correct |
5 |
Correct |
220 ms |
393044 KB |
Output is correct |
6 |
Correct |
225 ms |
393216 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |