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;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
using ll = long long;
using ii = pair<int, int>;
const ll inf = 1e18;
int n;
vector<vector<ii>> adj;
vector<vector<ll>> dp;
void dfs(int x, int p = -1, ll pw = -inf){
ll best1 = -inf, best2 = -inf;
ll sum = 0;
for (auto [k, w]: adj[x]){
if (k == p) continue;
dfs(k, x, w);
sum += dp[k][0];
ll delta = dp[k][1] - dp[k][0];
if (delta > best1){
best2 = best1;
best1 = delta;
}
else if (delta > best2){
best2 = delta;
}
}
sum = max(sum, 0LL);
dp[x][0] = max({sum, sum + best1 + best2, sum + best1 + pw});
dp[x][1] = max({sum + pw, sum + best1 + best2 + pw});
}
void main_program(){
cin >> n;
adj.resize(n);
for (int i = 0; i < n-1; i++){
int x, y, w; cin >> x >> y >> w;
x--; y--;
adj[x].emplace_back(y, w);
adj[y].emplace_back(x, w);
}
dp.assign(n, {0, 0});
dfs(0);
cout << dp[0][0] << "\n";
}
# | 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... |