#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int MX = 1e6+10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, a[MX], w[MX];
vector<int> adj[MX];
ll ans, ret;
vector<ll> V, S;
bool vv[MX];
inline int cycle(int u) {
if(vv[u]) return u;
vv[u] = 1;
int r = cycle(a[u]);
if(r) {
V.push_back(u);
S.push_back(w[u]);
if(r==u) return 0;
else return r;
}
vv[u] = 0;
return 0;
}
inline ll dfs(int u) {
ll mx = 0;
vv[u] = 1;
for(int &v : adj[u]) {
if(!vv[v]) {
ll r = dfs(v) + w[v];
ret = max(ret, mx+r);
mx = max(mx, r);
}
}
return mx;
}
inline void solve(int u) {
V.clear(); S.clear();
cycle(u);
reverse(V.begin(), V.end());
reverse(S.begin(), S.end());
for(auto &v : V) v = dfs(v);
m = V.size();
for(int i = m-1; i > 0; --i) swap(S[i], S[i-1]);
for(int i = 1; i < m; ++i) S[i] += S[i-1];
ll m1 = -INF, m2 = -INF;
for(int i = 0; i < m; ++i) {
ret = max(ret, max(m1+S[i]+V[i], m2-S[i]+V[i]+S[m-1]));
m1 = max(m1, V[i]-S[i]);
m2 = max(m2, V[i]+S[i]);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i] >> w[i];
adj[a[i]].push_back(i);
}
for(int i = 1; i <= n; ++i)
if(!vv[i]) solve(i), ans+=ret, ret = 0;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
17 ms |
23788 KB |
Output is correct |
5 |
Correct |
17 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23788 KB |
Output is correct |
8 |
Correct |
17 ms |
23788 KB |
Output is correct |
9 |
Correct |
17 ms |
23808 KB |
Output is correct |
10 |
Correct |
17 ms |
23800 KB |
Output is correct |
11 |
Correct |
17 ms |
23788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23936 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23916 KB |
Output is correct |
2 |
Correct |
21 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24428 KB |
Output is correct |
2 |
Correct |
30 ms |
25964 KB |
Output is correct |
3 |
Correct |
24 ms |
24428 KB |
Output is correct |
4 |
Correct |
21 ms |
24136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
26856 KB |
Output is correct |
2 |
Correct |
46 ms |
28520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
30828 KB |
Output is correct |
2 |
Correct |
84 ms |
38792 KB |
Output is correct |
3 |
Correct |
107 ms |
43400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
37228 KB |
Output is correct |
2 |
Correct |
170 ms |
52048 KB |
Output is correct |
3 |
Correct |
198 ms |
68048 KB |
Output is correct |
4 |
Correct |
248 ms |
72448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
36700 KB |
Output is correct |
2 |
Correct |
589 ms |
88136 KB |
Output is correct |
3 |
Correct |
238 ms |
46956 KB |
Output is correct |
4 |
Correct |
318 ms |
75216 KB |
Output is correct |
5 |
Correct |
324 ms |
73088 KB |
Output is correct |
6 |
Correct |
727 ms |
51476 KB |
Output is correct |
7 |
Correct |
361 ms |
100808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
125292 KB |
Output is correct |
2 |
Correct |
382 ms |
87532 KB |
Output is correct |
3 |
Runtime error |
349 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |