#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n, vis[N], par[N], wpar[N], on_cycle[N];
vector<pair<int, int>> g[N];
long long pf[N], f[N][2];
int cycle[N];
void dfs(int u){
vis[u]=1;
long long mx1=-1e18, mx2=-1e18;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (on_cycle[v]) continue;
dfs(v);
long long val=f[v][0]+w;
f[u][0]=max(f[u][0], val);
f[u][1]=max(f[u][1], f[v][1]);
if (mx1<val) mx2=mx1, mx1=val;
else mx2=max(mx2, val);
}
f[u][1]=max({f[u][1], f[u][0], mx1+mx2});
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i=1; i<=n; ++i){
int j, k; cin >> j >> k;
g[j].emplace_back(i, k);
par[i]=j;
wpar[i]=k;
}
long long ans=0;
for (int i=1; i<=n; ++i) if (!vis[i]){
int L=0, R=0;
long long cur=0;
int u=i;
while (1){
if (vis[u]) break;
vis[u]=1;
cycle[R++]=u;
u=par[u];
}
L=find(cycle+L, cycle+R, u)-cycle;
for (int j=L; j<R; ++j) on_cycle[cycle[j]]=1;
for (int j=L; j<R; ++j){
dfs(cycle[j]);
cur=max(cur, f[cycle[j]][1]);
}
pf[L]=0;
for (int j=L; j<R; ++j){
pf[j+1]=pf[j]+wpar[cycle[j]];
}
long long sum=pf[R];
long long mx1=-1e18, mx2=-1e18;
for (int j=L; j<R; ++j){
cur=max(cur, f[cycle[j]][0]+pf[j]+mx1);
cur=max(cur, f[cycle[j]][0]-pf[j]+mx2);
mx1=max(mx1, f[cycle[j]][0]-pf[j]);
mx2=max(mx2, f[cycle[j]][0]+sum+pf[j]);
}
ans+=cur;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31196 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
31188 KB |
Output is correct |
5 |
Correct |
5 ms |
31252 KB |
Output is correct |
6 |
Correct |
4 ms |
31064 KB |
Output is correct |
7 |
Correct |
4 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31068 KB |
Output is correct |
9 |
Correct |
5 ms |
31068 KB |
Output is correct |
10 |
Correct |
5 ms |
31068 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
5 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31324 KB |
Output is correct |
2 |
Correct |
5 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
31836 KB |
Output is correct |
2 |
Correct |
14 ms |
32588 KB |
Output is correct |
3 |
Correct |
10 ms |
32092 KB |
Output is correct |
4 |
Correct |
8 ms |
31688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33116 KB |
Output is correct |
2 |
Correct |
19 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41028 KB |
Output is correct |
2 |
Correct |
42 ms |
48724 KB |
Output is correct |
3 |
Correct |
47 ms |
48980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
54052 KB |
Output is correct |
2 |
Correct |
78 ms |
59984 KB |
Output is correct |
3 |
Correct |
91 ms |
71648 KB |
Output is correct |
4 |
Correct |
114 ms |
66132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
63628 KB |
Output is correct |
2 |
Correct |
279 ms |
84052 KB |
Output is correct |
3 |
Correct |
132 ms |
69712 KB |
Output is correct |
4 |
Correct |
164 ms |
80208 KB |
Output is correct |
5 |
Correct |
164 ms |
78108 KB |
Output is correct |
6 |
Correct |
497 ms |
75604 KB |
Output is correct |
7 |
Correct |
171 ms |
83284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
182 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |