#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-1]=0;
for (int j=L; j<R; ++j){
pf[j]=pf[j-1]+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 |
Incorrect |
5 ms |
31068 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
31068 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
31168 KB |
Output isn't correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Incorrect |
5 ms |
31068 KB |
Output isn't correct |
6 |
Incorrect |
4 ms |
31068 KB |
Output isn't correct |
7 |
Incorrect |
5 ms |
31068 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
31068 KB |
Output isn't correct |
9 |
Incorrect |
6 ms |
31068 KB |
Output isn't correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Incorrect |
4 ms |
31068 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
31068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
31324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
31836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
33116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
41048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
75 ms |
54096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
63748 KB |
Output is correct |
2 |
Incorrect |
295 ms |
83936 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
178 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |