#include<bits/stdc++.h>
using namespace std;
#define N 1000100
#define ll long long
int deg[N];
#define ck(a,b) a=max(a,b)
#define Ck(a,b,c) a=max({a,b,c})
ll v[N], l[N], O[N], I[N];
ll solve(int x) {
ll mx1=O[x],mx2=O[x],sum=l[x],ans1=I[x],ans2=-1e18;
for(int y=v[x];y-x;sum+=l[y],deg[y]=0,y=v[y]) {
Ck(ans1,O[y]+sum+mx1,I[y]);
ck(ans2,O[y]-sum+mx2);
ck(mx1,O[y]-sum);
ck(mx2,O[y]+sum);
}
return max(ans1,ans2+sum);
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i]>>l[i],deg[v[i]]++;
queue<int> q;
for(int i=1;i<=n;i++)
if(!deg[i])q.push(i);
while (q.size()) {
int x=q.front(),y=v[x];
q.pop();
Ck(I[y],O[y]+O[x]+l[x],I[x]);
ck(O[y],O[x]+l[x]);
if(!--deg[y])q.push(y);
}
ll ans=0;
for(int i=1;i<=n;i++)
if(deg[i])
ans+=solve(i),deg[i]=0;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6540 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6608 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7000 KB |
Output is correct |
2 |
Correct |
8 ms |
7764 KB |
Output is correct |
3 |
Correct |
6 ms |
7168 KB |
Output is correct |
4 |
Correct |
3 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
8024 KB |
Output is correct |
2 |
Correct |
16 ms |
9036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
16452 KB |
Output is correct |
2 |
Correct |
29 ms |
14168 KB |
Output is correct |
3 |
Correct |
43 ms |
18488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
23380 KB |
Output is correct |
2 |
Correct |
73 ms |
31316 KB |
Output is correct |
3 |
Correct |
68 ms |
23768 KB |
Output is correct |
4 |
Correct |
122 ms |
37064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
40388 KB |
Output is correct |
2 |
Correct |
159 ms |
46024 KB |
Output is correct |
3 |
Correct |
120 ms |
44356 KB |
Output is correct |
4 |
Correct |
155 ms |
49868 KB |
Output is correct |
5 |
Correct |
140 ms |
49744 KB |
Output is correct |
6 |
Correct |
246 ms |
51516 KB |
Output is correct |
7 |
Correct |
154 ms |
52196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
50884 KB |
Output is correct |
2 |
Correct |
146 ms |
50876 KB |
Output is correct |
3 |
Correct |
149 ms |
36440 KB |
Output is correct |
4 |
Correct |
138 ms |
36020 KB |
Output is correct |
5 |
Correct |
145 ms |
50384 KB |
Output is correct |
6 |
Correct |
162 ms |
49716 KB |
Output is correct |
7 |
Correct |
230 ms |
51856 KB |
Output is correct |