#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 2e5;
std::vector<std::pair<int, int>> e[N];
int n, upedge[N];
i64 dp[N];
void dfs(int u, int p) {
dp[u] = 0;
for (auto [v, w] : e[u]) {
if (v == p) {
upedge[u] = w;
}
}
for (auto [v, w] : e[u]) {
if (v != p) {
dfs(v, u);
dp[u] += dp[v] + upedge[v];
}
}
}
i64 ans[N];
void dfs2(int u, int p, int car = 0) {
ans[u] = dp[0] + car;
for (auto [v, w] : e[u]) {
if (v != p) {
dfs2(v, u, car + w - upedge[v]);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
i64 sum = 0;
for (int i = 0; i < n - 1; i++) {
int u, v, c, d;
std::cin >> u >> v >> c >> d;
u--;
v--;
e[u].emplace_back(v, c);
e[v].emplace_back(u, d);
sum += c + d;
}
dfs(0, 0);
dfs2(0, 0);
i64 res = *std::max_element(ans, ans + n);
std::cout << sum - res << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Incorrect |
267 ms |
16160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Incorrect |
267 ms |
16160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |