#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];
ll dp[MAXN][MAXN], najm[MAXN];
void dfs (int x) {
for (auto nx: susjedi[x]) {
d[nx] = d[x] + 1;
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);
}
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 ((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 |
1 |
Correct |
193 ms |
216716 KB |
Output is correct |
2 |
Correct |
123 ms |
216664 KB |
Output is correct |
3 |
Correct |
115 ms |
216624 KB |
Output is correct |
4 |
Correct |
142 ms |
216592 KB |
Output is correct |
5 |
Correct |
120 ms |
216660 KB |
Output is correct |
6 |
Correct |
113 ms |
216668 KB |
Output is correct |
# |
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 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
216716 KB |
Output is correct |
2 |
Correct |
123 ms |
216664 KB |
Output is correct |
3 |
Correct |
115 ms |
216624 KB |
Output is correct |
4 |
Correct |
142 ms |
216592 KB |
Output is correct |
5 |
Correct |
120 ms |
216660 KB |
Output is correct |
6 |
Correct |
113 ms |
216668 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
216716 KB |
Output is correct |
2 |
Correct |
123 ms |
216664 KB |
Output is correct |
3 |
Correct |
115 ms |
216624 KB |
Output is correct |
4 |
Correct |
142 ms |
216592 KB |
Output is correct |
5 |
Correct |
120 ms |
216660 KB |
Output is correct |
6 |
Correct |
113 ms |
216668 KB |
Output is correct |
7 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |