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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include<bits/stdc++.h> using namespace std;
using i64=long long; using pii=pair<int,int>;
const i64 INF=0x3f3f3f3f3f3f3f3f; const int N=1e6+5;
vector<int> e[N]; vector<array<int,3>> ed; int n,edge,m; int sz[N],mx[N]; i64 dep[N]; int vis[N]; int u[N],v[N],w[N]; int a[N]; i64 ans[N]; struct Line{ i64 k; i64 b; Line(){} Line(i64 _k,i64 _b){ k=_k; b=_b; } i64 val(int x){ return k*x+b; } }; struct Seg_Tree{ int tot; int rt; int lc[N<<5]; int rc[N<<5]; Line line[N<<5]; void clear(){ rt=tot=0; lc[0]=rc[0]=0; line[0]=Line(0,INF); } Seg_Tree(){ clear(); } int addnode(){ tot++; lc[tot]=rc[tot]=0; line[tot]=Line(0,INF); return tot; } void insert(int &x,int l,int r,int ql,int qr,Line li){ if(!x) x=addnode(); int mid=l+r>>1; if(l>=ql&&r<=qr){ if(li.val(l)<=line[x].val(l)&&li.val(r)<=line[x].val(r)){ line[x]=li; return; } else if(li.val(l)>line[x].val(l)&&li.val(r)>line[x].val(r)) return; else{ insert(lc[x],l,mid,ql,qr,li); insert(rc[x],mid+1,r,ql,qr,li); } return; } if(mid>=ql) insert(lc[x],l,mid,ql,qr,li); if(mid+1<=qr) insert(rc[x],mid+1,r,ql,qr,li); } void search(int x,int l,int r,int pos,i64 &res){ if(!x) return; res=min(res,line[x].val(pos)); if(l==r) return; int mid=l+r>>1; if(pos<=mid) search(lc[x],l,mid,pos,res); else search(rc[x],mid+1,r,pos,res); } }tr;
void get_edge(int x,int tot){ sz[x]=1; mx[x]=0; for(auto id:e[x]){ if(vis[id]) continue; vis[id]=1; int y=0; if(x!=v[id]) y=v[id]; else y=u[id]; get_edge(y,tot); sz[x]+=sz[y]; mx[id]=max(tot-sz[y],sz[y]); if(edge==0||mx[id]<mx[edge]) edge=id; vis[id]=0; } }
void dfs(int x,i64 dis,vector<int> &vec){ if(x<=m) vec.push_back(x); dep[x]=dis; for(auto id:e[x]){ if(vis[id]==1) continue; vis[id]=1; int y=0; if(x!=v[id]) y=v[id]; else y=u[id]; dfs(y,dis+w[id],vec); vis[id]=0; } }
void solve(int id){ vector<int> vecu,vecv; dfs(u[id],0,vecu); dfs(v[id],0,vecv); tr.clear(); for(auto x:vecu) tr.insert(tr.rt,1,N,1,N,Line(a[x],dep[x])); for(auto x:vecv){ i64 res=INF; tr.search(tr.rt,1,N,a[x],res); ans[x]=min(ans[x],res+dep[x]+w[id]); } tr.clear(); for(auto x:vecv) tr.insert(tr.rt,1,N,1,N,Line(a[x],dep[x])); for(auto x:vecu){ i64 res=INF; tr.search(tr.rt,1,N,a[x],res); ans[x]=min(ans[x],res+dep[x]+w[id]); } }
void divide(int x,int tot){ if(tot<=1) return; edge=0; get_edge(x,tot); vis[edge]=1; solve(edge); int nx=u[edge], ny=v[edge], szx=(sz[u[edge]]>sz[v[edge]]?tot-sz[v[edge]]:sz[u[edge]]), szy=(sz[u[edge]]>sz[v[edge]]?sz[v[edge]]:tot-sz[u[edge]]); divide(nx,szx); divide(ny,szy); return; }
void link(int x,vector<array<int,2>> &vec){ if(vec.size()==0) return; if(vec.size()==1){ ed.push_back({x,vec[0][0],vec[0][1]}); return; } vector<array<int,2>> vec1,vec2; for(int i=0;i<vec.size()/2;i++) vec1.push_back(vec[i]); for(int i=vec.size()/2;i<vec.size();i++) vec2.push_back(vec[i]); m++; ed.push_back({x,m,0}); link(m,vec1); m++; ed.push_back({x,m,0}); link(m,vec2); }
void init(int x,int fa){ vector<array<int,2>> vec; for(auto id:e[x]){ int y=0; if(x!=v[id]) y=v[id]; else y=u[id]; if(y==fa) continue; init(y,x); vec.push_back({y,w[id]}); } link(x,vec); }
int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<n;i++){ cin>>u[i]>>v[i]>>w[i]; e[u[i]].push_back(i); e[v[i]].push_back(i); }
m=n; init(1,1); for(int i=1;i<=n;i++) e[i].clear(); swap(n,m); for(int i=1;i<=ed.size();i++){ auto [x,y,z]=ed[i-1]; u[i]=x; v[i]=y; w[i]=z; e[x].push_back(i); e[y].push_back(i); }
for(int i=1;i<=m;i++) ans[i]=1ll*a[i]*a[i]; divide(1,n);
i64 sum=0; for(int i=1;i<=m;i++) sum+=ans[i];
cout<<sum;
return true&&false; }
|