#include <bits/stdc++.h>
using namespace std;
const int N = 1000002;
int a[N], l[N], cyc[N];
long long d[N], dp[N], mx[N];
vector<int> g[N];
long long Solve(int v, int pv) {
long long mx = 0;
for (int u : g[v]) {
if (d[u] != -2 || u == pv) {
continue;
}
mx = max(mx, Solve(u, v));
mx = max(mx, dp[v] + dp[u] + l[u]);
dp[v] = max(dp[v], dp[u] + l[u]);
}
vector<int>().swap(g[v]);
return mx;
}
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;
}
long long c, t;
int low, high, pos, mid;
for (int i = 0; i < n; i++) {
if (d[i] != -1) {
continue;
}
int sz = 0;
cyc[sz++] = i;
d[i] = 0;
for (int b = 0; b < sz; b++) {
int x = cyc[b];
if (d[a[x]] == -1) {
d[a[x]] = 0;
cyc[sz++] = a[x];
}
for (int y : g[x]) {
if (d[y] == -1) {
d[y] = 0;
cyc[sz++] = y;
}
}
}
for (int x = 0; x < sz; x++) {
d[cyc[x]] = -1;
}
int x = i;
while (d[x] == -1) {
d[x] = 0;
x = a[x];
}
for (int x = 0; x < sz; x++) {
d[cyc[x]] = -2;
}
sz = 0;
cyc[sz++] = x;
int y = a[x];
while (y != x) {
cyc[sz++] = y;
y = a[y];
}
d[cyc[0]] = 0;
for (int i = 1; i < sz; i++) {
int x = cyc[i - 1], y = cyc[i];
d[y] = d[x] + l[x];
}
int nsz = sz;
for (int b = 0; b < nsz; b++) {
int x = cyc[b];
for (int y : g[x]) {
if (d[y] == -2) {
d[y] = -3;
cyc[nsz++] = y;
}
}
}
c = 0;
for (int b = nsz - 1; b >= 0; b--) {
int x = cyc[b];
for (int y : g[x]) {
if (d[y] == -3 && a[y] == x) {
c = max(c, dp[x] + dp[y] + l[y]);
dp[x] = max(dp[x], dp[y] + l[y]);
}
}
}
for (int i = 0; i < sz; i++) {
// c = max(c, Solve(cyc[i], cyc[i]));
c = max(c, dp[cyc[i]]);
}
for (int i = sz - 1; i >= 0; i--) {
if (i + 1 < sz) {
mx[i] = mx[i + 1];
} else {
mx[i] = d[cyc[i]] + dp[cyc[i]];
}
mx[i] = max(mx[i], d[cyc[i]] + dp[cyc[i]]);
}
t = d[cyc[sz - 1]] + l[cyc[sz - 1]];
for (int i = 0; i < sz; i++) {
low = i + 1, high = sz - 1, pos = i;
while (low <= high) {
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]);
}
}
for (int i = sz - 1; i >= 0; i--) {
if (i + 1 < sz) {
mx[i] = mx[i + 1];
} else {
mx[i] = -d[cyc[i]] + dp[cyc[i]];
}
mx[i] = max(mx[i], -d[cyc[i]] + dp[cyc[i]]);
}
for (int i = 0; i < sz; i++) {
low = i + 1, high = sz - 1, pos = i;
while (low <= high) {
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]);
}
}
res += c;
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
23896 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23900 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
8 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23896 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
23900 KB |
Output is correct |
2 |
Correct |
8 ms |
23968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24668 KB |
Output is correct |
2 |
Correct |
15 ms |
25800 KB |
Output is correct |
3 |
Correct |
13 ms |
25192 KB |
Output is correct |
4 |
Correct |
15 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
26712 KB |
Output is correct |
2 |
Correct |
27 ms |
29524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
35152 KB |
Output is correct |
2 |
Correct |
53 ms |
38236 KB |
Output is correct |
3 |
Correct |
63 ms |
39900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
46160 KB |
Output is correct |
2 |
Correct |
93 ms |
52052 KB |
Output is correct |
3 |
Correct |
125 ms |
60500 KB |
Output is correct |
4 |
Correct |
137 ms |
64776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
59728 KB |
Output is correct |
2 |
Correct |
279 ms |
79372 KB |
Output is correct |
3 |
Correct |
150 ms |
65368 KB |
Output is correct |
4 |
Correct |
189 ms |
79808 KB |
Output is correct |
5 |
Correct |
181 ms |
77660 KB |
Output is correct |
6 |
Correct |
342 ms |
79952 KB |
Output is correct |
7 |
Correct |
196 ms |
86100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
98004 KB |
Output is correct |
2 |
Correct |
165 ms |
97872 KB |
Output is correct |
3 |
Correct |
221 ms |
97872 KB |
Output is correct |
4 |
Correct |
152 ms |
85840 KB |
Output is correct |
5 |
Correct |
179 ms |
81744 KB |
Output is correct |
6 |
Correct |
174 ms |
84108 KB |
Output is correct |
7 |
Correct |
359 ms |
83788 KB |
Output is correct |