#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC target("popcnt")
using namespace __gnu_pbds;
using namespace std;
template<typename T> using ordered_set = tree<T, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update>;
#define popcount __builtin_popcountll
#define all(a) (a).begin(), (a).end()
vector<pair<int,int>> adj[1'000'000];
int par[1'000'000], len[1'000'000], n;
void dfs(int u);
pair<long long, long long> longest(int u, int p1, int p2) {
long long mx1 = 0, mx2 = 0;
long long dia = 0;
for (auto [v, w] : adj[u]) if (v != p1 && v != p2) {
auto [a, b] = longest(v, u, u);
a += w;
dia = max(dia, b);
if (a >= mx1) {
swap(mx1, mx2);
mx1 = a;
} else mx2 = max(mx2, a);
}
dia = max(dia, mx1 + mx2);
return make_pair(mx1, dia);
}
long long solve_comp(int start) {
int fast = start, slow = start;
do {
fast = par[par[fast]];
slow = par[slow];
} while (fast != slow);
vector<int> cycle;
cycle.reserve(n);
vector<long long> pref { 0 };
pref.reserve(n+1);
do {
cycle.push_back(slow);
pref.push_back(pref.back() + len[slow]);
slow = par[slow];
} while (fast != slow);
int k = cycle.size();
long long ans = 0;
vector<long long> vals(k);
for (int i = 0; i < k; i++) {
auto [a, b] = longest(cycle[i], cycle[(i + 1) % k], cycle[(i - 1 + k) % k]);
vals[i] = a;
ans = max(ans, b);
}
// j -> i = vals[i] + vals[j] + pref[i] - pref[j] -> (vals[i] + pref[i]) + (vals[j] - pref[j])
long long mx = -1e18;
for (int i = 0; i < k; i++) {
ans = max(ans, vals[i] + pref[i] + mx);
mx = max(mx, vals[i] - pref[i]);
}
// i -> j = vals[i] + vals[j] + L - (pref[i] - pref[j]) -> L + (vals[i] - pref[i]) + (vals[j] + pref[j])
long long L = pref.back();
mx = -1e18;
for (int i = 0; i < k; i++) {
ans = max(ans, L + vals[i] - pref[i] + mx);
mx = max(mx, vals[i] + pref[i]);
}
for (int u : cycle) dfs(u);
return ans;
}
void dfs(int u) {
len[u] = -1;
for (auto [v, w] : adj[u]) if (len[v] != -1) dfs(v);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> par[i] >> len[i];
par[i]--;
adj[par[i]].emplace_back(i, len[i]);
}
long long ans = 0;
for (int i = 0; i < n; i++) if (len[i] != -1) {
ans += solve_comp(i);
}
cout << ans;
}
# | 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... |
# | 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... |