#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 |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6740 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7268 KB |
Output is correct |
2 |
Correct |
6 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8024 KB |
Output is correct |
2 |
Correct |
15 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
16464 KB |
Output is correct |
2 |
Correct |
28 ms |
14164 KB |
Output is correct |
3 |
Correct |
40 ms |
18512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
23488 KB |
Output is correct |
2 |
Correct |
70 ms |
31284 KB |
Output is correct |
3 |
Correct |
73 ms |
23636 KB |
Output is correct |
4 |
Correct |
96 ms |
37196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
40644 KB |
Output is correct |
2 |
Correct |
168 ms |
46160 KB |
Output is correct |
3 |
Correct |
115 ms |
44376 KB |
Output is correct |
4 |
Correct |
152 ms |
49880 KB |
Output is correct |
5 |
Correct |
141 ms |
49956 KB |
Output is correct |
6 |
Correct |
235 ms |
51552 KB |
Output is correct |
7 |
Correct |
151 ms |
52304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
51220 KB |
Output is correct |
2 |
Correct |
144 ms |
51024 KB |
Output is correct |
3 |
Correct |
144 ms |
36568 KB |
Output is correct |
4 |
Correct |
139 ms |
36044 KB |
Output is correct |
5 |
Correct |
140 ms |
50256 KB |
Output is correct |
6 |
Correct |
156 ms |
49520 KB |
Output is correct |
7 |
Correct |
228 ms |
51792 KB |
Output is correct |