# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100232 | TadijaSebez | Beads and wires (APIO14_beads) | C++11 | 7 ms | 4992 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const int N=200050;
const ll inf=1e18;
ll dp[N][2];
vector<pair<int,int>> E[N];
void Solve(int u, int p, int w)
{
ll sum=0,fir=-inf,sec=-inf;
for(auto e:E[u]) if(e.first!=p)
{
Solve(e.first,u,e.second);
sum+=max(dp[e.first][0],dp[e.first][1]);
ll dif=dp[e.first][0]-max(dp[e.first][0],dp[e.first][1])+e.second;
if(fir<=dif) sec=fir,fir=dif;
else sec=max(sec,dif);
}
dp[u][1]=sum+fir+w;
dp[u][0]=max(sum,sum+fir+sec);
}
int main()
{
int n,i,u,v,w;
scanf("%i",&n);
for(i=1;i<n;i++) scanf("%i %i %i",&u,&v,&w),E[u].pb(mp(v,w)),E[v].pb(mp(u,w));
Solve(1,0,0);
printf("%lld\n",dp[1][0]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |