#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n, vis[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];
int stk[N], LL, RR;
void dfs(int _u){
LL=0; RR=0;
stk[RR++]=_u;
while (RR-LL){
int u=stk[RR-1];
if (vis[u]){
--RR;
long long mx1=-1e18, mx2=-1e18;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (on_cycle[v]) continue;
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});
continue;
}
vis[u]=1;
for (auto &e:g[u]){
int v=e.first;
if (on_cycle[v]) continue;
stk[RR++]=v;
}
}
}
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 |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
5 ms |
33240 KB |
Output is correct |
4 |
Correct |
5 ms |
33236 KB |
Output is correct |
5 |
Correct |
5 ms |
33116 KB |
Output is correct |
6 |
Correct |
5 ms |
33116 KB |
Output is correct |
7 |
Correct |
5 ms |
33116 KB |
Output is correct |
8 |
Correct |
5 ms |
33116 KB |
Output is correct |
9 |
Correct |
5 ms |
33116 KB |
Output is correct |
10 |
Correct |
4 ms |
33284 KB |
Output is correct |
11 |
Correct |
5 ms |
33112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33372 KB |
Output is correct |
2 |
Correct |
6 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33880 KB |
Output is correct |
2 |
Correct |
13 ms |
34596 KB |
Output is correct |
3 |
Correct |
10 ms |
34140 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35164 KB |
Output is correct |
2 |
Correct |
20 ms |
37800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41868 KB |
Output is correct |
2 |
Correct |
44 ms |
47188 KB |
Output is correct |
3 |
Correct |
51 ms |
47824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
57552 KB |
Output is correct |
2 |
Correct |
83 ms |
63572 KB |
Output is correct |
3 |
Correct |
97 ms |
75772 KB |
Output is correct |
4 |
Correct |
106 ms |
70484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
69012 KB |
Output is correct |
2 |
Correct |
294 ms |
87924 KB |
Output is correct |
3 |
Correct |
131 ms |
73812 KB |
Output is correct |
4 |
Correct |
164 ms |
85828 KB |
Output is correct |
5 |
Correct |
160 ms |
83772 KB |
Output is correct |
6 |
Correct |
408 ms |
80724 KB |
Output is correct |
7 |
Correct |
166 ms |
87388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
96620 KB |
Output is correct |
2 |
Correct |
200 ms |
90448 KB |
Output is correct |
3 |
Correct |
171 ms |
103764 KB |
Output is correct |
4 |
Correct |
177 ms |
94292 KB |
Output is correct |
5 |
Correct |
157 ms |
83792 KB |
Output is correct |
6 |
Correct |
168 ms |
86352 KB |
Output is correct |
7 |
Correct |
432 ms |
81492 KB |
Output is correct |