#include <bits/stdc++.h>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
queue <int> q;
int n,p[mxn],l[mxn],last[mxn],x,u;
bitset <mxn> ch;
long long dp[mxn],d[mxn],val,res,a,b,sum,s;
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++){
cin >> p[i] >> l[i];
last[p[i]]++;
}
for (int i=1;i<=n;i++)
if (!last[i])
q.push(i);
while (!q.empty()){
u=q.front();
q.pop();
ch[u]=1;
d[p[u]]=max(max(d[p[u]],d[u]),dp[p[u]]+dp[u]+l[u]);
dp[p[u]]=max(dp[p[u]],dp[u]+l[u]);
if (!--last[p[u]])
q.push(p[u]);
}
memset(last,0,sizeof(last));
for (int i=1;i<=n;i++){
if (ch[i])
continue;
sum=0,a=b=-INF;
x=i;
while (true){
sum+=l[x];
ch[x]=1;
last[p[x]]=x;
x=p[x];
if (x==i)
break;
}
val=0,s=sum;
for (x=last[i];;x=last[x]){
val=max(max(val,d[x]),dp[x]+max(a-s,b+sum+s));
a=max(a,dp[x]+s);
b=max(b,dp[x]-s);
s-=l[last[x]];
if (x==i)
break;
}
res+=val;
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8792 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8796 KB |
Output is correct |
2 |
Correct |
6 ms |
9308 KB |
Output is correct |
3 |
Correct |
4 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9564 KB |
Output is correct |
2 |
Correct |
14 ms |
9948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12124 KB |
Output is correct |
2 |
Correct |
34 ms |
9940 KB |
Output is correct |
3 |
Correct |
37 ms |
12760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
18260 KB |
Output is correct |
2 |
Correct |
73 ms |
21588 KB |
Output is correct |
3 |
Correct |
67 ms |
14048 KB |
Output is correct |
4 |
Correct |
101 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
16584 KB |
Output is correct |
2 |
Correct |
146 ms |
23124 KB |
Output is correct |
3 |
Correct |
107 ms |
25428 KB |
Output is correct |
4 |
Correct |
145 ms |
29640 KB |
Output is correct |
5 |
Correct |
138 ms |
29776 KB |
Output is correct |
6 |
Correct |
195 ms |
28980 KB |
Output is correct |
7 |
Correct |
151 ms |
29400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
27856 KB |
Output is correct |
2 |
Correct |
148 ms |
27732 KB |
Output is correct |
3 |
Correct |
133 ms |
13276 KB |
Output is correct |
4 |
Correct |
130 ms |
13392 KB |
Output is correct |
5 |
Correct |
135 ms |
29140 KB |
Output is correct |
6 |
Correct |
136 ms |
29144 KB |
Output is correct |
7 |
Correct |
204 ms |
29136 KB |
Output is correct |