Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <array>
- #include <set>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 6e3 + 100;
- const int N = 1e5 + 10;
- array<array<int, MAXN>, 2> dp;
- array<int, MAXN> cost;
- vector<vector<int>> g;
- void dfs(int v) {
- for (auto to: g[v]) {
- dfs(to);
- dp[0][v] += max(dp[0][to], dp[1][to]);
- dp[1][v] += dp[0][to];
- }
- dp[1][v] += cost[v];
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> cost[i];
- }
- g.resize(n);
- set<int> st;
- for(int i = 0; i < n; ++i) {
- st.insert(i);
- }
- while (true) {
- int u, v;
- cin >> u >> v;
- if (u == 0 && v == 0) {
- break;
- }
- --u, --v;
- g[v].emplace_back(u);
- st.erase(u);
- }
- int root = *st.begin();
- dfs(root);
- cout << max(dp[0][root], dp[1][root]) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment