#include using namespace std; using int128 = __int128_t; using int64 = long long; using int32 = int; using uint128 = __uint128_t; using uint64 = unsigned long long; using uint32 = unsigned int; #define ALL(obj) (obj).begin(),(obj).end() template using priority_queue_reverse = priority_queue,greater>; constexpr int64 MOD = 1'000'000'000LL + 7; //' constexpr int64 MOD2 = 998244353; constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL; constexpr int64 LOWINF = 1'000'000'000'000'000LL; //' constexpr long double PI = 3.1415926535897932384626433L; template vector multivector(size_t N,T init){return vector(N,init);} template auto multivector(size_t N,T... t){return vector(N,multivector(t...));} template void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}} template ostream &operator<<(ostream &o, const map&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template ostream &operator<<(ostream &o, const set&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const multiset&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const vector&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const deque&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const pair&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} void print(void) {cout << endl;} template void print(Head&& head) {cout << head;print();} template void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward(tail)...);} template void chmax(T& a, const T b){a=max(a,b);} template void chmin(T& a, const T b){a=min(a,b);} vector split(const string &str, const char delemiter) {vector res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;} inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;} inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b) void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;} void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;} void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;} /* * @title Graph * @docs md/graph/Graph.md */ template class Graph{ private: const size_t N,H,W; public: vector>> edges; Graph(const size_t N):H(-1),W(-1),N(N), edges(N) {} Graph(const size_t H, const size_t W):H(H),W(W),N(H*W), edges(H*W) {} inline void make_edge(size_t from, size_t to, T w) { edges[from].emplace_back(to,w); } //{from_y,from_x} -> {to_y,to_x} inline void make_edge(pair from, pair to, T w) { make_edge(from.first*W+from.second,to.first*W+to.second,w); } inline void make_bidirectional_edge(size_t from, size_t to, T w) { make_edge(from,to,w); make_edge(to,from,w); } inline void make_bidirectional_edge(pair from, pair to, T w) { make_edge(from.first*W+from.second,to.first*W+to.second,w); make_edge(to.first*W+to.second,from.first*W+from.second,w); } inline size_t size(){return N;} inline size_t idx(pair yx){return yx.first*W+yx.second;} }; /* * @title Tree - 木 * @docs md/graph/Tree.md */ template class TreeBuilder; template class Tree { private: using TypeEdge = typename Operator::TypeEdge; size_t num; size_t ord; Graph& g; friend TreeBuilder; Tree(Graph& graph): g(graph), num(graph.size()), depth(graph.size(),-1), order(graph.size()), edge_dist(graph.size()){ } //for make_depth void dfs(int curr, int prev){ for(const auto& e:g.edges[curr]){ const int& next = e.first; if(next==prev) continue; depth[next] = depth[curr] + 1; edge_dist[next] = Operator::func_edge_merge(edge_dist[curr],e.second); dfs(next,curr); order[ord++] = next; } } //for make_eulertour void dfs(int from){ eulertour.push_back(from); for(auto& e:child[from]){ int to = e.first; dfs(to); eulertour.push_back(from); } } void make_root(const int root) { depth[root] = 0; edge_dist[root] = Operator::unit_edge; ord = 0; dfs(root,-1); order[ord++] = root; reverse_copy(order.begin(),order.end(),back_inserter(reorder)); } void make_root() { ord = 0; for(int i=0;i depth[e.first]) parent[i] = e; } void make_ancestor() { ancestor.resize(num); for (size_t i = 0; i < num; ++i) ancestor[i][0] = (parent[i].first!=num?parent[i]:make_pair(i,Operator::unit_lca_edge)); for (size_t j = 1; j < Operator::bit; ++j) { for (size_t i = 0; i < num; ++i) { size_t k = ancestor[i][j - 1].first; ancestor[i][j] = Operator::func_lca_edge_merge(ancestor[k][j - 1],ancestor[i][j - 1]); } } } pair lca_impl(size_t l, size_t r) { if (depth[l] < depth[r]) swap(l, r); int diff = depth[l] - depth[r]; auto ancl = make_pair(l,Operator::unit_lca_edge); auto ancr = make_pair(r,Operator::unit_lca_edge); for (int j = 0; j < Operator::bit; ++j) { if (diff & (1 << j)) ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][j],ancl); } if(ancl.first==ancr.first) return ancl; for (int j = Operator::bit - 1; 0 <= j; --j) { if(ancestor[ancl.first][j].first!=ancestor[ancr.first][j].first) { ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][j],ancl); ancr = Operator::func_lca_edge_merge(ancestor[ancr.first][j],ancr); } } ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][0],ancl); ancr = Operator::func_lca_edge_merge(ancestor[ancr.first][0],ancr); return Operator::func_lca_edge_merge(ancl,ancr); } pair> diameter_impl() { Tree tree = Tree::builder(g).build(); size_t root = 0; { tree.make_root(0); } root = max_element(tree.edge_dist.begin(),tree.edge_dist.end()) - tree.edge_dist.begin(); { tree.make_root(root); } size_t leaf = max_element(tree.edge_dist.begin(),tree.edge_dist.end()) - tree.edge_dist.begin(); TypeEdge sz = tree.edge_dist[leaf]; vector st; { tree.make_parent(); while(leaf != root) { st.push_back(leaf); leaf = tree.parent[leaf].first; } st.push_back(root); } return make_pair(sz,st); } template vector rerooting_impl(vector rerootdp,vector rerootparent) { for(size_t pa:order) for(auto& e:child[pa]) rerootdp[pa] = Operator::func_reroot_dp(rerootdp[pa],rerootdp[e.first]); for(size_t pa:reorder) { if(depth[pa]) rerootdp[pa] = Operator::func_reroot_dp(rerootdp[pa],rerootparent[pa]); size_t m = child[pa].size(); for(int j = 0; j < m && depth[pa]; ++j){ size_t ch = child[pa][j].first; rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],rerootparent[pa]); } if(m <= 1) continue; vector l(m),r(m); for(int j = 0; j < m; ++j) { size_t ch = child[pa][j].first; l[j] = rerootdp[ch]; r[j] = rerootdp[ch]; } for(int j = 1; j+1 < m; ++j) l[j] = Operator::func_reroot_merge(l[j],l[j-1]); for(int j = m-2; 0 <=j; --j) r[j] = Operator::func_reroot_merge(r[j],r[j+1]); size_t chl = child[pa].front().first; size_t chr = child[pa].back().first; rerootparent[chl] = Operator::func_reroot_dp(rerootparent[chl],r[1]); rerootparent[chr] = Operator::func_reroot_dp(rerootparent[chr],l[m-2]); for(int j = 1; j+1 < m; ++j) { size_t ch = child[pa][j].first; rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],l[j-1]); rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],r[j+1]); } } return rerootdp; } void make_eulertour() { dfs(reorder.front()); eulertour_range.resize(num); for(int i = 0; i < eulertour.size(); ++i) eulertour_range[eulertour[i]].second = i+1; for(int i = eulertour.size()-1; 0 <= i; --i) eulertour_range[eulertour[i]].first = i; } void make_heavy_light_decomposition(){ head.resize(num); hld.resize(num); iota(head.begin(),head.end(),0); for(size_t& pa:reorder) { pair maxi = {0,num}; for(auto& p:child[pa]) maxi = max(maxi,{subtree_size[p.first],p.first}); if(maxi.first) head[maxi.second] = head[pa]; } stack st_head,st_sub; size_t cnt = 0; //根に近い方から探索 for(size_t& root:reorder){ if(depth[root]) continue; //根をpush st_head.push(root); while(st_head.size()){ size_t h = st_head.top(); st_head.pop(); //部分木の根をpush st_sub.push(h); while (st_sub.size()){ size_t pa = st_sub.top(); st_sub.pop(); //部分木をカウントしていく hld[pa] = cnt++; //子を探索 for(auto& p:child[pa]) { //子のheadが親と同じなら、そのまま進む if(head[p.first]==head[pa]) st_sub.push(p.first); //そうじゃない場合は、そこから新しく部分木としてみなす else st_head.push(p.first); } } } } } //type 0: vertex, 1: edge vector> path_impl(size_t u,size_t v,int type = 0) { vector> path; while(1){ if(hld[u]>hld[v]) swap(u,v); if(head[u]!=head[v]) { path.push_back({hld[head[v]],hld[v]}); v=parent[head[v]].first; } else { path.push_back({hld[u],hld[v]}); break; } } reverse(path.begin(),path.end()); if(type) path.front().first++; return path; } pair>,vector>> ordered_path_impl(size_t u,size_t v,int type = 0) { vector> path_lca_to_u; vector> path_lca_to_v; while(1){ if(head[u] == head[v]) { if(depth[u] < depth[v]) path_lca_to_v.emplace_back(hld[u]+type,hld[v]); else path_lca_to_u.emplace_back(hld[v]+type,hld[u]); break; } else if(hld[u] < hld[v]) { path_lca_to_v.emplace_back(hld[head[v]],hld[v]); v = parent[head[v]].first; } else if(hld[u] > hld[v]) { path_lca_to_u.emplace_back(hld[head[u]],hld[u]); u = parent[head[u]].first; } } reverse(path_lca_to_v.begin(),path_lca_to_v.end()); return {path_lca_to_u,path_lca_to_v}; } size_t lca_idx_impl(size_t u,size_t v){ while(1){ if(hld[u]>hld[v]) swap(u,v); if(head[u]==head[v]) return u; v=parent[head[v]].first; } } vector head; public: vector depth; vector order; vector reorder; vector subtree_size; vector> parent; vector>> child; vector edge_dist; vector,Operator::bit>> ancestor; vector eulertour; vector> eulertour_range; vector hld; /** * O(N) builder */ static TreeBuilder builder(Graph& graph) { return TreeBuilder(graph);} /** * O(logN) after make_ancestor * return {lca,lca_dist} l and r must be connected */ pair lca(size_t l, size_t r) {return lca_impl(l,r);} /** * O(N) anytime * return {diameter size,diameter set} */ pair> diameter(void){return diameter_impl();} /** * O(N) after make_child */ template vector rerooting(const vector& rerootdp,const vector& rerootparent) {return rerooting_impl(rerootdp,rerootparent);} /** * O(logN) */ vector> vertex_set_on_path(size_t u, size_t v) {return path_impl(u,v,0);} /** /** * O(logN) */ vector> edge_set_on_path(size_t u, size_t v) {return path_impl(u,v,1);} /** * O(logN) * {lca to u path,lca to v path} */ pair>,vector>> vertex_ordered_set_on_path(size_t u, size_t v) {return ordered_path_impl(u,v,0);} /** * O(logN) * {lca to u path,lca to v path} */ pair>,vector>> edge_ordered_set_on_path(size_t u, size_t v) {return ordered_path_impl(u,v,1);} /** * O(logN) ancestorのlcaより定数倍軽め。idxだけ */ size_t lca_idx(size_t u, size_t v) {return lca_idx_impl(u,v);} }; template class TreeBuilder { bool is_root_made =false; bool is_child_made =false; bool is_parent_made=false; bool is_subtree_size_made=false; public: using TypeEdge = typename Operator::TypeEdge; TreeBuilder(Graph& g):tree(g){} TreeBuilder& root(const int rt) { is_root_made=true; tree.make_root(rt); return *this;} TreeBuilder& root() { is_root_made=true; tree.make_root(); return *this;} TreeBuilder& child() { assert(is_root_made); is_child_made=true; tree.make_child(); return *this;} TreeBuilder& parent() { assert(is_root_made); is_parent_made=true; tree.make_parent(); return *this;} TreeBuilder& subtree_size() { assert(is_child_made); is_subtree_size_made=true; tree.make_subtree_size(); return *this;} TreeBuilder& ancestor() { assert(is_parent_made); tree.make_ancestor(); return *this;} TreeBuilder& eulertour() { assert(is_child_made); tree.make_eulertour(); return *this;} TreeBuilder& heavy_light_decomposition() { assert(is_subtree_size_made); assert(is_parent_made); tree.make_heavy_light_decomposition(); return *this;} Tree&& build() {return move(tree);} private: Tree tree; }; template struct TreeOperator{ using TypeEdge = T; inline static constexpr size_t bit = 20; inline static constexpr TypeEdge unit_edge = 0; inline static constexpr TypeEdge unit_lca_edge = 0; inline static constexpr TypeEdge func_edge_merge(const TypeEdge& parent,const TypeEdge& w){return parent+w;} inline static constexpr pair func_lca_edge_merge(const pair& l,const pair& r){return make_pair(l.first,l.second+r.second);} template inline static constexpr TypeReroot func_reroot_dp(const TypeReroot& l,const TypeReroot& r) {return {l.first+r.first+r.second,l.second+r.second};} template inline static constexpr TypeReroot func_reroot_merge(const TypeReroot& l,const TypeReroot& r) {return {l.first+r.first,l.second+r.second};} }; //auto tree = Tree>::builder(g).build(); /** * @url * @est */ int main() { cin.tie(0);ios::sync_with_stdio(false); int N,K; cin >> N >> K; Graph g(N); for(int i=0;i> a >> b >> c; a--,b--; g.make_bidirectional_edge(a,b,c); } auto tree = Tree>::builder(g).root(0).parent().child().build(); vector cnt(N,0); int sum = 0; vector cost,value; for(int pa:tree.order) { if(tree.child[pa].empty()) { cnt[pa]++; sum += tree.edge_dist[pa]; } for(auto p:tree.child[pa]) { int ch = p.first; cnt[pa] += cnt[ch]; } auto w = tree.parent[pa].second; if(tree.depth[pa]) { value.push_back(cnt[pa]*w); cost.push_back(w); } } vector dp(K+1,0),tp(K+1,0); for(int i=0;i