#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int inf = 1e9;
int N,P[maxn],L[maxn],dp[maxn],mx[maxn],res;
int deg[maxn];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> N;
for(int i=1;i<=N;i++){
cin >> P[i] >> L[i];
deg[P[i]]++;
}
queue<int> q;
for(int i=1;i<=N;i++) if(!deg[i]) q.push(i);
while(!q.empty()){
int u=q.front(),p=P[u];q.pop();
mx[p]=max({mx[p],mx[u],dp[p]+dp[u]+L[u]});
dp[p]=max(dp[p],dp[u]+L[u]);
if(!(--deg[p])) q.push(p);
}
for(int i=1;i<=N;i++){
if(!deg[i]) continue;
vector<int> cycle;
int u=i,ans=0,total=0;
while(deg[u]){
ans=max(ans,mx[u]);
total+=L[u];
cycle.push_back(u);
deg[u]=0;u=P[u];
}
int m1=-inf,m2=-inf;
for(int v:cycle){
ans=max({ans,m1+dp[v],m2+dp[v]+total});
m1=max(m1,dp[v])+L[v];
m2=max(m2,dp[v])-L[v];
}
res+=ans;
}
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
4560 KB |
Output isn't correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
13128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
19060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
31308 KB |
Output is correct |
2 |
Incorrect |
135 ms |
34948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
35328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |