//#define _GLIBCXX_DEBUG #include using namespace std; #define endl '\n' #define lfs cout<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; templatebool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} templatebool chmax(T1 &a,T2 b){if(avoid ans(bool x,T1 y,T2 z){if(x)cout<void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;ivoid debug(vector&v,ll n){if(n!=0)cout<vector>vec(ll x, ll y, T w){vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,-1,0,0,1,1,-1,-1};vectordy={0,0,1,-1,1,-1,1,-1}; templatevector make_v(size_t a,T b){return vector(a,b);} templateauto make_v(size_t a,Ts... ts){return vector(a,make_v(ts...));} templateostream &operator<<(ostream &os, const pair&p){return os << p.first << " " << p.second;} templateostream &operator<<(ostream &os, const vector &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;} //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); struct LCA{ ll datasize = 18; ll n,root;//rootを根とする頂点数nの根付き木 vectordepth;//0から vector>G,data; LCA(vector>g,ll r):G(g),n(g.size()),root(r){ data.assign(n,vector(datasize,-1)); depth.assign(n,-1LL); build(); } void build(){ dfs(root,0); for(ll i=1;i=0?depth[x]:-1; } ll query(ll x,ll y){ if(depth[x]=1&&depth[y]>=deep(data[x][tmp]))tmp--; x = data[x][tmp]; } tmp = datasize - 1; while(x!=y){ while(tmp>=1&&data[x][tmp]==data[y][tmp])tmp--; x = data[x][tmp]; y = data[y][tmp]; } return x; } ll path(ll x,ll y){ return deep(x)+deep(y)-2*deep(query(x,y)); } ll par(ll x){ return data[x][0]; } }; template struct BIT{ ll n; ll k=1; vectordata; BIT() = default; BIT(ll size):n(size){ data.assign(n,0); while(k*2<=n)k*=2; } void add(ll a,T w){ for(ll i=a+1;i<=n;i+=i&-i)data[i-1]+=w; } T sum(ll a){ if(a<0)return 0; if(a>=n)a=n-1; T ret = 0; for(ll i=a+1;i>0;i-=i&-i)ret+=data[i-1]; return ret; } T sum(ll a,ll b){return a>b?0:sum(b)-sum(a-1);} T operator[](ll pos){ return sum(pos,pos); } ll lower_bound(ll x){ ll ret=0; for(ll i=k;i>0;i/=2){ if(ret+i<=n&&data[ret+i-1] map compress(vector &v){ sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); map ret; for(ll i=0;i<(ll)v.size();i++) ret[v[i]]=i; return ret; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; ll n,k;cin>>n>>k; vectorc(k),d(k); rep(i,0,k)cin>>c[i],c[i]--; rep(i,0,k)cin>>d[i]; vectorv(k+1); rep(i,0,k)v[i+1]=v[i]+d[i]; vector>g(n); rep(i,0,n-1){ ll a,b;cin>>a>>b;a--;b--; g[a].PB(b); g[b].PB(a); } LCA lca(g,0); vectorcl(k),cr(k),cld(k),crd(k); { cl[0]=c[0],cr[k-1]=c[k-1]; rep(i,0,k-1)cl[i+1]=lca.query(cl[i],c[i+1]); rrep(i,0,k-1)cr[i]=lca.query(cr[i+1],c[i]); rep(i,0,k)cld[i]=lca.depth[cl[i]]; rep(i,0,k)crd[i]=lca.depth[cr[i]]; } auto val=v; auto mp=compress(val); ll sz=mp.size(); struct zz{ ll d,v,lr; }; vectorord; { ll l=0,r=k-1; ll now=lca.query(c[l],c[r]); ord.PB({lca.depth[now],l,0}); ord.PB({lca.depth[now],r,1}); while(l+1lca.depth[lca.query(now,c[r]-1)]){ l++; now=lca.query(now,c[l]); ord.PB({lca.depth[now],l,0}); } else{ r--; now=lca.query(now,c[r]); ord.PB({lca.depth[now],r,1}); } } } ll sum=0; rep(i,0,k)sum+=d[i]; //ll ok=INF,ng=-INF; ll ok=INF,ng=-INF; ll lim=(k*(k+1)/2+1+1)/2-1; //cout<=2){ ll mid=(ok+ng)/2; ll m=mid-sum; ll num=0; { ll s=0; rep(i,0,k-1){ s+=d[i]; if(s+cld[i]<=mid)num++; } s=0; rrep(i,1,k){ s+=d[i]; if(s+crd[i]<=mid)num++; } } //cout<bl(sz),br(sz); for(auto [d,i,lr]:ord){ //cout<=lim)ok=mid; else ng=mid; } cout<