#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n, vis[N], par[N], wpar[N], on_cycle[N];
vector<pair<int, int>> g[N];
long long pf[N], f[N][2];
void dfs(int u){
vis[u]=1;
long long mx1=-1e18, mx2=-1e18;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (on_cycle[v]) continue;
dfs(v);
long long val=f[v][0]+w;
f[u][0]=max(f[u][0], val);
f[u][1]=max(f[u][1], f[v][1]);
if (mx1<val) mx2=mx1, mx1=val;
else mx2=max(mx2, mx1);
}
f[u][1]=max({f[u][1], f[u][0], mx1+mx2});
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
vector<int> deg(n+1);
vector<pair<int, pair<int, int>>> edge;
for (int i=1; i<=n; ++i){
int j, k; cin >> j >> k;
edge.push_back({k, {i, j}});
// g[j].emplace_back(i, k);
// par[i]=j;
// wpar[i]=k;
}
sort(edge.begin(), edge.end());
long long ans=0;
for (auto &i:edge){
if (deg[i.second.first]!=2 && deg[i.second.second]!=2){
ans+=i.first;
++deg[i.second.first];
++deg[i.second.second];
}
}
cout << ans << '\n';
// vector<long long> ans;
// for (int i=1; i<=n; ++i) if (!vis[i]){
// long long cur=0;
// vector<int> cycle;
// int u=i;
// while (1){
// if (vis[u]) break;
// vis[u]=1;
// cycle.push_back(u);
// u=par[u];
// }
// cycle.erase(cycle.begin(), find(cycle.begin(), cycle.end(), u));
// for (int v:cycle) on_cycle[v]=1;
// for (int v:cycle){
// dfs(v);
// cur=max(cur, f[v][1]);
// }
// for (int j=1; j<=(int)cycle.size(); ++j){
// pf[j]=pf[j-1]+wpar[cycle[j-1]];
// }
// long long sum=pf[(int)cycle.size()];
// long long mx1=-1e18, mx2=-1e18;
// for (int j=0; j<(int)cycle.size(); ++j){
// cur=max(cur, f[cycle[j]][0]+pf[j]+mx1);
// cur=max(cur, f[cycle[j]][0]-pf[j]+mx2);
// mx1=max(mx1, f[cycle[j]][0]-pf[j]);
// mx2=max(mx2, f[cycle[j]][0]+sum+pf[j]);
// }
// ans.push_back(cur);
// }
// cout << accumulate(ans.begin(), ans.end(), 0ll) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
23900 KB |
Output isn't correct |
2 |
Incorrect |
9 ms |
23900 KB |
Output isn't correct |
3 |
Incorrect |
9 ms |
23880 KB |
Output isn't correct |
4 |
Correct |
9 ms |
24148 KB |
Output is correct |
5 |
Incorrect |
10 ms |
23900 KB |
Output isn't correct |
6 |
Incorrect |
10 ms |
23900 KB |
Output isn't correct |
7 |
Incorrect |
10 ms |
23736 KB |
Output isn't correct |
8 |
Incorrect |
16 ms |
23900 KB |
Output isn't correct |
9 |
Incorrect |
9 ms |
23900 KB |
Output isn't correct |
10 |
Incorrect |
9 ms |
23900 KB |
Output isn't correct |
11 |
Incorrect |
12 ms |
23868 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
23900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
24412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
25428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
29636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
87 ms |
35516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
228 ms |
55080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
212 ms |
55020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |