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;
typedef long long ll;
const int MAXN = 5e3 + 500;
const ll INF = 1e18;
int n;
int val[MAXN], fk[MAXN];
vector<int> susjedi[MAXN], v;
int d[MAXN], parent[MAXN];
ll dp[MAXN][MAXN], najm[MAXN];
void dfs (int x) {
for (auto nx: susjedi[x]) {
if (parent[x] == nx) continue;
d[nx] = d[x] + 1;
parent[nx] = x;
dfs (nx);
}
}
bool cmp (int x, int y) {
return d[x] > d[y];
}
int main () {
ios_base::sync_with_stdio (false);
cin.tie (0);
cin >> n;
for (int i = 0; i < n; i++) cin >> val[i];
for (int i = 0; i < n; i++) cin >> fk[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
susjedi[a - 1].push_back (b - 1);
susjedi[b - 1].push_back (a - 1);
}
dfs (0);
for (int i = 0; i < n; i++) v.push_back (i);
sort (v.begin (), v.end (), cmp);
for (auto x: v) {
for (int i = 0; i < n; i++) {
if (val[x] > i) {
dp[x][i] = INF;
continue;
}
dp[x][i] = fk[i];
for (auto nx: susjedi[x]) {
if (parent[x] == nx) continue;
if ((val[x] + 1 == val[nx] && val[nx] <= i) || (val[x] == i && val[nx] == 0)) dp[x][i] += min (dp[nx][i] - fk[i], najm[nx]);
else dp[x][i] += najm[nx];
}
}
najm[x] = INF;
for (int i = 0; i < n; i++) najm[x] = min (najm[x], dp[x][i]);
}
cout << najm[0];
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... |