#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];
stack<int> stk;
void dfs(int _u){
stk.push(_u);
while (stk.size()){
int u=stk.top();
if (vis[u]){
stk.pop();
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.push(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 |
4 ms |
31068 KB |
Output is correct |
2 |
Correct |
5 ms |
31244 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31184 KB |
Output is correct |
7 |
Correct |
4 ms |
31068 KB |
Output is correct |
8 |
Correct |
4 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31320 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 |
31832 KB |
Output is correct |
2 |
Correct |
12 ms |
32728 KB |
Output is correct |
3 |
Correct |
9 ms |
32092 KB |
Output is correct |
4 |
Correct |
6 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33368 KB |
Output is correct |
2 |
Correct |
20 ms |
35684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
41940 KB |
Output is correct |
2 |
Correct |
46 ms |
49492 KB |
Output is correct |
3 |
Correct |
48 ms |
52052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
55636 KB |
Output is correct |
2 |
Correct |
91 ms |
61788 KB |
Output is correct |
3 |
Correct |
94 ms |
73812 KB |
Output is correct |
4 |
Correct |
108 ms |
68692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
68788 KB |
Output is correct |
2 |
Correct |
301 ms |
85904 KB |
Output is correct |
3 |
Correct |
137 ms |
72764 KB |
Output is correct |
4 |
Correct |
172 ms |
84304 KB |
Output is correct |
5 |
Correct |
161 ms |
82004 KB |
Output is correct |
6 |
Correct |
453 ms |
78956 KB |
Output is correct |
7 |
Correct |
168 ms |
87120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
96852 KB |
Output is correct |
2 |
Correct |
198 ms |
103764 KB |
Output is correct |
3 |
Correct |
179 ms |
117588 KB |
Output is correct |
4 |
Correct |
177 ms |
107088 KB |
Output is correct |
5 |
Correct |
163 ms |
95316 KB |
Output is correct |
6 |
Correct |
185 ms |
96848 KB |
Output is correct |
7 |
Correct |
448 ms |
94828 KB |
Output is correct |