#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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |