#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=210000;
struct Node
{
int t,w;
int next;
};
int p[maxn];
Node G[maxn];
int l;
int V;
void init()
{
memset(p,-1,sizeof(p));
l=0;
}
void addedge(int u,int t,int w,int l)
{
G[l].t=t;
G[l].w=w;
G[l].next=p[u];
p[u]=l;
}
int team[maxn];
long long son[maxn];
long long sum[maxn];
long long ans;
void dfs0(int u,int fath,int dis)
{
son[u]=team[u];
sum[1]+=(long long)team[u]*dis;
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
if(t==fath) continue;
dfs0(t,u,dis+w);
son[u]+=son[t];
}
}
void dfs(int u,int fath,int d)
{
sum[u]=sum[fath]-son[u]*d+(son[1]-son[u])*d;
ans=min(ans,sum[u]);
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
if(t==fath) continue;
dfs(t,u,w);
}
}
int main()
{
while(scanf("%d",&V)==1)
{
init();
for(int i=1;i<=V;i++) scanf("%d",&team[i]);
for(int i=1;i<V;i++)
{
int u,t,w;scanf("%d%d%d",&u,&t,&w);
addedge(u,t,w,l++);
addedge(t,u,w,l++);
}
sum[1]=0;
dfs0(1,-1,0);
ans=sum[1];
for(int i=p[1];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
dfs(t,1,w);
}
cout<<ans<<endl;
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=210000;
struct Node
{
int t,w;
int next;
};
int p[maxn];
Node G[maxn];
int l;
int V;
void init()
{
memset(p,-1,sizeof(p));
l=0;
}
void addedge(int u,int t,int w,int l)
{
G[l].t=t;
G[l].w=w;
G[l].next=p[u];
p[u]=l;
}
int team[maxn];
long long son[maxn];
long long sum[maxn];
long long ans;
void dfs0(int u,int fath,int dis)
{
son[u]=team[u];
sum[1]+=(long long)team[u]*dis;
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
if(t==fath) continue;
dfs0(t,u,dis+w);
son[u]+=son[t];
}
}
void dfs(int u,int fath,int d)
{
sum[u]=sum[fath]-son[u]*d+(son[1]-son[u])*d;
ans=min(ans,sum[u]);
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
if(t==fath) continue;
dfs(t,u,w);
}
}
int main()
{
while(scanf("%d",&V)==1)
{
init();
for(int i=1;i<=V;i++) scanf("%d",&team[i]);
for(int i=1;i<V;i++)
{
int u,t,w;scanf("%d%d%d",&u,&t,&w);
addedge(u,t,w,l++);
addedge(t,u,w,l++);
}
sum[1]=0;
dfs0(1,-1,0);
ans=sum[1];
for(int i=p[1];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
dfs(t,1,w);
}
cout<<ans<<endl;
}
return 0;
}