#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MxN = 1000010;
pair<int, ll> nxt[MxN];
bitset<MxN> cycle;
int in[MxN];
queue<int> q;
ll dist[MxN], dp[MxN];
signed main(int argc, char *argv[]) {
cin.tie(nullptr)->ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i=1; i<=n; ++i) {
cin >> nxt[i].first >> nxt[i].second;
in[nxt[i].first]++;
}
for(int i=1; i<=n; ++i) {
if(in[i] != 0) {
continue;
}
q.emplace(i);
}
while(!q.empty()) {
int u = q.front(); q.pop();
int v = nxt[u].first;
ll w = nxt[u].second;
dp[v] = max({dp[u], dp[v], dist[v] + dist[u] + w});
dist[v] = max(dist[v], dist[u] + w);
if (--in[v] == 0) {
q.emplace(v);
}
}
#ifdef ICY
for(int i=1; i<=n; ++i) {
cerr << "D: " << dp[i] << " " << dist[i] << "\n";
}
#endif
auto calc = [&](int pos) {
int cur_pos = pos;
cur_pos = nxt[pos].first;
ll cur_dist = nxt[cur_pos].second;
ll with = dist[cur_pos];
ll without = dist[cur_pos];
ll max_dp_with = dp[cur_pos];
ll max_dp_without = -1e18 - 100ll;
#ifdef ICY
cerr << "PRE: " << max_dp_with << " " << max_dp_without << " " << cur_dist << "\n";
#endif
while(cur_pos != pos) {
in[cur_pos] = 0;
max_dp_without = max({max_dp_without, dp[cur_pos], dist[cur_pos] + cur_dist + without});
max_dp_with = max(max_dp_with, dist[cur_pos] - cur_dist + with);
with = max(with, dist[cur_pos] + cur_dist);
without = max(without, dist[cur_pos] - cur_dist);
cur_dist = cur_pos + nxt[cur_pos].second;
cur_pos = nxt[cur_pos].first;
#ifdef ICY
cerr << "A: " << max_dp_with << " " << max_dp_without << "\n";
#endif
}
return max(max_dp_without, max_dp_with + cur_dist);
};
ll answer = 0ll;
for(int i=1; i<=n; ++i) {
if(in[i] == 0) {
continue;
}
#ifdef ICY
cerr << "X: " << i << "\n";
#endif
answer += calc(i);
}
cout << answer << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
10336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
149 ms |
19024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
138 ms |
35668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
147 ms |
50960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |