//Arayi
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
const int N = 1e6 + 30;
int n;
vector <pair<int, int> > g[N];
bool col[N], cl[N];
vector<int> sm, fp;
lli dp[N], dp1[N], pat;
void dfs0(int v, int par)
{
col[v] = 1;
if(sm.empty()) fp.ad(v);
bool bl = 0;
for(auto& p : g[v])
{
if(p.fr == par && !bl) {bl = 1; continue;}
if(col[p.fr] && sm.empty())
{
while(fp.back() != p.fr) sm.ad(fp.back()), cl[fp.back()] = 1, fp.pop_back();
cl[p.fr] = 1;
sm.ad(p.fr);
}
if(!col[p.fr]) dfs0(p.fr, v);
}
if(sm.empty()) fp.pop_back();
}
void dfs1(int v, int par)
{
for(auto& p : g[v])
{
if(cl[p.fr] || p.fr == par) continue;
dfs1(p.fr, v);
dp[v] = max(dp[v], dp[p.fr] + p.sc);
}
}
lli dfs(int v, int par)
{
lli ret = dp[v] + dp1[v];
lli mx = 0, mx1 = 0;
for(auto& p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
dp1[p.fr] = dp1[v] + p.sc;
mx1 = max(mx1, dp[p.fr] + p.sc);
if(mx1 > mx) swap(mx, mx1);
}
if(mx1)
{
for(auto& p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
if(dp[p.fr] + p.sc == mx) dp1[p.fr] = max(dp1[p.fr], mx1 + dp[p.fr] + p.sc);
else dp1[p.fr] = max(dp1[p.fr], mx + dp[p.fr] + p.sc);
}
}
for(auto& p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
ret = max(ret, dfs(p.fr, v));
}
return ret;
}
int main()
{
fastio;
//freopen("input.in", "r", stdin);
cin >> n;
for (int i = 1; i <= n; i++)
{
int a, b;
cin >> a >> b;
g[i].ad(MP(a, b));
g[a].ad(MP(i, b));
}
lli mx, sum, nx, ans;
for (int i = 1; i <= n; i++)
{
if(!col[i])
{
dfs0(i, i);
ans = 0;
for(auto& p : sm)
{
dfs1(p, p);
ans = max(ans, dfs(p, p));
}
fp.clear();
reverse(all(g[sm.back()]));
sm.ad(sm[0]);
for (int i = 0; i < (int)sm.size() - 1; i++)
for(auto& p : g[sm[i]]) if(p.fr == sm[i + 1]) {fp.ad(p.sc); break;}
sm.pop_back();
mx = 0, nx = 0;
for (int i = 0; i < (int)sm.size(); i++)
{
ans = max(ans, dp[sm[i]] + nx + mx);
mx = max(mx, dp[sm[i]] - nx);
nx += fp[i];
}
sum = nx; nx = mx = 0;
for (int i = 0; i < (int)sm.size(); i++)
{
if(i) ans = max(ans, dp[sm[i]] + sum - nx + mx);
mx = max(mx, dp[sm[i]] + nx);
nx += fp[i];
}
pat += ans;
fp.clear(), sm.clear();
}
}
cout << pat << endl;
//cout << "ended" << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
13 ms |
23804 KB |
Output isn't correct |
3 |
Correct |
14 ms |
23812 KB |
Output is correct |
4 |
Correct |
14 ms |
23804 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23900 KB |
Output is correct |
7 |
Correct |
14 ms |
23744 KB |
Output is correct |
8 |
Correct |
14 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23816 KB |
Output is correct |
11 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23856 KB |
Output is correct |
2 |
Correct |
17 ms |
23784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23884 KB |
Output is correct |
2 |
Correct |
20 ms |
24268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
24996 KB |
Output is correct |
2 |
Correct |
34 ms |
27508 KB |
Output is correct |
3 |
Incorrect |
27 ms |
25260 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
29076 KB |
Output is correct |
2 |
Correct |
53 ms |
31492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
37280 KB |
Output is correct |
2 |
Correct |
106 ms |
40640 KB |
Output is correct |
3 |
Correct |
161 ms |
51776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
45912 KB |
Output is correct |
2 |
Correct |
221 ms |
69944 KB |
Output is correct |
3 |
Correct |
242 ms |
69044 KB |
Output is correct |
4 |
Correct |
321 ms |
93572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
72476 KB |
Output is correct |
2 |
Correct |
1030 ms |
124236 KB |
Output is correct |
3 |
Correct |
315 ms |
68476 KB |
Output is correct |
4 |
Correct |
429 ms |
105344 KB |
Output is correct |
5 |
Correct |
442 ms |
106064 KB |
Output is correct |
6 |
Incorrect |
1619 ms |
76812 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
421 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |