#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=1e6+5;
int n;
pair<int,int> nxt[N];
int deg[N];
queue<int> q;
ll dp[N],dp2[N],dia[N];
ll ans;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for(int i=1;i<=n;i++){
auto &[v,w]=nxt[i];
cin >> v >> w;
deg[v]++;
}
for(int i=1;i<=n;i++)if(!deg[i])q.emplace(i);
while(!q.empty()){
int u=q.front();
q.pop();
auto [v,w]=nxt[u];
dp2[v]=max(dp2[v],dp[u]+w);
if(dp[v]<dp2[v])swap(dp[v],dp2[v]);
dia[v]=max({dia[v],dia[u],dp[v]+dp2[v]});
if(--deg[v]==0)q.emplace(v);
}
for(int i=1;i<=n;i++)if(deg[i]){
vector<pair<int,ll>> comp;
ll len=0;
for(int u=i;deg[u];){
comp.emplace_back(u,len);
deg[u]=0;
auto [v,w]=nxt[u];
len+=w;
u=v;
}
ll res=0;
deque<pair<ll,int>> dq;
for(auto [u,w]:comp){
res=max(res,dia[u]);
ll val=dp[u]-w;
while(!dq.empty()&&dq.back().first<val)dq.pop_back();
dq.emplace_back(val,u);
}
for(auto [u,w]:comp){
if(dq.front().second==u)dq.pop_front();
res=max(res,w+len+dp[u]+dq.front().first);
ll val=dp[u]-w-len;
while(!dq.empty()&&dq.back().first<val)dq.pop_back();
dq.emplace_back(val,u);
}
ans+=res;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
472 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
524 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
6 ms |
2460 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3544 KB |
Output is correct |
2 |
Correct |
15 ms |
5316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
10740 KB |
Output is correct |
2 |
Correct |
32 ms |
10396 KB |
Output is correct |
3 |
Correct |
46 ms |
17188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
17748 KB |
Output is correct |
2 |
Correct |
76 ms |
27756 KB |
Output is correct |
3 |
Correct |
77 ms |
22604 KB |
Output is correct |
4 |
Correct |
148 ms |
42804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
24404 KB |
Output is correct |
2 |
Correct |
168 ms |
47972 KB |
Output is correct |
3 |
Correct |
117 ms |
36696 KB |
Output is correct |
4 |
Correct |
162 ms |
55060 KB |
Output is correct |
5 |
Correct |
152 ms |
55352 KB |
Output is correct |
6 |
Correct |
196 ms |
47188 KB |
Output is correct |
7 |
Correct |
170 ms |
66232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
47700 KB |
Output is correct |
2 |
Correct |
147 ms |
47600 KB |
Output is correct |
3 |
Correct |
164 ms |
56496 KB |
Output is correct |
4 |
Correct |
159 ms |
23636 KB |
Output is correct |
5 |
Correct |
153 ms |
55136 KB |
Output is correct |
6 |
Correct |
150 ms |
48716 KB |
Output is correct |
7 |
Correct |
200 ms |
47696 KB |
Output is correct |