1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <set> #include <map> #include <queue> #include <stack> #include <deque> #include <cmath> #include <bitset> #include <cstdio> #include <vector> #include <string> #include <complex> #include <utility> #include <cstring> #include <iostream> #include <assert.h> #include <algorithm> #include <unordered_set> #include <unordered_map> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define com complex<db> #define mp(x,y) make_pair((x),(y)) #define fi first #define se second #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&(-(x))) #define bit(x,i) (((x)>>(i))&1) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } const int N=150010; struct line{ ll k,b; ll f(int x){return k*x+b;} }; namespace SGT{ #define lc (u<<1) #define rc (u<<1|1) #define ls lc,l,mid #define rs rc,mid+1,r line mx[N<<2]; void init(int u,int l,int r) { mx[u].k=mx[u].b=0; if(l==r) return; int mid=l+r>>1; init(ls); init(rs); } ll ask(int u,int l,int r,int x) { if(l==r) return mx[u].f(x); int mid=l+r>>1; return max(mx[u].f(x),(x<=mid)?ask(ls,x):ask(rs,x)); } void add(int u,int l,int r,line A) { if(mx[u].f(l)<=A.f(l)&&mx[u].f(r)<=A.f(r)) {mx[u]=A; return;} if(mx[u].f(l)>=A.f(l)&&mx[u].f(r)>=A.f(r)) return; int mid=l+r>>1; if(mx[u].f(mid)<A.f(mid)) swap(mx[u],A); if(A.k<mx[u].k) add(ls,A); else add(rs,A); } }
int n; ll val[N],ans; vector<int> adj[N]; int siz[N],mxsiz[N],rt; int dep[N],fa[N],cnt; int st[N],len[N],from[N],top; ll sum[N],val1[N],val2[N]; bool vis[N]; inline void add(int x,int y) { adj[x].pb(y); adj[y].pb(x); } void getroot(int u,int cnt) { siz[u]=1; mxsiz[u]=0; for(auto v:adj[u]) if(!vis[v]&&v!=fa[u]) { fa[v]=u; getroot(v,cnt); siz[u]+=siz[v]; mxsiz[u]=max(mxsiz[u],siz[v]); } mxsiz[u]=max(mxsiz[u],cnt-siz[u]); if(mxsiz[rt]>mxsiz[u]) rt=u; } void dfs(int u,ll v1,ll v2,ll s,int id) { dep[u]=dep[fa[u]]+1; siz[u]=1; bool flag=0; for(auto v:adj[u]) if(!vis[v]&&v!=fa[u]) { fa[v]=u; flag=1; dfs(v,v1+s+val[v],v2+1ll*dep[u]*val[v],s+val[v],id); siz[u]+=siz[v]; } if(!flag) { st[++top]=u; cnt=max(cnt,dep[u]); len[top]=dep[u]; val1[top]=v1; val2[top]=v2; from[top]=id; sum[top]=s; } } inline void calc(int u) { st[top=0]=-1; dep[u]=1; cnt=0; for(auto v:adj[u]) if(!vis[v]) fa[v]=u,dfs(v,0ll+val[u]*2+val[v],val[v],val[v]+val[u],v); fo(i,1,top) sum[i]-=val[u],ans=max(ans,val1[i]),ans=max(ans,val2[i]+sum[i]+val[u]); st[top+1]=-1; SGT::init(1,1,cnt); for(int i=1,j;i<=top;i=j) { for(j=i;from[i]==from[j];j++) ans=max(ans,SGT::ask(1,1,cnt,len[j])+val1[j]); for(j=i;from[i]==from[j];j++) SGT::add(1,1,cnt,(line){sum[j],val2[j]}); } SGT::init(1,1,cnt); for(int i=top,j;i;i=j) { for(j=i;from[i]==from[j];j--) ans=max(ans,SGT::ask(1,1,cnt,len[j])+val1[j]); for(j=i;from[i]==from[j];j--) SGT::add(1,1,cnt,(line){sum[j],val2[j]}); } } void solve(int u) { vis[u]=1; calc(u); for(auto v:adj[u]) { if(vis[v]||siz[v]==1) continue; fa[v]=0; rt=0; getroot(v,siz[v]); solve(rt); } }
int main() { n=read(); fo(i,2,n) add(read(),read()); fo(i,1,n) val[i]=read(); mxsiz[rt=0]=(int)1e9; getroot(1,n); solve(rt); printf("%lld",ans); return 0; }
|