#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector w(n + 1, std::vector<long long>(n + 1));
long long tot = 0;
for (int i = 0, a, b, c, d; i < n - 1; ++i) {
std::cin >> a >> b >> c >> d;
w[a][b] = c;
w[b][a] = d;
tot += c + d;
}
std::vector<std::set<std::pair<int, int>>> edges(n + 1);
for (int i = 1; i <= n; ++i) {
auto dfs = [&](auto &&self, int u, int p) -> void {
for (int c = 1; c <= n; ++c) {
if (c == p or w[u][c] == 0) {
continue;
}
edges[i].insert({c, u});
self(self, c, u);
}
};
dfs(dfs, i, 0);
}
std::vector<long long> ans(n + 1);
for (int mask = 0; mask < (1 << n); ++mask) {
std::set<std::pair<int, int>> st;
for (int i = 0; i < n; ++i) {
if (mask & (1 << i)) {
for (auto &e : edges[i + 1]) {
st.insert(e);
}
}
}
long long best = 0;
for (auto &i : st) {
best += w[i.first][i.second];
}
ans[__builtin_popcount(mask)] =
std::max(ans[__builtin_popcount(mask)], best);
}
int q;
std::cin >> q;
while (q--) {
int x;
std::cin >> x;
std::cout << tot - ans[x] << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |