#include <iostream>
#include <bitset>
#include <vector>
#include <queue>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
vector <int> ke[mxn],V[mxn];
queue <int> q;
int n,p[mxn],l[mxn],last[mxn],r[mxn],idx,x;
bitset <mxn> ch;
long long dp[mxn],dp2[mxn],d[mxn],val,res,a,b,sum,s;
pair <long long, int> mx={-1,0},mx2={-1,0};
void dfs(int u){
for (int v:ke[u]){
d[v]=d[u]+l[v];
dfs(v);
dp[u]=max(dp[u],dp[v]+l[v]);
}
}
void dfs2(int u){
mx={-1,0},mx2={-1,0};
for (int v:ke[u]){
auto tmp=make_pair(dp[v]+l[v],v);
if (tmp>mx){
mx2=mx;
mx=tmp;
}
else
mx2=max(mx2,tmp);
}
for (int v:ke[u])
dp2[v]=max(dp2[u],(mx.second==v?mx2.first:mx.first))+l[v];
for (int v:ke[u])
dfs2(v);
}
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()){
int u=q.front();
q.pop();
ch[u]=1;
r[f(u)]=f(p[u]);
if (!--last[p[u]])
q.push(p[u]);
}
for (int i=1;i<=n;i++)
if (ch[i])
ke[p[i]].push_back(i);
for (int i=1;i<=n;i++)
if (!ch[i]){
dfs(i);
dfs2(i);
}
for (int i=1;i<=n;i++){
last[i]=0;
vector <int>().swap(ke[i]);
V[f(i)].push_back(i);
}
for (int i=1;i<=n;i++){
if (ch[i])
continue;
sum=0,a=-INF,b=-INF;
int j=i;
while (true){
sum+=l[j];
ch[j]=1;
last[p[j]]=j;
j=p[j];
if (j==i)
break;
}
val=0,s=sum;
for (j=last[i];;j=last[j]){
for (int u:V[j])
val=max(val,max(max(dp[u],dp2[u]),d[u]+max(a-s,b+sum+s)));
a=max(a,dp[j]+s);
b=max(b,dp[j]-s);
s-=l[last[j]];
if (j==i)
break;
}
res+=val;
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
55896 KB |
Output is correct |
2 |
Correct |
12 ms |
55640 KB |
Output is correct |
3 |
Correct |
12 ms |
55644 KB |
Output is correct |
4 |
Correct |
11 ms |
55644 KB |
Output is correct |
5 |
Correct |
12 ms |
55500 KB |
Output is correct |
6 |
Correct |
12 ms |
55748 KB |
Output is correct |
7 |
Correct |
12 ms |
55644 KB |
Output is correct |
8 |
Correct |
12 ms |
55664 KB |
Output is correct |
9 |
Correct |
12 ms |
55644 KB |
Output is correct |
10 |
Correct |
12 ms |
55516 KB |
Output is correct |
11 |
Correct |
12 ms |
55644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
55644 KB |
Output is correct |
2 |
Correct |
12 ms |
55640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
55644 KB |
Output is correct |
2 |
Correct |
13 ms |
55896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
56152 KB |
Output is correct |
2 |
Correct |
22 ms |
58972 KB |
Output is correct |
3 |
Correct |
18 ms |
56668 KB |
Output is correct |
4 |
Correct |
15 ms |
56156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
59480 KB |
Output is correct |
2 |
Correct |
35 ms |
59480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
65512 KB |
Output is correct |
2 |
Correct |
59 ms |
64428 KB |
Output is correct |
3 |
Correct |
64 ms |
67152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
76964 KB |
Output is correct |
2 |
Correct |
112 ms |
84140 KB |
Output is correct |
3 |
Correct |
113 ms |
82516 KB |
Output is correct |
4 |
Correct |
143 ms |
92756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
92520 KB |
Output is correct |
2 |
Correct |
425 ms |
95276 KB |
Output is correct |
3 |
Correct |
188 ms |
94372 KB |
Output is correct |
4 |
Correct |
223 ms |
104740 KB |
Output is correct |
5 |
Correct |
211 ms |
102208 KB |
Output is correct |
6 |
Correct |
826 ms |
111220 KB |
Output is correct |
7 |
Correct |
221 ms |
105132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
201 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |