#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vpll vector<pll>
#define msi multiset<int>
#define si set<int>
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7;
const int MOD = 998244353;
const int inf = mod;
const ll INF = 1e18;
const int logo = 20;
const int MAXN = 1e6 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
bool bio[MAXN];
ll to[MAXN], w[MAXN];
vi g[MAXN];
ll ans, ret;
vll dp, s;
int ciklus(int u){
if(bio[u]) return u;
bio[u] = 1;
int v = ciklus(to[u]);
if(v){
dp.PB(u);
s.PB(w[u]);
if(u == v) return 0;
return u;
}
bio[u] = 0;
return 0;
}
ll dfs(int u){
ll mx = 0;
bio[u] = 1;
for(auto &x : g[u]){
if(bio[x]) continue;
ll ch = (ll)w[x] + dfs(x);
ret = max(ret, ch + mx);
mx = max(mx, ch);
}
return mx;
}
void resi(int u){
dp.clear(), s.clear();
ciklus(u);
int sz = s.size();
reverse(all(dp));
reverse(all(s));
for(auto &x : dp) x = dfs(x);
for(int i = sz - 1; i; i--) swap(s[i], s[i - 1]);
FOR(i, 1, sz) s[i] += s[i - 1];
ll m1 = -INF, m2 = -INF;
for(int i=0; i<sz; i++){
ret = max(ret, dp[i] + s[i] + m1);
ret = max(ret, s[sz - 1] + m2 + dp[i] - s[i]);
m1 = max(m1, dp[i] - s[i]);
m2 = max(m2, dp[i] + s[i]);
}
}
void solve(){
int n;
cin >> n;
FOR(i, 1, n + 1) cin >> to[i] >> w[i], g[to[i]].PB(i);
FOR(i, 1, n + 1) if(!bio[i]) ret = 0, resi(i), ans += ret;
cout << ans << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin >> t;
while(t--)solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
14 ms |
23764 KB |
Output isn't correct |
3 |
Correct |
13 ms |
23892 KB |
Output is correct |
4 |
Incorrect |
13 ms |
23812 KB |
Output isn't correct |
5 |
Correct |
17 ms |
23772 KB |
Output is correct |
6 |
Incorrect |
14 ms |
23812 KB |
Output isn't correct |
7 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
8 |
Incorrect |
13 ms |
23824 KB |
Output isn't correct |
9 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
10 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
11 |
Correct |
13 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
23932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
24660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
27852 KB |
Output is correct |
2 |
Correct |
37 ms |
30364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
34824 KB |
Output is correct |
2 |
Correct |
75 ms |
42640 KB |
Output is correct |
3 |
Incorrect |
109 ms |
48924 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
45600 KB |
Output is correct |
2 |
Correct |
147 ms |
61664 KB |
Output is correct |
3 |
Correct |
159 ms |
78152 KB |
Output is correct |
4 |
Correct |
238 ms |
86764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
59924 KB |
Output is correct |
2 |
Correct |
522 ms |
111280 KB |
Output is correct |
3 |
Correct |
201 ms |
63820 KB |
Output is correct |
4 |
Correct |
259 ms |
95816 KB |
Output is correct |
5 |
Incorrect |
273 ms |
93028 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
262 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |