#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];
long long Solve(int v, int pv) {
long long mx = 0;
while (!g[v].empty()) {
int u = g[v].back();
g[v].pop_back();
if (d[u] != -2 || u == pv) {
continue;
}
Solve(u, v);
mx = max(mx, dp[v] + dp[u] + l[u]);
dp[v] = max(dp[v], dp[u] + l[u]);
}
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;
}
for (int i = 0; i < n; i++) {
if (d[i] != -1) {
continue;
}
vector<int> cyc(1, i);
d[i] = 0;
for (int b = 0; b < (int) cyc.size(); b++) {
int x = cyc[b];
if (d[a[x]] == -1) {
d[a[x]] = 0;
cyc.push_back(a[x]);
}
for (int y : g[x]) {
if (d[y] == -1) {
d[y] = 0;
cyc.push_back(y);
}
}
}
for (int x : cyc) {
d[x] = -1;
}
int x = i;
while (d[x] == -1) {
d[x] = 0;
x = a[x];
}
for (int x : cyc) {
d[x] = -2;
}
int y = a[x];
vector<int>(1, x).swap(cyc);
while (y != x) {
cyc.push_back(y);
y = a[y];
}
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];
}
long long c = 0;
for (int i : cyc) {
c = max(c, Solve(i, i));
c = max(c, dp[i]);
}
int sz = (int) cyc.size();
vector<long long> mx(sz);
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][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]);
}
}
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++) {
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);
res += c;
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
7 ms |
23900 KB |
Output isn't correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
7 ms |
23956 KB |
Output is correct |
5 |
Correct |
8 ms |
23900 KB |
Output is correct |
6 |
Correct |
7 ms |
23900 KB |
Output is correct |
7 |
Correct |
8 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
24024 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 |
9 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
8 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24524 KB |
Output is correct |
2 |
Correct |
15 ms |
25436 KB |
Output is correct |
3 |
Correct |
12 ms |
24924 KB |
Output is correct |
4 |
Incorrect |
11 ms |
24408 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
26136 KB |
Output is correct |
2 |
Correct |
23 ms |
28340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
32500 KB |
Output is correct |
2 |
Correct |
52 ms |
36360 KB |
Output is correct |
3 |
Correct |
58 ms |
37260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
41292 KB |
Output is correct |
2 |
Correct |
93 ms |
46796 KB |
Output is correct |
3 |
Correct |
125 ms |
56276 KB |
Output is correct |
4 |
Correct |
125 ms |
57640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
49580 KB |
Output is correct |
2 |
Correct |
304 ms |
67288 KB |
Output is correct |
3 |
Correct |
132 ms |
56960 KB |
Output is correct |
4 |
Correct |
170 ms |
69348 KB |
Output is correct |
5 |
Correct |
170 ms |
68888 KB |
Output is correct |
6 |
Incorrect |
404 ms |
67424 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
163 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |