Submission #1059741

#TimeUsernameProblemLanguageResultExecution timeMemory
1059741NeroZeinNestabilnost (COI23_nestabilnost)C++17
0 / 100
2 ms860 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...