#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];
void dfs(int v, int p) {
for (int u : g[v]) {
if (u != p) {
dfs(u, v);
}
}
fill(dp[v], dp[v] + N, INF);
for (int k = a[v] + 1; k <= n; ++k) {
dp[v][k] = f[k];
for (int u : g[v]) {
if (u == p) {
continue;
}
long long val = mn[u];
if ((a[v] < a[u] && a[u] < k) || (a[u] == 0 && a[v] == k - 1)) {
val = min(val, dp[u][k] - f[k]);
}
dp[v][k] += val;
}
}
mn[v] = INF;
for (int k = 1; k <= n; ++k) {
mn[v] = min(mn[v], dp[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 |
128 ms |
196948 KB |
Output is correct |
2 |
Correct |
128 ms |
197020 KB |
Output is correct |
3 |
Correct |
112 ms |
196944 KB |
Output is correct |
4 |
Correct |
122 ms |
196948 KB |
Output is correct |
5 |
Incorrect |
121 ms |
196944 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
196948 KB |
Output is correct |
2 |
Correct |
128 ms |
197020 KB |
Output is correct |
3 |
Correct |
112 ms |
196944 KB |
Output is correct |
4 |
Correct |
122 ms |
196948 KB |
Output is correct |
5 |
Incorrect |
121 ms |
196944 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
196948 KB |
Output is correct |
2 |
Correct |
128 ms |
197020 KB |
Output is correct |
3 |
Correct |
112 ms |
196944 KB |
Output is correct |
4 |
Correct |
122 ms |
196948 KB |
Output is correct |
5 |
Incorrect |
121 ms |
196944 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |