#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define w (a[e][1])
#define V() (e == u ? a[e][0] : e)
#define v V()
const int LIM = 1e6;
vector<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM];
ll d[LIM], s[LIM], best;
bitset<LIM> vis;
void dfs(int u){
vis[u] = 1, s[u] = d[u];
ll curr = 0;
for(int e : g[u]) if(e != r && !vis[v]){
p[v] = u;
d[v] = d[u] + (ll)w;
dfs(v);
s[u] = max(s[u], s[v]);
best = max(best, curr + s[v] - d[u]);
curr = max(curr, s[v] - d[u]);
}
}
void findCycle(int u, int par){
vis[u] = 1;
for(int e : g[u]) if(e != par){
if(vis[v]) return void(r = e + (vis[u] = 0));
findCycle(v, e);
if(r >= 0) break;
}
vis[u] = 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; ++i){
cin >> a[i][0] >> a[i][1]; --a[i][0];
g[a[i][0]].push_back(i);
g[i].push_back(i);
}
fill(p, p+n, -1);
ll ans = 0;
for(int z=0; z<n; ++z){
if(vis[z]) continue;
r = -1, best = 0;
findCycle(z, -1);
assert(r >= 0);
dfs(r);
int u = a[r][0], last = -1;
while(1){
vis[u] = 0;
s[u] = d[u];
for(int e : g[u])
if(e != r && v != p[u] && v != last) s[u] = max(s[u], s[v]);
if(u == r) break;
last = u;
u = p[u];
}
u = r;
while(1){
vis[u] = 1;
for(int e : g[u])
if(e != r && v != p[u] && !vis[v]) last = v;
s[last] = max(s[last], s[u]);
if(u == a[r][0]) break;
u = last;
last = -1;
}
u = a[r][0], last = -1;
ll res = 0;
while(u != r){
ll mx = 0;
for(int e : g[u])
if(e != r && v != p[u] && v != last) mx = max(mx, s[v] - d[u]);
res = max(res, d[a[r][0]] - d[u] + mx + s[p[u]]);
last = u;
u = p[u];
}
res += a[r][1];
ans += max(best, res);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23828 KB |
Output is correct |
2 |
Correct |
16 ms |
23796 KB |
Output is correct |
3 |
Correct |
17 ms |
23876 KB |
Output is correct |
4 |
Correct |
15 ms |
23716 KB |
Output is correct |
5 |
Correct |
17 ms |
23744 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
15 ms |
23756 KB |
Output is correct |
9 |
Correct |
15 ms |
23820 KB |
Output is correct |
10 |
Correct |
17 ms |
23728 KB |
Output is correct |
11 |
Correct |
18 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23924 KB |
Output is correct |
2 |
Correct |
19 ms |
23912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23884 KB |
Output is correct |
2 |
Correct |
16 ms |
24200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24752 KB |
Output is correct |
2 |
Correct |
35 ms |
26936 KB |
Output is correct |
3 |
Correct |
27 ms |
25036 KB |
Output is correct |
4 |
Correct |
21 ms |
24404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
28316 KB |
Output is correct |
2 |
Correct |
55 ms |
31096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
37320 KB |
Output is correct |
2 |
Correct |
111 ms |
43232 KB |
Output is correct |
3 |
Correct |
143 ms |
50184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
48568 KB |
Output is correct |
2 |
Correct |
247 ms |
68884 KB |
Output is correct |
3 |
Correct |
275 ms |
79552 KB |
Output is correct |
4 |
Correct |
323 ms |
91616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
377 ms |
86472 KB |
Output is correct |
2 |
Correct |
1091 ms |
127176 KB |
Output is correct |
3 |
Correct |
311 ms |
72312 KB |
Output is correct |
4 |
Correct |
449 ms |
106568 KB |
Output is correct |
5 |
Correct |
445 ms |
106240 KB |
Output is correct |
6 |
Correct |
1222 ms |
80952 KB |
Output is correct |
7 |
Correct |
490 ms |
131072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
399 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |