# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
888607 | fanwen | Nestabilnost (COI23_nestabilnost) | C++17 | 194 ms | 206648 KiB |
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>
#define fi first
#define se second
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
const int MAX = 3e5 + 5;
int n, a[MAX], f[MAX];
vector <int> adj[MAX];
namespace sub1 {
bool check() {
return n <= 5000;
}
long long dp[5005][5005], ans[MAX];
void dfs(int u, int p) {
for (int i = a[u] + 1; i <= n; ++i) dp[u][i] = f[i];
for (auto v : adj[u]) if(v != p) {
dfs(v, u);
for (int k = a[u] + 1; k <= n; ++k) {
if(a[v] == (a[u] + 1) % k) {
dp[u][k] += min(ans[v], dp[v][k] - f[k]);
} else {
dp[u][k] += ans[v];
}
}
}
ans[u] = *min_element(dp[u] + 1, dp[u] + n + 1);
}
void solve() {
memset(dp, 0x3f, sizeof dp);
dfs(1, 0);
cout << ans[1]; exit(0);
}
}
void you_make_it(void) {
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;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(sub1::check()) sub1::solve();
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("nestabilnost");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
# | 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... |