Problem:
Score : 1900 points

Problem Statement

Ringo has a tree with N vertices.
The i-th of the N-1 edges in this tree connects Vertex A_i and Vertex B_i and has a weight of C_i.
Additionally, Vertex i has a weight of X_i.

Here, we define f(u,v) as the distance between Vertex u and Vertex v, plus X_u + X_v.

We will consider a complete graph G with N vertices.
The cost of its edge that connects Vertex u and Vertex v is f(u,v).
Find the minimum spanning tree of G.

Constraints

2 \leq N \leq 200,000

1 \leq X_i \leq 10^9

1 \leq A_i,B_i \leq N

1 \leq C_i \leq 10^9

The given graph is a tree.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N
A_1 B_1 C_1
A_2 B_2 C_2
:
A_{N-1} B_{N-1} C_{N-1}

Output

Print the cost of the minimum spanning tree of G.

Sample Input 1

4
1 3 5 1
1 2 1
2 3 2
3 4 3

Sample Output 1

22

We connect the following pairs: Vertex 1 and 2, Vertex 1 and 4, Vertex 3 and 4. The costs are 5, 8 and 9, respectively, for a total of 22.

Sample Input 2

6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15

Sample Output 2

359

Sample Input 3

2
1000000000 1000000000
2 1 1000000000

Sample Output 3

3000000000

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read() {
	char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=2e5+5;
struct E{int v,nxt,w;}e[maxn<<1];
struct Edge{int a,b;LL c;}E[maxn*55];
int sum[maxn],vis[maxn],head[maxn],mx[maxn],a[maxn],fa[maxn],sz[maxn];
int n,num,m,dx,S,rt;LL dw,pre[maxn];
inline void add(int x,int y,int z) {
	e[++num].v=y;e[num].nxt=head[x];head[x]=num;e[num].w=z;
}
void getroot(int x,int fa) {
	sum[x]=1,mx[x]=0;
	for(re int i=head[x];i;i=e[i].nxt) {
		if(vis[e[i].v]||e[i].v==fa) continue;
		getroot(e[i].v,x);sum[x]+=sum[e[i].v];
		mx[x]=max(mx[x],sum[e[i].v]);
	}
	mx[x]=max(mx[x],S-sum[x]);
	if(mx[x]<mx[rt]) rt=x;
}
void getdis(int x,int fa) {
	E[++m]=(Edge){dx,x,pre[x]+a[x]+dw};
	for(re int i=head[x];i;i=e[i].nxt) {
		if(vis[e[i].v]||e[i].v==fa) continue;
		getdis(e[i].v,x);
	}
}
void chk(int x,int fa) {
	if(pre[x]+a[x]<dw) dw=pre[x]+a[x],dx=x;
	for(re int i=head[x];i;i=e[i].nxt) {
		if(vis[e[i].v]||e[i].v==fa) continue;
		pre[e[i].v]=pre[x]+e[i].w;chk(e[i].v,x);
	}
}
void dfs(int x) {
	dw=1e18;vis[x]=1;pre[x]=0,chk(x,0),getdis(x,0);
	for(re int i=head[x];i;i=e[i].nxt) {
		if(vis[e[i].v]) continue;
		S=sum[e[i].v],rt=0,getroot(e[i].v,0),dfs(e[i].v);
	}
}
inline int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
inline int merge(int x,int y) {
	int xx=find(x),yy=find(y);
	if(xx==yy) return 0;
	if(sz[xx]<sz[yy]) fa[xx]=yy,sz[yy]+=sz[xx];
		else fa[yy]=xx,sz[xx]+=sz[yy];
	return 1;
}
inline int cmp(Edge A,Edge B) {return A.c<B.c;}
int main() {
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	for(re int x,y,z,i=1;i<n;i++) 
		x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z); 
	mx[0]=n+1,S=n,rt=0,getroot(1,0),dfs(rt);
	std::sort(E+1,E+m+1,cmp);
	LL ans=0;
	for(re int i=1;i<=n;i++) sz[i]=1,fa[i]=i;
	for(re int i=1;i<=m;i++) if(merge(E[i].a,E[i].b)) ans+=E[i].c;
	std::cout<<ans;
	return 0;
}