#include #include #include #include #include #include #include #include #include #include using namespace std; int main() { int n, k; cin >> n >> k; if (k > n) { cout << -1 << endl; return 0; } vector> edges(n); for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; edges.at(a - 1).emplace(b - 1); edges.at(b - 1).emplace(a - 1); } deque searchingNode; for (int node : edges.at(0)) { searchingNode.emplace_back(node); } deque nextCandidateNode; unordered_set reachedNode; reachedNode.emplace(0); int depth = 0; long long result = 0; while (k > 1) { ++depth; for (int node : searchingNode) { --k; result += depth; if( k <= 1) { break; } for (int nextNode : edges.at(node)) { if (reachedNode.find(nextNode) == reachedNode.end()) { nextCandidateNode.emplace_back(nextNode); reachedNode.emplace(nextNode); } } } searchingNode.clear(); swap(searchingNode, nextCandidateNode); } cout << result << endl; }