#include template class graph { public: graph(const int size) : m_size{size}, m_eis(m_size) {} void add_edge(const int u, const int v, const T& c, const bool bi = false) { const int ei = (int)m_us.size(); m_eis[u].push_back(ei); if (bi) { m_eis[v].push_back(ei); } m_us.push_back(u), m_vs.push_back(v), m_cs.push_back(c); } void add_edge(const int u, const int v, const bool bi = false) { add_edge(u, v, 1, bi); } const std::vector& operator[](const int u) const { return m_eis[u]; } std::vector& operator[](const int u) { return m_eis[u]; } std::tuple edge(const int u, const int i) const { return std::make_tuple(u, m_us[i] ^ m_vs[i] ^ u, m_cs[i]); } int size() const { return m_size; } friend std::ostream& operator<<(std::ostream& os, const graph& g) { for (int u = 0; u < g.size(); u++) { for (const int ei : g[u]) { const auto& [from, to, cost] = g.edge(u, ei); os << "[" << ei << "]: " << from << "->" << to << "(" << cost << ")\n"; } } return os; } private: int m_size; std::vector> m_eis; std::vector m_us, m_vs; std::vector m_cs; }; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template using max_heap = std::priority_queue; template using min_heap = std::priority_queue, std::greater>; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template void bset(T& mask, const int ind) { mask |= ((T)1 << ind); } template void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); } template void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); } template void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN(n - 1) * T{10}; } template struct fix : F { fix(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; template auto nd_array(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], nd_array(szs, x)); } } class printer { public: printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); } template int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; } template int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; } private: template void dump(const T& v) { m_os << v; } template void dump(const std::vector& vs) { for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); } } template void dump(const std::vector>& vss) { for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); } } template int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; } std::ostream& m_os; }; printer out; class scanner { public: scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } void fastio(const bool on) { if (on) { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } else { m_is.tie(&std::cout), std::ios::sync_with_stdio(true); } } template T val() { T v; return m_is >> v, v; } template T val(const T offset) { return val() - offset; } template std::vector vec(const int n) { return make_v(n, [this]() { return val(); }); } template std::vector vec(const int n, const T offset) { return make_v(n, [this, offset]() { return val(offset); }); } template std::vector> vvec(const int n0, const int n1) { return make_v>(n0, [this, n1]() { return vec(n1); }); } template std::vector> vvec(const int n0, const int n1, const T offset) { return make_v>(n0, [this, n1, offset]() { return vec(n1, offset); }); } template auto tup() { return std::tuple...>{val()...}; } template auto tup(const Args&... offsets) { return std::tuple...>{val(offsets)...}; } private: template std::vector make_v(const int n, F f) { std::vector ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } std::istream& m_is; }; scanner in; int main() { const auto [N, Q, C] = in.tup(); graph g(N); for (int i = 0; i < N - 1; i++) { const auto [u, v, c] = in.tup(1, 1, 0); g.add_edge(u, v, c, true); } const auto xs = in.vec(Q, 1); std::vector dp(N, inf_v); dp[xs[0]] = 0; for (int i = 1; i < Q; i++) { std::vector ndp(N, inf_v); std::vector ds(N, 0); std::vector ps(N, -1); std::vector dp_depth_min(N, inf_v); fix([&](auto self, const int s, const int p) -> void { dp_depth_min[s] = dp[s] + ds[s]; for (const int ei : g[s]) { const auto [from, to, cost] = g.edge(s, ei); if (to == p) { continue; } ds[to] = ds[from] + cost, ps[to] = from; self(to, from); chmin(dp_depth_min[from], dp_depth_min[to]); } })(xs[i], -1); const ll pmin = *std::min_element(dp.begin(), dp.end()); for (int v = xs[i - 1]; v != -1; v = ps[v]) { ndp[v] = pmin + ds[xs[i - 1]]; } for (int j = 0; j < N; j++) { chmin(ndp[j], dp[j] + ds[xs[i - 1]]); } for (int j = 0; j < N; j++) { chmin(ndp[j], dp_depth_min[j] + C); } dp = ndp; } out.ln(*std::min_element(dp.begin(), dp.end())); }