This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |