#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],r[mxn],x,u;
bitset <mxn> ch;
long long dp[mxn],d[mxn],val,res,a,b,sum,s;
int f(int i){
return (r[i]==i?i:r[i]=f(r[i]));
}
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]]++;
r[i]=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;
r[f(u)]=f(p[u]);
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=-INF,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 |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10716 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
8 ms |
11356 KB |
Output is correct |
3 |
Correct |
5 ms |
11084 KB |
Output is correct |
4 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
11612 KB |
Output is correct |
2 |
Correct |
15 ms |
11832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
14164 KB |
Output is correct |
2 |
Correct |
28 ms |
14172 KB |
Output is correct |
3 |
Correct |
38 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
22356 KB |
Output is correct |
2 |
Correct |
67 ms |
25688 KB |
Output is correct |
3 |
Correct |
66 ms |
18256 KB |
Output is correct |
4 |
Correct |
110 ms |
27604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
21460 KB |
Output is correct |
2 |
Correct |
153 ms |
27220 KB |
Output is correct |
3 |
Correct |
110 ms |
29520 KB |
Output is correct |
4 |
Correct |
138 ms |
33620 KB |
Output is correct |
5 |
Correct |
146 ms |
33996 KB |
Output is correct |
6 |
Correct |
212 ms |
32900 KB |
Output is correct |
7 |
Correct |
148 ms |
33492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
46412 KB |
Output is correct |
2 |
Correct |
147 ms |
47184 KB |
Output is correct |
3 |
Correct |
140 ms |
32776 KB |
Output is correct |
4 |
Correct |
138 ms |
32208 KB |
Output is correct |
5 |
Correct |
145 ms |
46544 KB |
Output is correct |
6 |
Correct |
144 ms |
45492 KB |
Output is correct |
7 |
Correct |
220 ms |
48072 KB |
Output is correct |