#include<bits/stdc++.h>
#define next uwuwuwuwu
using namespace std;
using ll = long long;
const int maxn = 1e6 + 6;
const ll inf = (1ll<<50);
int n;
int up[maxn];
ll dp[maxn], dep[maxn];
int tmp[maxn], tmp2[maxn], sz = 0, offset = 0, tsz = 0;
bitset<maxn> vis;
int link[maxn], next[maxn];
__attribute__(( always_inline)) inline void add_edge(int v, int ch) {
next[ch] = link[v];
link[v] = ch;
}
ll ans;
void dfs(int v) {
#define u link[v]
for(dp[v] = 0; u; u = next[u]) {
dep[u] = dep[v] + up[u];
dfs(u);
ans = max(ans, dep[v] + dp[v] + dp[u] + up[u]);
dp[v] = max(dp[v], dp[u] + up[u]);
}
#undef u
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> up[i] >> dp[i];
}
for(int i = 1; i <= n; i++) if(!vis[i]) {
sz = offset;
int v = i;
while(!vis[v]) {
tmp[sz] = v;
tmp2[sz++] = dp[v];
vis[v] = 1;
v = up[v];
}
reverse(tmp+offset, tmp+sz);
reverse(tmp2+offset, tmp2+sz);
while(sz>offset && tmp[sz-1] != v) sz--;
for(int i = offset+1; i < sz; i++)
swap(tmp2[i], tmp2[i-1]);
if(sz<=offset) continue;//no new cycle
tmp2[sz-1] *= -1;
offset = sz;
}
vis = 0;
for(int i = 0; i < sz; i++)
vis[tmp[i]] = 1;
for(int i = 1; i <= n; i++) if(!vis[i]) {
add_edge(up[i], i);
}
for(int i = 0; i < n; i++)
up[i] = dp[i];
ll totalAns = 0, total = 0, pref = 0, A = -inf, B = -inf;
for(int start = 0, end = 0; start < sz; start = ++end) {
total = 0, pref = 0, A = -inf, B = -inf, ans = -inf;
while(tmp2[end] >= 0) {
total += tmp2[end];
end++;
}
for(int i = start; i <= end; i++) {
dfs(tmp[i]);
ans = max(ans, A + pref + dp[tmp[i]]);
ans = max(ans, B + dp[tmp[i]] + (total-pref) - tmp2[end]);
A = max(A, dp[tmp[i]]-pref);
B = max(B, dp[tmp[i]]+pref);
pref += tmp2[i];
}
totalAns += ans;
}
cout << totalAns << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
748 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1132 KB |
Output is correct |
2 |
Correct |
10 ms |
1644 KB |
Output is correct |
3 |
Correct |
7 ms |
1260 KB |
Output is correct |
4 |
Incorrect |
4 ms |
876 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2156 KB |
Output is correct |
2 |
Correct |
23 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
6252 KB |
Output is correct |
2 |
Correct |
46 ms |
5868 KB |
Output is correct |
3 |
Correct |
65 ms |
8684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
11752 KB |
Output is correct |
2 |
Correct |
115 ms |
15980 KB |
Output is correct |
3 |
Correct |
116 ms |
13136 KB |
Output is correct |
4 |
Correct |
154 ms |
21100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
232 ms |
24208 KB |
Output is correct |
2 |
Correct |
290 ms |
24112 KB |
Output is correct |
3 |
Correct |
180 ms |
24088 KB |
Output is correct |
4 |
Correct |
229 ms |
31132 KB |
Output is correct |
5 |
Correct |
224 ms |
31084 KB |
Output is correct |
6 |
Incorrect |
421 ms |
27244 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
98284 KB |
Output is correct |
2 |
Incorrect |
305 ms |
59628 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |