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 = 5002;
int a[N];
int f[N];
int suf[N];
long long dp[N];
vector<int> g[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
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 = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
suf[n] = f[n];
for (int i = n - 1; i >= 1; --i) {
suf[i] = min(suf[i + 1], f[i]);
}
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1] + suf[a[i] + 1];
int mx = a[i], d = -1;
for (int j = i - 1; j > 0; --j) {
if (a[j + 1] > a[j] + 1) {
break;
}
if (a[j + 1] == 0) {
if (d != -1 && d != a[j]) break;
d = a[j] + 1;
} else if (a[j + 1] <= a[j]) {
break;
}
mx = max(mx, a[j]);
if (d != -1 && mx >= d) {
break;
}
dp[i] = min(dp[i], dp[j - 1] + (d != -1 ? f[d] : suf[mx + 1]));
}
//debug(i, dp[i]);
}
cout << dp[n] << '\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... |