#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int a[N], l[N];
long long d[N], dp[N];
vector<int> g[N];
bool was[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> l[i];
--a[i];
}
for (int i = 0; i < n; i++) {
g[a[i]].push_back(i);
}
long long res = 0;
for (int i = 0; i < n; i++) {
d[i] = -1;
}
for (int i = 0; i < n; i++) {
if (was[i]) {
continue;
}
vector<int> que(1, i);
for (int b = 0; b < (int) que.size(); b++) {
int x = que[b];
if (!was[a[x]]) {
was[a[x]] = true;
que.push_back(a[x]);
}
for (int y : g[x]) {
if (!was[y]) {
was[y] = true;
que.push_back(y);
}
}
}
for (int x : que) {
was[x] = false;
}
int x = i;
while (!was[x]) {
was[x] = true;
x = a[x];
}
int y = a[x];
vector<int> cyc(1, x);
while (y != x) {
cyc.push_back(y);
y = a[y];
}
for (int x : que) {
was[x] = true;
}
d[cyc[0]] = 0;
for (int i = 1; i < (int) cyc.size(); i++) {
int x = cyc[i - 1], y = cyc[i];
d[y] = d[x] + l[x];
}
vector<int>().swap(que);
long long c = 0;
function<void(int, int)> Solve = [&](int v, int pv) {
for (int u : g[v]) {
if (d[u] != -1 || u == pv) {
continue;
}
Solve(u, v);
c = max(c, dp[v] + dp[u] + l[u]);
dp[v] = max(dp[v], dp[u] + l[u]);
}
};
for (int i : cyc) {
Solve(i, i);
c = max(c, dp[i]);
}
int sz = (int) cyc.size();
vector<long long> mx(sz, -(long long) 1e18);
for (int i = sz - 1; i >= 0; i--) {
if (i + 1 < sz) {
mx[i] = mx[i + 1];
}
// mx[i][0] = max(mx[i][0], d[cyc[i]] + dp[cyc[i]]);
// mx[i][1] = max(mx[i][1], -d[cyc[i]] + dp[cyc[i]]);
mx[i] = max(mx[i], d[cyc[i]] + dp[cyc[i]]);
}
long long t = d[cyc.back()] + l[cyc.back()];
for (int i = 0; i < sz; i++) {
int low = i + 1, high = sz - 1, pos = i;
while (low <= high) {
int mid = (low + high) >> 1;
if (t - (d[cyc[mid]] - d[cyc[i]]) >= (d[cyc[mid]] - d[cyc[i]])) {
pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if (pos + 1 < sz) {
c = max(c, -d[cyc[i]] + dp[cyc[i]] + mx[pos + 1]);
}
if (pos > i) {
// c = max(c, t + d[cyc[i]] + dp[cyc[i]] + mx[i + 1][1]);
}
}
for (int i = sz - 1; i >= 0; i--) {
if (i + 1 < sz) {
mx[i] = mx[i + 1];
} else {
mx[i] = -(long long) 1e18;
}
mx[i] = max(mx[i], -d[cyc[i]] + dp[cyc[i]]);
}
for (int i = 0; i < sz; i++) {
int low = i + 1, high = sz - 1, pos = i;
while (low <= high) {
int mid = (low + high) >> 1;
if (t - (d[cyc[mid]] - d[cyc[i]]) >= (d[cyc[mid]] - d[cyc[i]])) {
pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if (pos > i) {
c = max(c, t + d[cyc[i]] + dp[cyc[i]] + mx[i + 1]);
}
}
vector<long long>().swap(mx);
vector<int>().swap(cyc);
res += c;
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24664 KB |
Output is correct |
2 |
Correct |
6 ms |
24668 KB |
Output is correct |
3 |
Correct |
6 ms |
24668 KB |
Output is correct |
4 |
Correct |
6 ms |
24668 KB |
Output is correct |
5 |
Correct |
6 ms |
24668 KB |
Output is correct |
6 |
Correct |
6 ms |
24668 KB |
Output is correct |
7 |
Correct |
6 ms |
24664 KB |
Output is correct |
8 |
Correct |
6 ms |
24668 KB |
Output is correct |
9 |
Correct |
7 ms |
24568 KB |
Output is correct |
10 |
Correct |
6 ms |
24668 KB |
Output is correct |
11 |
Correct |
7 ms |
24736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
6 ms |
24836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
7 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
25436 KB |
Output is correct |
2 |
Correct |
13 ms |
26272 KB |
Output is correct |
3 |
Correct |
10 ms |
25688 KB |
Output is correct |
4 |
Correct |
8 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26904 KB |
Output is correct |
2 |
Correct |
23 ms |
29204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
33336 KB |
Output is correct |
2 |
Correct |
52 ms |
37244 KB |
Output is correct |
3 |
Correct |
52 ms |
37232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
42348 KB |
Output is correct |
2 |
Correct |
86 ms |
47656 KB |
Output is correct |
3 |
Correct |
128 ms |
57376 KB |
Output is correct |
4 |
Correct |
141 ms |
58632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
50632 KB |
Output is correct |
2 |
Correct |
304 ms |
68320 KB |
Output is correct |
3 |
Correct |
133 ms |
58064 KB |
Output is correct |
4 |
Correct |
187 ms |
70364 KB |
Output is correct |
5 |
Correct |
179 ms |
69604 KB |
Output is correct |
6 |
Correct |
418 ms |
68452 KB |
Output is correct |
7 |
Correct |
191 ms |
73972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
167 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |