#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define map unordered_map
int best_path(int n,int m,int H[][2],int L[]){
  vector<vector<pair<int,int>>> G(n); for (int i(0);i < n-1;++i){
    G[H[i][0]].emplace_back(H[i][1],L[i]),G[H[i][1]].emplace_back(H[i][0],L[i]);
  }
  int r = n;
  auto dfs = [&](auto dfs,int x,int p,long long d,int dd,map<long long,int>& ret) -> void {
    vector<map<long long,int>> dp;
    for (auto [y,z]:G[x]) if (y!=p) dp.emplace_back(),dfs(dfs,y,x,d+z,dd+1,dp.back());
    sort(dp.begin(),dp.end(),[](auto& a,auto& b){
      return a.size()>b.size();
    });
    if (dp.empty()) dp.emplace_back();
    while(dp.size()>1){
      for (auto [a,b]:dp.back()) if (dp[0].contains(m-a+2*d)) r = min(r,b+dp[0][m-a+2*d]-2*dd);
      for (auto [a,b]:dp.back()){
        if (!dp[0].contains(a)) dp[0][a] = 1e9;
        dp[0][a] = min(dp[0][a],b);
      }
      dp.pop_back();
    }
    for (auto [a,b]:dp[0]) if (a-d==m) r = min(r,b-dd);
    dp[0][d] = dd,swap(dp[0],ret);
  };
  map<long long,int> M;
  dfs(dfs,0,-1,0,0,M);
  return (r==n?-1:r);
}
| # | 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... |