#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] < a[u] && a[u] < k) {
val = min(val, dp[u][k] - f[k]);
}
dp[v][k] += val;
if ((a[v] < 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
393040 KB |
Output is correct |
2 |
Correct |
210 ms |
392952 KB |
Output is correct |
3 |
Correct |
212 ms |
393044 KB |
Output is correct |
4 |
Correct |
205 ms |
393040 KB |
Output is correct |
5 |
Incorrect |
219 ms |
393044 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
393040 KB |
Output is correct |
2 |
Correct |
210 ms |
392952 KB |
Output is correct |
3 |
Correct |
212 ms |
393044 KB |
Output is correct |
4 |
Correct |
205 ms |
393040 KB |
Output is correct |
5 |
Incorrect |
219 ms |
393044 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
393040 KB |
Output is correct |
2 |
Correct |
210 ms |
392952 KB |
Output is correct |
3 |
Correct |
212 ms |
393044 KB |
Output is correct |
4 |
Correct |
205 ms |
393040 KB |
Output is correct |
5 |
Incorrect |
219 ms |
393044 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |