#include <bits/stdc++.h>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
vector <int> ke[mxn],V[mxn],q;
int n,p[mxn],l[mxn],last[mxn],r[mxn];
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]]++;
}
iota(r,r+n+1,0);
for (int i=1;i<=n;i++)
if (!last[i])
q.push_back(i);
while (!q.empty()){
int u=q.back();
q.pop_back();
ch[u]=1;
ke[p[u]].push_back(u);
r[f(u)]=f(p[u]);
if (!--last[p[u]])
q.push_back(p[u]);
}
for (int i=1;i<=n;i++)
if (f(i)==i){
dfs(i);
dfs2(i);
}
memset(last,0,sizeof(last));
for (int i=1;i<=n;i++){
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 (int j=last[i];;j=last[j]){
for (int u:V[j])
val=max(val,max(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 |
57688 KB |
Output is correct |
2 |
Correct |
12 ms |
57692 KB |
Output is correct |
3 |
Correct |
13 ms |
57804 KB |
Output is correct |
4 |
Correct |
14 ms |
57692 KB |
Output is correct |
5 |
Correct |
12 ms |
57692 KB |
Output is correct |
6 |
Correct |
12 ms |
57692 KB |
Output is correct |
7 |
Correct |
13 ms |
57688 KB |
Output is correct |
8 |
Correct |
12 ms |
57796 KB |
Output is correct |
9 |
Correct |
12 ms |
57688 KB |
Output is correct |
10 |
Correct |
12 ms |
57692 KB |
Output is correct |
11 |
Correct |
12 ms |
57692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
57944 KB |
Output is correct |
2 |
Correct |
13 ms |
57692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
57692 KB |
Output is correct |
2 |
Correct |
13 ms |
57944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
58600 KB |
Output is correct |
2 |
Correct |
23 ms |
61460 KB |
Output is correct |
3 |
Correct |
19 ms |
58972 KB |
Output is correct |
4 |
Correct |
16 ms |
58460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
62296 KB |
Output is correct |
2 |
Correct |
32 ms |
62432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
69836 KB |
Output is correct |
2 |
Correct |
56 ms |
67920 KB |
Output is correct |
3 |
Correct |
68 ms |
71956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
81360 KB |
Output is correct |
2 |
Correct |
115 ms |
88268 KB |
Output is correct |
3 |
Correct |
113 ms |
85584 KB |
Output is correct |
4 |
Correct |
147 ms |
97312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
101956 KB |
Output is correct |
2 |
Correct |
488 ms |
105920 KB |
Output is correct |
3 |
Correct |
173 ms |
101572 KB |
Output is correct |
4 |
Correct |
228 ms |
113460 KB |
Output is correct |
5 |
Correct |
213 ms |
110656 KB |
Output is correct |
6 |
Correct |
797 ms |
122380 KB |
Output is correct |
7 |
Correct |
213 ms |
114884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
205 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |