//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];
int pr[N];
bool col[N], cl[N];
vector<int> sm, fp;
lli dp[N], pat;
void dfs0(int v, int par)
{
//cout << v << par << endl;
pr[v] = par;
col[v] = 1;
bool bl = 0;
for(auto& p : g[v])
{
if(p.fr == par && !bl) {bl = 1; continue;}
if(col[p.fr] && sm.empty())
{
int u = v;
while(u != p.fr) sm.ad(u), cl[u] = 1, u = pr[u];
cl[p.fr] = 1;
sm.ad(p.fr);
}
if(!col[p.fr]) dfs0(p.fr, v);
}
}
lli dfs1(int v, int par)
{
lli ret = 0;
lli mx = 0, mx1 = 0;
for(auto& p : g[v])
{
if(cl[p.fr] || p.fr == par) continue;
ret = max(ret, dfs1(p.fr, v));
dp[v] = max(dp[v], dp[p.fr] + p.sc);
mx1 = max(mx1, dp[p.fr] + p.sc);
if(mx1 > mx) swap(mx, mx1);
}
ret = max(ret, mx + mx1);
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])
{
//cout << i << endl;
dfs0(i, i);
ans = 0;
for(auto& p : sm) ans = max(ans, dfs1(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 |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23756 KB |
Output is correct |
3 |
Correct |
15 ms |
23816 KB |
Output is correct |
4 |
Correct |
16 ms |
23724 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
15 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23720 KB |
Output is correct |
10 |
Correct |
16 ms |
23732 KB |
Output is correct |
11 |
Correct |
16 ms |
23732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23820 KB |
Output is correct |
2 |
Correct |
15 ms |
23876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23884 KB |
Output is correct |
2 |
Correct |
17 ms |
24216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
24976 KB |
Output is correct |
2 |
Correct |
31 ms |
27596 KB |
Output is correct |
3 |
Correct |
24 ms |
25164 KB |
Output is correct |
4 |
Correct |
19 ms |
24460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
28976 KB |
Output is correct |
2 |
Correct |
53 ms |
30912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
36932 KB |
Output is correct |
2 |
Correct |
125 ms |
41324 KB |
Output is correct |
3 |
Correct |
138 ms |
54400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
45404 KB |
Output is correct |
2 |
Correct |
210 ms |
72180 KB |
Output is correct |
3 |
Correct |
235 ms |
74944 KB |
Output is correct |
4 |
Correct |
288 ms |
98956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
69256 KB |
Output is correct |
2 |
Correct |
909 ms |
124340 KB |
Output is correct |
3 |
Correct |
312 ms |
66884 KB |
Output is correct |
4 |
Correct |
418 ms |
106844 KB |
Output is correct |
5 |
Correct |
413 ms |
107688 KB |
Output is correct |
6 |
Correct |
1261 ms |
73480 KB |
Output is correct |
7 |
Runtime error |
440 ms |
131072 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
394 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |