#include<bits/stdc++.h>
using namespace std;
struct node{int to,next;}e[200010];
int n,m,x,y,ans,cnt,s,a[400010],head[100010];
bool vis[100010],c[100010];
void add(){e[++cnt]={y,head[x]},head[x]=cnt,e[++cnt]={x,head[y]},head[y]=cnt;}
void dfs(int u,int fa)
{
    vis[u]=1,a[++ans]=u,c[u]^=1;
    for(int i=head[u];i;i=e[i].next)
        if(!vis[e[i].to])
            dfs(e[i].to,u),a[++ans]=u,c[u]^=1;
    if(c[u]){a[++ans]=fa,c[fa]^=1,a[++ans]=u,c[u]^=1;}
}
int main()
{
    scanf("%d%d",&n,&m);
    while(m--)scanf("%d%d",&x,&y),add();
    for(int i=1;i<=n;i++){cin >> c[i];if(c[i])s=i;}
    if(s){dfs(s,-1);for(int i=1;i<=n;i++)if(!vis[i]&&c[i]){puts("-1");return 0;}}
    if(ans>1&&a[ans-1]==-1)ans-=3;
    printf("%d\n",ans);
    for(int i=1;i<=ans;i++)printf("%d ",a[i]);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define M 100005

vector <int> G[M];
int n, m, st, x[M], cnt, ans[M * 4], now[M];
bool vis[M];

void dfs (int Index, int fa){
    vis[Index] = 1;
    ans[++ cnt] = Index;
    now[Index] ^= 1;
    for (int i = 0; i < G[Index].size (); i ++){
        int tmp = G[Index][i];
        if (! vis[tmp]){
            dfs (tmp, Index);
            now[Index] ^= 1;
            ans[++ cnt] = Index;
        }
    }
    if (now[Index] != x[Index]){
        ans[++ cnt] = fa;
        ans[++ cnt] = Index;
        now[fa] ^= 1;
        now[Index] ^= 1;
    }
}
int main (){
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= m; i ++){
        int u, v;
        scanf ("%d %d", &u, &v);
        G[u].push_back (v);
        G[v].push_back (u);
    }
    for (int i = 1; i <= n; i ++){
        scanf ("%d", &x[i]);
        if (x[i])
            st = x[i];
    }
    dfs (st, 0);
    for (int i = 1; i <= n; i ++){
        if (! vis[i] && x[i]){
            printf ("-1\n");
            return 0;
        }
    }
    if (ans[cnt - 1] == 0 && cnt > 1)
        cnt -= 3;
    printf ("%d\n", cnt);
    for (int i = 1; i <= cnt; i ++)
        printf ("%d ", ans[i]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int readint(){
    int x=0,f=1;char s=getchar();
    #define sc (s=getchar())
    while(s<'0'||s>'9'){
        if(s=='-')
            f=-1;
        sc;
    }
    while(s>='0'&&s<='9'){
        x=(x<<3)+(x<<1)+(s^48);
        sc;
    }
    #undef sc
    return x*f;
}
int head[maxn],edge_cnt=-1;
struct edge{
    int v,nxt;
    edge(){};
    edge(int V,int N){
        v=V;
        nxt=N;
    }
}e[maxn<<1];
void addedge(int u,int v){
    e[++edge_cnt]=edge(v,head[u]);
    head[u]=edge_cnt;
    e[++edge_cnt]=edge(u,head[v]);
    head[v]=edge_cnt;
}
int deg[maxn];
bool vis[maxn];
vector<int> ans;
void dfs(int x,int fa){
    vis[x]=1;
    deg[x]^=1;
    ans.push_back(x);
    for(int i=head[x];~i;i=e[i].nxt){
        int v=e[i].v;
        if(!vis[v]&&v!=fa){
            dfs(v,x);
            ans.push_back(x);
            deg[x]^=1;
        }
    }
    if(deg[x]==1&&fa!=-1){
        deg[x]^=1;
        deg[fa]^=1;
        ans.push_back(fa);
        ans.push_back(x);
    }
}
int main (){
    memset(head,-1,sizeof(head));
    int n=readint(),m=readint();
    for(int i=1;i<=m;i++){
        int u=readint(),v=readint();
        addedge(u,v);
    }
    for(int i=1;i<=n;i++){
        deg[i]=readint();
    }
    int be=1;
    for(int i=1;i<=n;i++){
        if(deg[i]==1){
            be=i;
            break;
        }
    }
    dfs(be,-1);
    if(deg[be]==1){
        ans.resize(ans.size()-1);
    }
    bool flag=0;
    for(int i=1;i<=n;i++){
        if(!vis[i]&&deg[i]==1){
            flag=1;
           break;
        }
    }
    if(flag){
        puts("-1");
        return 0;
    }
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++){
        printf("%d ",ans[i]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
struct node{
	int to,nxt;
}e[N];
int n,m,cnt=0,ans,s,vis[N],road[N<<2];
int a[N],head[N];
void add(int u,int v){
	e[++cnt].nxt=head[u];e[cnt].to=v;head[u]=cnt;
}
void dfs(int u,int fa)
{
	vis[u]=1;road[++ans]=u;a[u]^=1;
	for(int i=head[u];i;i=e[i].nxt){
		if(!vis[e[i].to]){
			dfs(e[i].to,u); road[++ans]=u;a[u]^=1;
		}
	}
	if(a[u]){
		road[++ans]=fa;a[fa]^=1;road[++ans]=u;a[u]^=1;
	}
}
int main()
{
	cin>>n>>m;int u,v;
	for(int i=1;i<=m;i++){
		scanf("%d %d",&u,&v);
		add(u,v);add(v,u);
	}
	for(int i=1;i<=n;i++){scanf("%d",&a[i]);if(a[i]) s=i;}
	if(s) dfs(s,-1);
	for(int i=1;i<=n;i++) if(!vis[i]&&a[i]){cout<<-1;return 0;}
	if(ans>1&&road[ans-1]==-1) ans-=3;
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++) printf("%d ",road[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=100005;

struct Edge{
	int next,to;
}edge[maxn*2];

vector<int> ans;

int n,m,nedge=0;
int head[maxn],x[maxn],vis[maxn];

void addedge(int a,int b){
	edge[nedge].to=b;
	edge[nedge].next=head[a];
	head[a]=nedge++;
}

void dfs(int v,int fa){
	vis[v]=1;
	x[v]^=1;
	ans.push_back(v);
	for (int i=head[v];i!=-1;i=edge[i].next){
		int u=edge[i].to;
		if (vis[u]) continue;
		dfs(u,v);
		ans.push_back(v);
		x[v]^=1;
	}
	if (x[v]){
		ans.push_back(fa);
		x[fa]^=1;
		ans.push_back(v);
		x[v]^=1;
	}
}

int check(int v){
	vis[v]=1;
	for (int i=head[v];i!=-1;i=edge[i].next){
		int u=edge[i].to;
		if (!vis[u]) check(u);
	}
}

int main(){
	memset(vis,0,sizeof(vis));
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	while(m--){
		int a,b;
		scanf("%d%d",&a,&b);
		addedge(a,b);
		addedge(b,a);
	}
	for (int i=1;i<=n;i++){
		scanf("%d",&x[i]);
	}
	int r=-1;
	for (int i=1;i<=n;i++){
		if (x[i]){
			r=i;
			break;
		}
	}
	if (r==-1){
		printf("0");
		return 0;
	}
	check(r);
	for (int i=1;i<=n;i++){
		if (x[i] && !vis[i]){
			printf("-1");
			return 0;
		}
	}
	for (int i=1;i<=n;i++){
		if (vis[i]){
			memset(vis,0,sizeof(vis));
			dfs(i,0);
			break;
		}
	}

	if (ans[ans.size()-2]==0){
		printf("%d\n",ans.size()-3);
		for (int i=0;i<ans.size()-3;i++){
			printf("%d ",ans[i]);
		}
	}
	else{
		printf("%d\n",ans.size());
		for (int i=0;i<ans.size();i++){
			printf("%d ",ans[i]);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define I inline
#define ri register int
#define int long long
#define ll long long
#define ull unsigned long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define swap(a,b) ((a)^=(b)^=(a)^=(b))
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define dow(i,a,b) for(ri i=(a);i>=(b);--i)
#define go(i,u) for(ri i=lnk[u];i;i=nxt[i])
#define mem(f,x) memset(f,x,sizeof(f))
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define vec vector
#define que queue
#define pb push_back
#define lowbit(x) (x&(-x))

I int gi(){
    int x=0;bool f=0;char ch=getchar();
    while(!isdigit(ch))f|=ch=='-',ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}

I void pi(int x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)pi(x/10);
    putchar(48+x%10);
}

I void pin(int x){pi(x);putchar('\n');}
I void pib(int x){pi(x);putchar(' ');}

const int N = 2e5+5,M = N<<1;

int lnk[N],nxt[M],to[M],ec;
I void add(int u,int v){to[++ec]=v;nxt[ec]=lnk[u];lnk[u]=ec;}

int n,m;
int a[N],s,ot[N],ans,vis[N];

void dfs(int u,int Fa){
    ot[++ans]=u;a[u]^=1;vis[u]=1;
    go(i,u)
    {
        int v=to[i];
        if(vis[v])continue;
        dfs(v,u);ot[++ans]=u;a[u]^=1;
    }
    if(a[u]){ot[++ans]=Fa;a[Fa]^=1;ot[++ans]=u;a[u]^=1;}
}

signed main(){
    n=gi(),m=gi();
    rep(i,1,m){int u=gi(),v=gi();add(u,v);add(v,u);}
    rep(i,1,n){a[i]=gi();if(a[i])s=i;}
    if(s){dfs(s,-1);rep(i,1,n){if(a[i] && !vis[i])puts("-1"),exit(0);}}
    if(ans>1&&ot[ans-1]==-1)ans-=3;
    pin(ans);
    rep(i,1,ans)pib(ot[i]);
}


#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<vector>
#define re register int
using namespace std;
typedef long long ll;
int read() {
	re x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9') {
		x=10*x+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int Size=100005;
int n,m,cnt,head[Size],a[Size];
struct Edge {
	int v,next;
} w[Size<<1];
void AddEdge(int u,int v) {
	w[++cnt].v=v;
	w[cnt].next=head[u];
	head[u]=cnt;
}
int tot,ans[Size<<2];
bool vis[Size];
void dfs(int x,int fa) {
	vis[x]=true;
	ans[++tot]=x;
	a[x]=!a[x];
	for(int i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v;
		if(!vis[nxt] && nxt!=fa) {
			dfs(nxt,x);
			ans[++tot]=x;
			a[x]^=1;
		}
	}
	if(a[x]) {
		ans[++tot]=fa;
		ans[++tot]=x;
		a[fa]=!a[fa];
		a[x]=!a[x];
	}
}
int main() {
	n=read();
	m=read();
	for(re i=1; i<=m; i++) {
		int u=read();
		int v=read();
		AddEdge(u,v);
		AddEdge(v,u);
	}
	int s=0;
	for(re i=1; i<=n; i++) {
		a[i]=read();
		if(!s && a[i])	s=i;
	}
	dfs(s,0);
	for(re i=1; i<=n; i++) {
		if(a[i] && !vis[i]) {
			printf("-1");
			return 0;
		}
	}
	if(tot>2 && !ans[tot-1]) {
		tot-=3;
	}
	printf("%d\n",tot);
	for(re i=1; i<=tot; i++) {
		printf("%d ",ans[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
#define maxn 4000005
using namespace std;
int m,n,Tt,son[maxn],d[maxn],x,y,Ans[maxn],lnk[maxn],nxt[maxn],tot,Q[maxn],Cnt,root;
bool vis[maxn];
inline int read(){
	int ret=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-f;ch=getchar();}
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret*f;
}
inline void add(int x,int y){nxt[++tot]=lnk[x];lnk[x]=tot;son[tot]=y;}
inline void dfs(int x){
	vis[x]=1;Tt+=d[x];
	for (int i=lnk[x];i;i=nxt[i]) if (!vis[son[i]]) dfs(son[i]);
}
inline void Write(int x){
	if (x<0){puts("-1");exit(0);}
	int N=0;while (x) Q[++N]=x%10,x/=10;
	for (int i=N;i;i--) putchar('0'+Q[i]);
}
inline void Dfs(int x){
	Ans[++Cnt]=x;vis[x]=1;
	for (int i=lnk[x];i;i=nxt[i])
	  if (!vis[son[i]]){
	  	Dfs(son[i]);
	  	d[x]^=1,Ans[++Cnt]=x;
	  	if (d[son[i]]&1) d[x]^=1,d[son[i]]^=1,Ans[++Cnt]=son[i],Ans[++Cnt]=x;
	  }
}
int main(){
	n=read(),m=read();
	for (int i=1;i<=m;i++) x=read(),y=read(),add(x,y),add(y,x);
	for (int i=1;i<=n;i++) d[i]=read();
	for (int i=1,Tot=0;i<=n;i++,Tt=0)
	  if (!vis[i]){
	     dfs(i);
	     if (Tt) root=i,Tot++;
	     if (Tot>1) Write(-1);
      }
    memset(vis,0,sizeof vis);
    Dfs(root);
    if (d[root]&1) Cnt--;
    Write(Cnt),putchar('\n');
    for (int i=1;i<=Cnt;i++) Write(Ans[i]),putchar(' ');
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N=1e5+10;
#define pii pair<int,int>

int read() {
	int a; scanf("%d",&a);
	return a;
}

int n,m,A[N],cnt[N],fuck;
bool vis[N];
vector<int> G[N];
vector<int> ans;

void dfs(int u,int fa) {
	ans.push_back(u); cnt[u]^=1; vis[u]=1;
	for(int siz=G[u].size(),v,i=0;i<siz;i++) {
		v=G[u][i];
		if(vis[v]) continue;
		dfs(v,u);
		ans.push_back(u);
		cnt[u]^=1;
	}
	if(cnt[u] != A[u] && u!=fuck) {
		ans.push_back(fa);
		ans.push_back(u);
		cnt[u]^=1; cnt[fa]^=1;
	}
}

int main() {
	cin>>n>>m;
	for(int i=1,u,v;i<=m;i++) {
		u=read(); v=read();
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int sum=0;
	for(int i=1;i<=n;i++) A[i]=read(),sum+=A[i];
	for(int i=1;i<=n;i++)
		if(A[i]==1) {
			fuck=i;
			dfs(i,0);
			for(int j=1;j<=n;j++)
				if(!vis[j] && A[j])
					return 0*printf("-1");
			int siz=ans.size();
			if(A[i]!=cnt[i]) siz--;
			cout<<siz<<endl;
			for(int j=0;j<siz;j++)
				cout<<ans[j]<<' ';
			break;
		}
	for(int j=1;j<=n;j++)
		if(!vis[j] && A[j])
			return 0*printf("-1");
	int siz=ans.size();
			cout<<siz<<endl;
			for(int j=0;j<siz;j++)
				cout<<ans[j]<<' ';
}


#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,u,v,s,tot;
int a[N],c[N],ans[N<<2];
bool vis[N];
int cnt,head[N];
struct edge{int next,to;}e[N<<1];

inline void add(int u,int v)
{
	cnt++;
	e[cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}

void dfs(int u,int fa)
{
	vis[u]=true; c[u]^=1; ans[++tot]=u;
	for (register int i=head[u]; i; i=e[i].next)
	if (!vis[e[i].to])
	{
		dfs(e[i].to,u);
		c[u]^=1; ans[++tot]=u;
	}
	if (c[u])
	{
		if (u==s) c[u]^=1;
		else c[fa]^=1,ans[++tot]=fa,c[u]^=1,ans[++tot]=u;
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for (register int i=1; i<=m; ++i) scanf("%d%d",&u,&v),add(u,v),add(v,u);
	for (register int i=1; i<=n; ++i)
	{
		scanf("%d",&c[i]);
		if (c[i] && !s) s=i;
	}
	dfs(s,0);
	for (register int i=1; i<=n; ++i) if (c[i]) {puts("-1"); return 0;}
	printf("%d\n",tot);
	for (register int i=1; i<=tot; ++i) printf("%d ",ans[i]); puts("");
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mz(y) memset(y,0,sizeof(y))
#define REP(i,n) for(i=0;i<(n);i++)
#define mp make_pair
#define pb push_back
const int maxn=111111;
const int maxm=222222;
struct Edge {int v,next;}e[maxm];
int en;
int head[maxn];
void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;}
void init() {
memset(head,-1,sizeof(head));
en=0;}
bool a[maxn];
int d[maxn];
int n,m;
int st,ed;
int vis[maxn];
vector<int>b;
void dfs(int x){vis[x]=1;
b.pb(x);
for(int i=head[x];i!=-1;i=e[i].next){
if (!vis[e[i].v]){dfs(e[i].v);b.pb(x);
vis[x]++;
if(vis[e[i].v]%2!=a[e[i].v]){
vis[e[i].v]++;vis[x]++;b.pb(e[i].v);b.pb(x);}}}
return;}
int farm(){
int i;
int st=-1;
for(i=1;i<=n;i++){
if(a[i]==1){st=i;break;}}
if(st==-1)return 0;
mz(vis);
b.clear();
dfs(st);
if(vis[st]%2!=a[st]){
vis[st]--;b.erase(b.begin());}
int maxi=b.size();
for(i=1;i<=n;i++)
if(vis[i]%2!=a[i])return -1;
return b.size();}
int main() {
int i,j,x,y;
init();
scanf("%d%d",&n,&m);
mz(d);REP(i,m){
scanf("%d%d",&x,&y);
add(x,y);add(y,x);d[x]++;d[y]++;}
for(i=1; i<=n; i++)scanf("%d",&a[i]);
int ans=farm();printf("%d\n",ans);
if(ans!=-1){int maxi=b.size();
if(maxi>0)printf("%d",b[0]);
for(i=1;i<maxi;i++){printf(" %d",b[i]);}
puts("");}
return 0;}


#include<bits/stdc++.h>

#define ll long long
#define re register int
#define For(i,a,b) for(ll i=(a); i<=(b); i++)
const int INF=1<<30;
const int mod=1e9+3;
using namespace std;
template <typename T>
inline void read(T &x) {
	x = 0;
	ll f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
			ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch ^ 48);
		ch = getchar();
	}
	x *= f;
	return;
}
template <typename T>
inline void write(T x){
	if(x < 0) {
		putchar('-');
		x = -x;
	}
	if(x > 9)
		write(x/10);
	putchar(x % 10 + '0');
	return;
}
inline ll qpow(ll a,ll b){
	ll x=1,y=a;
	while(b>=1){
		if(b%2==1) x=x*y%mod;
		y=y*y%mod;
		b/=2;
	}
	return x;
}
struct fuck{
	int next,to;
}you[100005];
int head[100005],n,m;
int q[100005];
int a[100005],s;
int tot=0,ans;
bool vis[100005];
inline void add(int u,int v){
	++tot;
	you[tot].to=v;
	you[tot].next=head[u];
	head[u]=tot;
}
inline dfs(int s,int fa){
	vis[s]=1;
	a[++ans]=s;
	q[s]^=1;
	for(int i=head[s];i;i=you[i].next){
		if(!vis[you[i].to]){
			dfs(you[i].to,s);
			a[++ans]=s;
			q[s]^=1;
		}
	}
	if(q[s]){
		a[++ans]=fa;
		q[fa]^=1;
		a[++ans]=s;
		q[s]^=1;
	}
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("a.in","r",stdin);
		freopen("a.out","w",stdout);
	#endif
	int x,y;
	read(n);
	read(m);
	For(i,1,m){
		read(x);
		read(y);
		add(x,y);
		add(y,x);
	}
	For(i,1,n){
		read(q[i]);
		if(q[i]==1){
			s=i;
		}
	}
	if(s){
		dfs(s,-1);
		For(i,1,n){
			if(!vis[i]&&q[i]){
				puts("-1");
				return 0;
			}
		}
	}
	if(ans>1&&a[ans-1]==-1){
		ans-=3;
	}
	write(ans);
	puts("");
	For(i,1,ans){
		printf("%d ",a[i]);
	}
}


#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=4e5+5;

int n,m,op[N],Ans[N],vis[N],gs,s,R,cnt;
vector<int> g[N];

inline void dfs(int u)
{
	vis[u]=1;
	s+=op[u];
	For(i,0,(int)g[u].size()-1) if(!vis[g[u][i]]) dfs(g[u][i]);
}

inline void redfs(int u)
{
	Ans[++cnt]=u;
	vis[u]=1;
	op[u]^=1;
	For(i,0,(int)g[u].size()-1)
	{
		int v=g[u][i];
		if(vis[v]) continue;
		redfs(v);
		op[u]^=1;
		Ans[++cnt]=u;
		if(op[v]&1) op[v]^=1,op[u]^=1,Ans[++cnt]=v,Ans[++cnt]=u;
	}
}

int main()
{
	io.read(n),io.read(m);
	For(i,1,m)
	{
		int x,y;
		io.read(x),io.read(y);
		g[x].pb(y),g[y].pb(x);
	}
	For(i,1,n) io.read(op[i]);
	For(i,1,n) if(!vis[i])
	{
		s=0; dfs(i);
		if(s) R=i,gs++;
		if(gs>1) GG;
	}
	For(i,1,n) vis[i]=0;
	redfs(R);
	if(op[R]&1) cnt--;
	io.write(cnt),puts("");
	For(i,1,cnt) io.write(Ans[i]),putchar(' ');
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

inline int read(){
    int x = 0; int zf = 1; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}

struct Edge{
    int to, next;
} edges[400005];

int head[200005], edge_num = 0;

int ans[800005], sta[200005];
bool vis[200005];
int num;

void addEdge(int u, int v){
    edges[++edge_num] = (Edge){v, head[u]};
    head[u] = edge_num;
}

void DFS(int u, int fa){
    vis[u] = 1, sta[u] ^= 1;
    ans[++num] = u;
    int v;
    for (int c_e = head[u]; c_e; c_e = edges[c_e].next){
        v = edges[c_e].to;
        if(!vis[v] && (v != fa)){
            DFS(v, u);
            ans[++num] = u;
            sta[u] ^= 1;
        }
    }
    if ((sta[u] == 1) && fa != -1){
        ans[++num] = fa, ans[++num] = u;
        sta[u] ^= 1, sta[fa] ^= 1;
    }
}

int main(){
    int n = read(), m = read();
    for (int i = 0; i < m; ++i){
        int u = read(), v = read();
        addEdge(u, v), addEdge(v, u);
    }
    int st = 0;
    for (int i = 1; i <= n; ++i){
        sta[i] = read();
        if(sta[i])
            st = i;
    }
    if(st){
        DFS(st, -1);
        if(sta[st])
            --num;
    }
    for(int i = 1; i <= n; ++i)
        if(!vis[i] && sta[i]){
            printf("-1");
            return 0;
        }
    printf("%d\n", num);
    for(int i = 1; i <= num; ++i){
        if (i != 1)
            printf(" ");
        printf("%d", ans[i]);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,ans[100010<<2],tot,lin[100010],root,w[100010],vis[100010];
struct edge{
	int to,nxt;
}a[100010];
void add(int x,int y)
{
	a[tot].to=y;
	a[tot].nxt=lin[x];
	lin[x]=tot++;
}
void dfs(int x,int fa)
{
	w[x]^=1;
	ans[++tot]=x;
	vis[x]=1;
	for(int i=lin[x];i!=-1;i=a[i].nxt)
	{
		if(!vis[a[i].to])
		{
			dfs(a[i].to,x);
			w[x]^=1;
			ans[++tot]=x;
		}
	}
	if(fa!=-1&&w[x])
	{
		ans[++tot]=fa;
		w[fa]^=1;
		ans[++tot]=x;
		w[x]^=1;
	}
}
int main()
{
	memset(lin,-1,sizeof(lin));
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	tot=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&w[i]);
		if(w[i])
			root=i;
	}
	if(root)
	{
		dfs(root,-1);
		for(int i=1;i<=n;i++)
		{
			if(w[i]&&!vis[i])
			{
				printf("-1");
				return 0;
			}
		}
	}
	if(w[root])
		tot--;
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++)
	{
		printf("%d ",ans[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=100005;

struct Edge{
	int next,to;
}edge[maxn*2];

vector<int> ans;

int n,m,nedge=0;
int head[maxn],x[maxn],vis[maxn];

void addedge(int a,int b){
	edge[nedge].to=b;
	edge[nedge].next=head[a];
	head[a]=nedge++;
}

void dfs(int v,int fa){
	vis[v]=1;
	x[v]^=1;
	ans.push_back(v);
	for (int i=head[v];i!=-1;i=edge[i].next){
		int u=edge[i].to;
		if (vis[u]) continue;
		dfs(u,v);
		ans.push_back(v);
		x[v]^=1;
	}
	if (x[v]){
		ans.push_back(fa);
		x[fa]^=1;
		ans.push_back(v);
		x[v]^=1;
	}
}

int main(){
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	while(m--){
		int a,b;
		scanf("%d%d",&a,&b);
		addedge(a,b);
		addedge(b,a);
	}
	for (int i=1;i<=n;i++){
		scanf("%d",&x[i]);
	}
	int r=-1;
	for (int i=1;i<=n;i++){
		if (x[i]){
			r=i;
			break;
		}
	}
	if (r==-1){
		printf("0");
		return 0;
	}
	dfs(r,0);
	for (int i=1;i>=n;i++){
		if (x[i] && vis[i]){
			printf("-1");
			return 0;
		}
	}
	if (ans[ans.size()-2]==0){
		printf("%d\n",ans.size()-3);
		for (int i=0;i<ans.size()-3;i++){
			printf("%d ",ans[i]);
		}
	}
	else{
		printf("%d\n",ans.size());
		for (int i=0;i<ans.size();i++){
			printf("%d ",ans[i]);
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define M 100005

vector <int> G[M];
int n, m, st, x[M], cnt, ans[M * 4], now[M];
bool vis[M];

void dfs (int Index, int fa){
    vis[Index] = 1;
    ans[++ cnt] = Index;
    now[Index] ^= 1;
    for (int i = 0; i < G[Index].size (); i ++){
        int tmp = G[Index][i];
        if (! vis[tmp]){
            dfs (tmp, Index);
            now[Index] ^= 1;
            ans[++ cnt] = Index;
        }
    }
    if (now[Index] != x[Index]){
        ans[++ cnt] = fa;
        ans[++ cnt] = Index;
        now[fa] ^= 1;
        now[Index] ^= 1;
    }
}
int main (){
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= m; i ++){
        int u, v;
        scanf ("%d %d", &u, &v);
        G[u].push_back (v);
        G[v].push_back (u);
    }
    for (int i = 1; i <= n; i ++){
        scanf ("%d", &x[i]);
        if (x[i])
            st = x[i];
    }
    dfs (st, -1);
    for (int i = 1; i <= n; i ++){
        if (! vis[i] && x[i]){
            printf ("-1\n");
            return 0;
        }
    }
    if (ans[cnt - 1] == -1 && cnt > 1)
        cnt -= 3;
    printf ("%d\n", cnt);
    for (int i = 1; i <= cnt; i ++)
        printf ("%d ", ans[i]);
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
int n,m,a[100005],ans[400005],cnt,s;
vector<int>G[100005];
bool vis[100005];
void dfs(int step,int fa){
	vis[step] = 1;
	ans[++ cnt] = step;
	a[step] ^= 1;
	for (int i = 0;i < G[step].size();i ++){
		if (!vis[G[step][i]]){
			dfs(G[step][i],step);
			a[step] ^= 1;
			ans[++ cnt] = step;
		}
	}
	if (a[step]){
		a[fa] ^= 1;
		a[step] ^= 1;
		ans[++ cnt] = fa;
		ans[++ cnt] = step;
	}
}
int main(){
	scanf ("%d%d",&n,&m);
	for (int i = 1;i <= m;i ++){
		int a,b;
		scanf ("%d%d",&a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for (int i = 1;i <= n;i ++){
		scanf ("%d",&a[i]);
		if (a[i])
			s = i;
	}
	if (!s){
		printf("0\n");
		return 0;
	}
	else {
		dfs(s,-1);
		for (int i = 1;i <= n;i ++)
			if (a[i] || !vis[i]){
				printf("-1\n");
				return 0;
			}
		if (cnt > 1 && ans[cnt - 1] == -1)
			cnt -= 3;
		printf("%d\n",cnt);
		for (int i = 1;i < cnt;i ++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[cnt]);
	}
}


#include <bits/stdc++.h>
using namespace std;

// why am I so weak

int n, m;
int x[100055];
bool used[100055];
vector<int> g[100055];

void dfs(int v, int par, vector<int> &res) {
	used[v] = true;
	x[v] ^= true;

	res.push_back(v);

	for (auto u : g[v]) {
		if (used[u]) continue;

		dfs(u, v, res);

		x[v] ^= true;
		res.push_back(v);
	}

	if (x[v] && ~par) {
		x[v] = 0;
		x[par] ^= true;

		res.push_back(par);
		res.push_back(v);
	}
}
int main() {
	scanf("%d %d", &n, &m);

	while (m--) {
		int x, y;
		scanf("%d %d", &x, &y);
		x--, y--;

		g[x].push_back(y);
		g[y].push_back(x);
	}

	for (int i = 0; i < n; i++) scanf("%d", &x[i]);

	vector<int> res;

	for (int i = 0; i < n; i++) if (!used[i]) {
		dfs(i, -1, res);

		if (x[i]) {
			x[i] = 0
			res.pop_back();
		}

		if (res.empty()) continue;

		break;
	}

	for (int i = 0; i < n; i++) if (x[i]) {
		puts("-1");
		return 0;
	}

	printf("%d\n", (int)res.size());

	for (int i = 0; i < (int)res.size(); i++) {
		if (i) printf(" ");
		printf("%d", res[i] + 1);
	}

	puts("");

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int r[200001];

pair<int,int> root(int i){
	int cnt=0;
	while(r[i]!=i){
		i=r[i];
		cnt++;
	}
	return make_pair(i,cnt);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif

    int n,m;
    cin >> n >> m;
    vector < pair<long long int,int> > a;
    long long int temp;
    for(int i=1;i<=n;i++){
    	r[i]=i;
    	cin >> temp;
    	a.push_back({temp,i});
    }
    sort(a.begin(),a.end());
    vector < pair< long long int,pair<int,int> > >edges;
    for(int i=1;i<n;i++){
    	edges.push_back({a[0].first+a[i].first,{a[0].second,a[i].second}});
    }
    for(int i=0;i<m;i++){
    	long long int w;
    	int u,v;
    	cin >> u >> v >> w;
    	edges.push_back({w,{u,v}});
    }
    sort(edges.begin(),edges.end());
    long long int ans=0;
    int cmp=0;
    for(auto i:edges){
    	int u = i.second.first,v = i.second.second;
    	pair<int,int> pu = root(u),pv=root(v);
    	if(pu.first==pv.first){
    		continue;
    	}
    	if(pu.second>pu.second){
    		r[pu.first]=pv.first;
    	}
    	else{
    		r[pv.first]=pu.first;
    	}
    	ans+=i.first;
    	cmp++;
    	if(cmp==n-1){
    		break;
    	}
    }
    cout << ans;

}


#include <bits/stdc++.h>
using namespace std;
int r[200001];

pair<int,int> root(int i){
	int cnt=0;
	while(r[i]!=i){
		i=r[i];
		cnt++;
	}
	return make_pair(i,cnt);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif

    int n,m;
    cin >> n >> m;
    vector < pair<long long int,int> > a;
    long long int temp;
    for(int i=1;i<=n;i++){
    	r[i]=i;
    	cin >> temp;
    	a.push_back({temp,i});
    }
    sort(a.begin(),a.end());
    vector < pair< long long int,pair<int,int> > >edges;
    for(int i=1;i<n;i++){
    	edges.push_back({a[0].first+a[i].first,{a[0].second,a[i].second}});
    }
    for(int i=0;i<m;i++){
    	long long int w;
    	int u,v;
    	cin >> u >> v >> w;
    	edges.push_back({w,{u,v}});
    }
    sort(edges.begin(),edges.end());
    long long int ans=0;
    int cmp=0;
    for(auto i:edges){
    	int u = i.second.first,v = i.second.second;
    	pair<int,int> pu = root(u),pv=root(v);
    	if(pu.first==pv.first){
    		continue;
    	}
    	if(pv.second>pu.second){
    		r[pu.first]=pv.first;
    	}
    	else{
    		r[pv.first]=pu.first;
    	}
    	ans+=i.first;
    	cmp++;
    	if(cmp==n-1){
    		break;
    	}
    }
    cout << ans;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
int i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:

		ll cost;
	public:
	MST(){
		int parent[n];
		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	int findParent(int a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<int,int> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,int> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<int> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	vector<pair<ll,pair<int,int> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<t;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	int l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
ll i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
			ll parent[S];
		ll cost;
	public:
	MST(){

		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	ll findParent(ll a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<ll,ll> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,ll> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<ll> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	//cout<<t;
	vector<pair<ll,pair<ll,ll> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<1;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	ll l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
int i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
			int parent[n];
		ll cost;
	public:
	MST(){

		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	int findParent(int a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<int,int> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,int> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<int> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	vector<pair<ll,pair<int,int> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<t;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	int l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
ll p;
ll i,n,m,x,y,k,w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
		ll parent[S];
		ll cost;
	public:
	MST(){
		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	ll findParent(ll a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<ll,ll> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,ll> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<ll> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	vector<pair<ll,pair<ll,ll> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=1;i<n;i++)
	{
		for(ll j=0;j<t;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	ll l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
int i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
			int parent[S];
		ll cost;
	public:
	MST(){

		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	int findParent(int a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<int,int> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,int> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<int> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	cout<<t;
	vector<pair<ll,pair<int,int> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<1;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	int l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
int i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
			int parent[S];
		ll cost;
	public:
	MST(){

		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	int findParent(int a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<int,int> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,int> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<int> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	//cout<<t;
	vector<pair<ll,pair<int,int> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<1;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	int l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)2000006

vector<pair<ll,ll> > graph[S];
bool arr[S];
ll p;
ll i,n,m,x,y,k,w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
		ll parent[S];
		ll cost;
	public:
	MST(){
		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	ll findParent(ll a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<ll,ll> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,ll> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<ll> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	vector<pair<ll,pair<ll,ll> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		graph[x].pb({y,w});
		graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=1;i<n;i++)
	{
		for(ll j=0;j<t;j++)
		{
			vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	ll l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sc second
#define fr first
#define S (ll)200006

//vector<pair<ll,ll> > graph[S];
bool arr[S];
int i,n,m,x,y,k;
ll w;
//void bfs()
//{
//	queue<pair<ll,ll> > q;
//	q.push({1,0});
//	vis[1]=1;
//	while(!q.empty())
//	{
//		pair<ll,ll> temp=q.front();
//		q.pop();
//		ll level=temp.sc;
//		ll node=temp.fr;
//		l[node]=level;
//																						//incomplete
//
//		//cout<<node<<" ";
//		for(ll j=0;j<graph[node].size();j++)
//		{
//			if(!vis[graph[node][j]])
//			{
//				q.push({graph[node][j],level+1});
//				vis[graph[node][j]]=1;
//				pa[graph[node][j]]=node;
//			}
//		}
//	}
//
//}
//ll dfs1(ll node,ll pa)
//{
//	//cout<<"in node "<<node;
//	vis[node]=true;
//	ll sum=0;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//
//				sum+=dfs1(next,node);
//				if(st.find({next,node})!=st.end())
//				{
//					graph[node][i].sc=1;
//					sum++;
//				}
//		}
//	}
//	//cout<<"returning from "<<node;
//	ans[node]=sum;
//	return sum;
//}
//void dfs2(ll node,ll pa,ll sum)
//{
//	vis[node]=true;
//	for(ll i=0;i<graph[node].size();i++)
//	{
//		pair<ll,ll> temp = graph[node][i];
//		ll next = temp.fr;
//
//		if(!vis[next])
//		{
//			if(temp.sc)
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]-1);
//			}
//			else
//			{
//				dfs2(next,node,sum+ ans[node] - ans[next]+1);
//			}
//		}
//	}
//	fin[node]=sum+ans[node];
//	return;
//}
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<b.first);
}
bool cmp2(pair<ll,pair<ll,ll> > a,pair<ll,pair<ll,ll> > b)
{
	return (a.first<b.first);
}
class MST{
	public:
			int parent[S];
		ll cost;
	public:
	MST(){

		cost=0;
		for(i=0;i<=n;i++)
		{
			parent[i]=i;
		}
		//cout<<"in cons\n";
	}
	public:
	int findParent(int a){
		if(parent[a]!=a)
		{
			return parent[a]=findParent(parent[a]);
		}
		return a;
	}
	public:
	bool unionn(pair<ll,pair<int,int> > edge)
	{
		ll a=edge.sc.fr;
		ll b=edge.sc.sc;

		ll p1=findParent(a);
		ll p2=findParent(b);
		if(p1!=p2)
		{
			parent[p2]=p1;
			//cout<<"edge "<<a<<" "<<b<<endl;
			cost+=edge.fr;

			return true;
		}
		else
		{
			return false;
		}
	}
	public:
		ll getcost()
		{
			return cost;
		}
};

int main()
{
	cin>>n>>m;
	vector<pair<ll,int> > arr;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		arr.pb({x,i});
	}
	sort(arr.begin(),arr.end(),cmp);
	i=0;
	vector<int> temp;
	while(arr[0].fr==arr[i].fr)
	{
		temp.pb(arr[i].sc);
		i++;
	}
	ll t=temp.size();
	vector<pair<ll,pair<int,int> > > vc;
	for(i=0;i<m;i++)
	{
		cin>>x>>y>>w;
		//graph[x].pb({y,w});
		//graph[y].pb({x,w});
		vc.pb({w,{x,y}});
	}
	for(i=0;i<n;i++)
	{
		for(ll j=0;j<t;j++)
		{
			if(i!=j)
			{
				vc.pb({arr[j].fr+arr[i].fr,{arr[j].sc,arr[i].sc}});
			}
		}
	}
	sort(vc.begin(),vc.end(),cmp2);
	int l=vc.size();
	MST tree;
	for(i=0;i<l;i++)
	{
		tree.unionn(vc[i]);
	}
	ll ans=tree.getcost();
	cout<<ans<<endl;
	return 0;

}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

bool visited[MAXN];

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000000000l;
	int nd;

	i64 sum = 0;

	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + (n - 1));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		add(u-1, v-1, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;
	metric[0] = ans = 0;
	memset(visited, 0, sizeof(visited));
	pq.push(make_pair(0,0));
	while(!pq.empty()) {
		pair<i64, int> pr = pq.top(); pq.pop();
		if(visited[pr.second]) continue;

		ans += pr.first;
		visited[pr.second] = true;
		for(int i = 0; i < (int)graph[pr.second].size(); i++) {
			pair<i64, int> cur = graph[pr.second][i];
			if(!visited[cur.second] && metric[cur.second] > cur.first) {
				pq.push(cur);
				metric[cur.second] = cur.first;
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

bool visited[MAXN];

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000l;
	int nd;

	i64 sum = 0;

	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	/*if(m == 0) {
		printf("%lli", (sum - minw) + (n - 1));
		return;
	}*/

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		u--; v--;
		add(u, v, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	i64 dist;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;

	metric[0] = ans = 0;

	memset(visited, 0, sizeof(visited));

	for (pq.push( make_pair( 0, 0 ) ); !pq.empty(); pq.pop() ) {

		pli top = pq.top();
		u = top.second;
		dist = top.first;

		if(visited[u] || dist > metric[u])continue;

		for(int i = 0; i < (int)graph[u].size(); i++) {
			v = graph[u][i].second;
			w = graph[u][i].first;
			if(!visited[v] && w < metric[v]) {
				metric[v] = w;
				ans += w;
				pq.push(make_pair(metric[v], v));
			}
		}
		visited[u] = 1;
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define MAXE 400004
#define oo 9.223372e+18
//#define size( u ) ( -parent[u] )

int n,m;
int edge_count;

i64 data[MAXN];
int parent[MAXN];

map<pii, i64> mapv;

struct Edge {
	int u, v; i64 w;
	bool operator < ( const Edge& b ) const { return w < b.w; }
} graph[MAXE];

int size(int u) {
	return ( -parent[u] );
}

int find( int u ) {
	return ( parent[u] < 0 ) ? u : parent[u] = find( parent[u] );
}

void join( int u, int v ) {
	u = find( u ); v = find( v );
	if ( size( u ) < size( v ) )
		swap( u, v );
	parent[u] += parent[v];
	parent[v] = u;
}

void add(int u, int v, i64 w) {
	pii key = make_pair(min(u, v), max(u, v));
	i64 value = mapv[key];
	if(!value || value > w) mapv[key] = w;
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = oo;
	i64 sum = 0;

	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}
	edge_count = 0;
	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		//graph[edge_count++] = ( Edge ) { i, nd, weight };
		add(i, nd, weight);
	}
	memset( parent, -1, sizeof( parent ) );
	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		//graph[edge_count++] = ( Edge ) { u, v, w };
		add(u-1, v-1, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;
		graph[edge_count++] = ( Edge ) { key.first, key.second, value };
	}

	sort( graph, graph + edge_count );
	i64 ans;
	ans = u = 0;
	for ( int i = 0; i < edge_count; i++ )
		if ( find( graph[i].u ) != find( graph[i].v ) ) {
			ans += graph[i].w;
			join( graph[i].u, graph[i].v );
		}

	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

bool visited[MAXN];

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000000000l;
	int nd;

	i64 sum = 0;

	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		add(u-1, v-1, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;
	metric[0] = ans = 0;
	memset(visited, 0, sizeof(visited));
	pq.push(make_pair(0,0));
	while(!pq.empty()) {
		pair<i64, int> pr = pq.top(); pq.pop();
		if(visited[pr.second]) continue;

		ans += pr.first;
		visited[pr.second] = true;
		for(int i = 0; i < (int)graph[pr.second].size(); i++) {
			pair<i64, int> cur = graph[pr.second][i];
			if(!visited[cur.second] && metric[cur.second] > cur.first) {
				pq.push(cur);
				metric[cur.second] = cur.first;
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 9.223372e+18

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

//map<pii, i64> mapv;

bool visited[MAXN];

/*void add(int u, int v, i64 w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}*/

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = oo;
	i64 sum = 0;

	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		//add(i, nd, weight);
		graph[i].push_back(make_pair(weight, nd));
		graph[nd].push_back(make_pair(weight, i));
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		graph[u].push_back(make_pair(w, v));
		graph[v].push_back(make_pair(w, u));

		//add(u-1, v-1, w);
	}

	/*for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}*/

	i64 ans;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;
	metric[0] = ans = 0;
	memset(visited, 0, sizeof(visited));
	pq.push(make_pair(0,0));
	while(!pq.empty()) {
		pair<i64, int> pr = pq.top(); pq.pop();
		if(visited[pr.second]) continue;

		ans += pr.first;
		visited[pr.second] = true;
		for(int i = 0; i < (int)graph[pr.second].size(); i++) {
			pair<i64, int> cur = graph[pr.second][i];
			if(!visited[cur.second] && metric[cur.second] > cur.first) {
				pq.push(cur);
				metric[cur.second] = cur.first;
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000l;
	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		u--; v--;
		add(u, v, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	i64 dist;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;

	metric[0] = ans = 0;

	for (pq.push( make_pair( 0, 0 ) ); !pq.empty(); pq.pop() ) {

		pli top = pq.top();
		u = top.second;
		dist = top.first;

		if(dist > metric[u])continue;

		for(int i = 0; i < (int)graph[u].size(); i++) {
			v = graph[u][i].second;
			w = graph[u][i].first;
			if(w < metric[v]) {
				metric[v] = w;
				ans += w;
				pq.push(make_pair(metric[v], v));
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000l;
	int nd;

	i64 sum = 0;

	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + (n - 1));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		u--; v--;
		add(u, v, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	i64 dist;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;

	metric[0] = ans = 0;

	for (pq.push( make_pair( 0, 0 ) ); !pq.empty(); pq.pop() ) {

		pli top = pq.top();
		u = top.second;
		dist = top.first;

		if(dist > metric[u])continue;

		for(int i = 0; i < (int)graph[u].size(); i++) {
			v = graph[u][i].second;
			w = graph[u][i].first;
			if(w < metric[v]) {
				metric[v] = w;
				ans += w;
				pq.push(make_pair(metric[v], v));
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define MAXE 400004
#define oo 9.223372e+18
//#define size( u ) ( -parent[u] )

int n,m;
int edge_count;

i64 data[MAXN];
int parent[MAXN];

map<pii, i64> mapv;

struct Edge {
	int u, v; i64 w;
	bool operator < ( const Edge& b ) const { return w < b.w; }
} graph[MAXE];

int size(int u) {
	return ( -parent[u] );
}

int find( int u ) {
	return ( parent[u] < 0 ) ? u : parent[u] = find( parent[u] );
}

void join( int u, int v ) {
	u = find( u ); v = find( v );
	if ( size( u ) < size( v ) )
		swap( u, v );
	parent[u] += parent[v];
	parent[v] = u;
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = oo;
	i64 sum = 0;

	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}
	edge_count = 0;
	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		graph[edge_count++] = ( Edge ) { i, nd, weight };
	}
	memset( parent, -1, sizeof( parent ) );
	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		graph[edge_count++] = ( Edge ) { u-1, v-1, w };
	}

	sort( graph, graph + edge_count );
	i64 ans;
	ans = u = 0;
	for ( int i = 0; i < edge_count; i++ )
		if ( find( graph[i].u ) != find( graph[i].v ) ) {
			ans += graph[i].w;
			join( graph[i].u, graph[i].v );
		}

	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 100000000000000l

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

bool visited[MAXN];

void add(int u, int v, int w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = 1000000000000l;
	int nd;

	i64 sum = 0;

	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + (n - 1));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		add(u-1, v-1, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;
	metric[0] = ans = 0;
	memset(visited, 0, sizeof(visited));
	pq.push(make_pair(0,0));
	while(!pq.empty()) {
		pair<i64, int> pr = pq.top(); pq.pop();
		if(visited[pr.second]) continue;

		ans += pr.first;
		visited[pr.second] = true;
		for(int i = 0; i < (int)graph[pr.second].size(); i++) {
			pair<i64, int> cur = graph[pr.second][i];
			if(!visited[cur.second] && metric[cur.second] > cur.first) {
				pq.push(cur);
				metric[cur.second] = cur.first;
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define MAXE 400004
#define oo 9.223372e+18
//#define size( u ) ( -parent[u] )

int n,m;
int edge_count;

i64 data[MAXN];
int parent[MAXN];

map<pii, i64> mapv;

struct Edge {
	int u, v; i64 w;
	bool operator < ( const Edge& b ) const { return w < b.w; }
} graph[MAXE];

int size(int u) {
	return ( -parent[u] );
}

int find( int u ) {
	return ( parent[u] < 0 ) ? u : parent[u] = find( parent[u] );
}

void join( int u, int v ) {
	u = find( u ); v = find( v );
	if ( size( u ) < size( v ) )
		swap( u, v );
	parent[u] += parent[v];
	parent[v] = u;
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = oo;
	i64 sum = 0;

	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}
	edge_count = 0;
	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		graph[edge_count++] = ( Edge ) { i, nd, weight };
	}
	memset( parent, -1, sizeof( parent ) );
	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		graph[edge_count++] = ( Edge ) { u, v, w };
	}

	sort( graph, graph + edge_count );
	i64 ans;
	ans = u = 0;
	for ( int i = 0; i < edge_count; i++ )
		if ( find( graph[i].u ) != find( graph[i].v ) ) {
			ans += graph[i].w;
			join( graph[i].u, graph[i].v );
		}

	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


//============================================================================
// Name        : Codeforces.cpp
// Author      : Jos Ral Prez Rodrguez
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

typedef long long int i64;
typedef pair<i64, int> pli;
typedef pair<int, int> pii;

#define MAXN 200002
#define oo 9.223372e+18

int n,m;

vector< pair<i64, int > > graph[MAXN];
i64 data[MAXN];
i64 metric[MAXN];

map<pii, i64> mapv;

bool visited[MAXN];

void add(int u, int v, i64 w) {
	pii key = make_pair(u, v);

	i64 value = mapv[key];
	if(!value || value > w) {
		mapv[key] = w;
		mapv[make_pair(v, u)] = w;
	}
}

void solve() {

	scanf("%d %d", &n, &m);

	i64 minw = oo;
	i64 sum = 0;

	int nd;
	for(int i = 0; i < n; i++) {
		scanf("%lli", &data[i]);
		sum += data[i];
		if(data[i] < minw) {
			minw = data[i];
			nd = i;
		}
	}

	if(m == 0) {
		printf("%lli", (sum - minw) + ((i64)(n - 1) * minw));
		return;
	}

	for(int i = 0; i < n; i++) {
		if(i == nd) continue;
		i64 weight = data[i] + data[nd];
		add(i, nd, weight);
	}

	int u,v;
	i64 w;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %lli", &u, &v, &w);
		add(u-1, v-1, w);
	}

	for(map<pii, i64>::iterator it = mapv.begin(); it != mapv.end(); it++) {
		pii key = (*it).first;
		i64 value = (*it).second;

		pli edge = make_pair(value, key.second);
		graph[key.first].push_back(edge);
	}

	i64 ans;
	priority_queue< pli, vector< pli >, greater< pli > > pq;
	for(int i = 0; i < n; i++) metric[i] = oo;
	metric[0] = ans = 0;
	memset(visited, 0, sizeof(visited));
	pq.push(make_pair(0,0));
	while(!pq.empty()) {
		pair<i64, int> pr = pq.top(); pq.pop();
		if(visited[pr.second]) continue;

		ans += pr.first;
		visited[pr.second] = true;
		for(int i = 0; i < (int)graph[pr.second].size(); i++) {
			pair<i64, int> cur = graph[pr.second][i];
			if(!visited[cur.second] && metric[cur.second] > cur.first) {
				pq.push(cur);
				metric[cur.second] = cur.first;
			}
		}
	}
	printf("%lli", ans);
}

int main(int size, char** args) {
	//freopen("input","r",stdin);
	solve();
	//test();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;

}

ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    int fin=0;
    int s1=1;
    int s2=0;
    int ss=2;
    while(fin<n-1)
    {
        if((a[s1].first+a[ss].first)>edge[s2].first)
        {
            x=edge[s2].second.first;
            y=edge[s2].second.second;
            cost=edge[s2].first;
            s2++;
        }
        else
        {
            x=a[s1].second;
            y=a[ss].second;
            cost=a[s1].first+a[ss].first;
            ss++;
            if(ss==n+1)
            {
                ss=s1+2;
                s1++;
            }
        }
        if(root(x)!=root(y))
        {
            fin++;
            mincost+=cost;
            id[root(x)]=id[root(y)];
        }

    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    int x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(edge,edge+m);
    edge[m]=make_pair(1000000000000000,make_pair(0,0));
    sort(a,a+n);
    cout<<kruskal()<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;
}

void union1(int x, int y)
{
    int p = root(x);
    int q = root(y);
    id[p] = id[q];
}
ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    for(int i=0;i<m+n-1;i++)
    {
        x=edge[i].second.first;
        y=edge[i].second.second;
        cost=edge[i].first;
        if(root(x)!=root(y))
        {
            mincost+=cost;
            union1(x,y);
        }
    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    int x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i+1);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        ll co=a[i].first;
        int node=a[i].second;
        edge[m+i-1]=make_pair(co+a[0].first,make_pair(a[0].second,node));
    }
    //edge[m]=make_pair(100000000000000,make_pair(0,0));
    sort(edge,edge+m+n-1);
    if(n==1)
        cout<<"0";
    else
    cout<<kruskal()<<endl;
    /*for(int i=0;i<n+m-1;i++)
    {
        cout<<edge[i].first<<" "<<edge[i].second.first<<" "<<edge[i].second.second<<endl;

    }*/
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;
}

void union1(int x, int y)
{
    int p = root(x);
    int q = root(y);
    id[p] = id[q];
}
ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    for(int i=0;i<m+n-1;i++)
    {
        x=edge[i].second.first;
        y=edge[i].second.second;
        cost=edge[i].first;
        if(root(x)!=root(y))
        {
            mincost+=cost;
            union1(x,y);
        }
    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    ll x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i+1);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        ll co=a[i].first;
        int node=a[i].second;
        edge[m+i-1]=make_pair(co+a[0].first,make_pair(a[0].second,node));
    }
    //edge[m]=make_pair(100000000000000,make_pair(0,0));
    sort(edge,edge+m+n-1);
    if(n==1)
        cout<<"0";
    else
    cout<<kruskal()<<endl;
    /*for(int i=0;i<n+m-1;i++)
    {
        cout<<edge[i].first<<" "<<edge[i].second.first<<" "<<edge[i].second.second<<endl;

    }*/
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;
}

ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    for(int i=0;i<m+n-1;i++)
    {
        x=edge[i].second.first;
        y=edge[i].second.second;
        cost=edge[i].first;
        if(root(x)!=root(y))
        {
            mincost+=cost;
            id[root(x)]=id[root(y)];
        }
    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    int x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i+1);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        ll co=a[i].first;
        int node=a[i].second;
        edge[m+i-1]=make_pair(co+a[0].first,make_pair(node,a[0].second));
    }
    //edge[m]=make_pair(100000000000000,make_pair(0,0));
    sort(edge,edge+m+n-1);
    if(n==1)
        cout<<"0";
    else
    cout<<kruskal()<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;
}

ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    for(int i=0;i<m+n-1;i++)
    {
        x=edge[i].second.first;
        y=edge[i].second.second;
        cost=edge[i].first;
        if(root(x)!=root(y))
        {
            mincost+=cost;
            id[root(x)]=id[root(y)];
        }
    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    int x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i+1);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        ll co=a[i].first;
        int node=a[i].second;
        edge[m+i-1]=make_pair(co+a[0].first,make_pair(node,1));
    }
    //edge[m]=make_pair(100000000000000,make_pair(0,0));
    sort(edge,edge+m+n-1);
    if(n==1)
        cout<<"0";
    else
    cout<<kruskal()<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int n,m;
pair<ll,int> a[200006];
pair <ll,pair<int,int> > edge[200005];
int id[200005];

int root(int x)
{
    while(id[x]!=x)
    {
        id[x]=id[id[x]];
        x=id[x];
    }
    return x;

}

ll kruskal()
{
    int x,y;
    ll cost,mincost=0;
    int fin=0;
    int s1=1;
    int s2=0;
    int ss=2;
    while(fin<n-1)
    {
        if((a[s1].first+a[ss].first)>edge[s2].first)
        {
            x=edge[s2].second.first;
            y=edge[s2].second.second;
            cost=edge[s2].first;
            s2++;
        }
        else
        {
            x=a[s1].second;
            y=a[ss].second;
            cost=a[s1].first+a[ss].first;
            ss++;
            if(ss==n+1)
            {
                ss=s1+2;
                s1++;
            }
        }
        if(root(x)!=root(y))
        {
            fin++;
            mincost+=cost;
            id[root(x)]=id[root(y)];
        }

    }
    return mincost;

}

int main()
{
    cin>>n>>m;
    int x;
    for(int i=1;i<=n;i++)
        id[i]=i;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=make_pair(x,i);
    }
    for(int i=0;i<m;i++)
    {
        int u,v;
        ll w;
        cin>>u>>v;
        cin>>w;
        edge[i]=make_pair(w,make_pair(u,v));
    }
    sort(edge,edge+m);
    edge[m]=make_pair(1000000000000000,make_pair(0,0));
    sort(a,a+n);
    if(n==1)
        cout<<"0";
    else
    cout<<kruskal()<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <limits>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <deque>
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0u == (x & (1<<y)))?0u:1u)
#define TOGGLE(x,y) (x ^= (1<<y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORI(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define CL(a,b) memset(a,b,sizeof(a))
#define sqr(x) ((x)*(x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a,b) __gcd((a),(b))
#define dbl(x) (double)(x)
#define vi vector <int>
#define vii vector < pair <int, int> >
#define vll vector <long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin cin.sync_with_stdio(0);cin.tie(0)
#define larger(x,v) x=max((x),(v))
#define smaller(x,v) x=min((x),(v))
#define scanUnsigned(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#define printArr(a,l) cout<<#a<<": ";for(int i=0;i<(l);i++){cout<<a[i]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(int i=0;i<(r);i++){for(int j=0;j<(c);j++){cout<<a[i][j]<<" ";}cout<<endl;}
char _;
using namespace std;
void PRINT (int x, int y) { for (int i = y-1; i >= 0; i--) { cout << READ(x,i); } cout << endl; }

int dsu[2*100005] = {0};
bool v[2*100005] = {0};
ll a[2*100005] = {0}, ans = 0, cnt = 0, l[2*100005] = {0};
vector < vi > graph(100005*2);

int find (int pos) {
	if (dsu[pos] != pos) {
		dsu[pos] = find (dsu[pos]);
	} return dsu[pos];
}

void merge (int a, int b) {
	if (a < b) {
		l[find(a)] = min (l[find(a)], l[find(b)]);
		dsu[find(b)] = find (a);
	} else {
		l[find(b)] = min (l[find(a)], l[find(b)]);
		dsu[find(a)] = find (b);
	}
}

void dfs (int pos) {
	v[pos] = true;
	l[cnt] = min (l[cnt], a[pos]);
	REP (x, (int)graph[pos].size()) {
		if (!v[graph[pos][x]]) {
			dfs (graph[pos][x]);
		}
	}
}

int main () {
	REP (x,2*100005) {
		dsu[x] = x;
	}
	fastCin;
	int n, m, A, B;
	ll c;
	vector < pair <ll, pii > > e;
	cin >> n >> m;
	FORI (x,1,n) {
		cin >> a[x];
		l[x] = a[x];
	}
	REP (x, m) {
		cin >> A >> B >> c;
		if (c < a[A]+a[B]) {
			e.pb (mp (c, mp (A, B)));
		}
	}
	sort (allof (e));
	REP (x, (int)e.size()) {
		A = e[x].pS.pF; B = e[x].pS.pS;
		c = e[x].pF;
		if (find (A) != find(B) && c < l[find(A)]+l[find(B)]) {
			merge (A, B);
			ans += c;
			graph[A].pb (B);
			graph[B].pb (A);
			//cout << A << " " << B << " " << c << "\n";
		}
	}
	CL (l, 0);
	ll gLeast = longLIMIT::max(), best;
	FORI (x,1,n) {
		if (!v[x]) {
			cnt++;
			l[cnt] = longLIMIT::max();
			dfs (x);
			//cout << cnt << " " << l[cnt] << "!\n";
			if (l[cnt] < gLeast) {
				gLeast = l[cnt];
				best = cnt;
			}
		}
	}
	FORI (x,1,cnt) {
		if(x==best) {
			continue;
		} else {
			ans += l[x]+gLeast;
		}
	}
	cout << ans << "\n";
}


#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <limits>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <deque>
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0u == (x & (1<<y)))?0u:1u)
#define TOGGLE(x,y) (x ^= (1<<y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORI(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define CL(a,b) memset(a,b,sizeof(a))
#define sqr(x) ((x)*(x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a,b) __gcd((a),(b))
#define dbl(x) (double)(x)
#define vi vector <int>
#define vii vector < pair <int, int> >
#define vll vector <long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin cin.sync_with_stdio(0);cin.tie(0)
#define larger(x,v) x=max((x),(v))
#define smaller(x,v) x=min((x),(v))
#define scanUnsigned(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#define printArr(a,l) cout<<#a<<": ";for(int i=0;i<(l);i++){cout<<a[i]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(int i=0;i<(r);i++){for(int j=0;j<(c);j++){cout<<a[i][j]<<" ";}cout<<endl;}
char _;
using namespace std;
void PRINT (int x, int y) { for (int i = y-1; i >= 0; i--) { cout << READ(x,i); } cout << endl; }

ll dsu[2*100005] = {0};
bool v[2*100005] = {0};
ll a[2*100005] = {0}, ans = 0, cnt = 0, l[2*100005] = {0};
vector < vll > graph(100005*2);

ll find (int pos) {
	if (dsu[pos] != pos) {
		int par = find (dsu[pos]);
		l[par] = min (l[par], l[pos]);
		dsu[pos] = par;
	} return dsu[pos];
}

void merge (int a, int b) {
	if (a < b) {
		l[find(a)] = min (l[find(a)], l[find(b)]);
		dsu[find(b)] = find (a);
	} else {
		l[find(b)] = min (l[find(a)], l[find(b)]);
		dsu[find(a)] = find (b);
	}
}

void dfs (int pos) {
	v[pos] = true;
	l[cnt] = min (l[cnt], a[pos]);
	REP (x, (int)graph[pos].size()) {
		if (!v[graph[pos][x]]) {
			dfs (graph[pos][x]);
		}
	}
}

int main () {
	REP (x,2*100005) {
		dsu[x] = x;
	}
	fastCin;
	ll n, m, A, B;
	ll c;
	vector < pair <ll, pll > > e;
	cin >> n >> m;
	FORI (x,1,n) {
		cin >> a[x];
		l[x] = a[x];
	}
	REP (x, m) {
		cin >> A >> B >> c;
		if (c < a[A]+a[B]) {
			e.pb (mp (c, mp (A, B)));
		}
	}
	sort (allof (e));
	REP (x, (int)e.size()) {
		A = e[x].pS.pF; B = e[x].pS.pS;
		c = e[x].pF;
		if (find (A) != find(B) && c < l[find(A)]+l[find(B)]) {
			merge (A, B);
			ans += c;
			graph[A].pb (B);
			graph[B].pb (A);
			//cout << A << " " << B << " " << c << "\n";
		}
	}
	ll gLeast = longLIMIT::max(), best;
	FORI (x,1,n) {
		if (!v[x]) {
			cnt++;
			l[cnt] = longLIMIT::max();
			dfs (x);
			//cout << cnt << " " << l[cnt] << "!\n";
			if (l[cnt] < gLeast) {
				gLeast = l[cnt];
				best = cnt;
			}
		}
	}
	FORI (x,1,cnt) {
		if(x==best) {
			continue;
		} else {
			ans += l[x]+gLeast;
		}
	}
	cout << ans << "\n";
}


#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <limits>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <deque>
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0u == (x & (1<<y)))?0u:1u)
#define TOGGLE(x,y) (x ^= (1<<y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORI(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define CL(a,b) memset(a,b,sizeof(a))
#define sqr(x) ((x)*(x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a,b) __gcd((a),(b))
#define dbl(x) (double)(x)
#define vi vector <int>
#define vii vector < pair <int, int> >
#define vll vector <long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin cin.sync_with_stdio(0);cin.tie(0)
#define larger(x,v) x=max((x),(v))
#define smaller(x,v) x=min((x),(v))
#define scanUnsigned(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#define printArr(a,l) cout<<#a<<": ";for(int i=0;i<(l);i++){cout<<a[i]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(int i=0;i<(r);i++){for(int j=0;j<(c);j++){cout<<a[i][j]<<" ";}cout<<endl;}
char _;
using namespace std;
void PRINT (int x, int y) { for (int i = y-1; i >= 0; i--) { cout << READ(x,i); } cout << endl; }

ll dsu[200005] = {0};
bool v[200005] = {0};
ll a[200005] = {0}, ans = 0, cnt = 0, l[200005] = {0};
vector < vll > graph(200005);

ll find (int pos) {
	if (dsu[pos] != pos) {
		int par = find (dsu[pos]);
		l[par] = min (l[par], l[pos]);
		dsu[pos] = par;
	} return dsu[pos];
}

void merge (int a, int b) {
	if (a < b) {
		l[find(a)] = min (l[find(a)], l[find(b)]);
		dsu[find(b)] = find (a);
	} else {
		l[find(b)] = min (l[find(a)], l[find(b)]);
		dsu[find(a)] = find (b);
	}
}

void dfs (int pos) {
	v[pos] = true;
	if (l[cnt] == -1) {
		l[cnt] = a[pos];
	}
	l[cnt] = min (l[cnt], a[pos]);
	REP (x, (int)graph[pos].size()) {
		if (!v[graph[pos][x]]) {
			dfs (graph[pos][x]);
		}
	}
}

int main () {
	REP (x,200005) {
		dsu[x] = x;
	}
	fastCin;
	ll n, m, A, B;
	ll c, gLeast = longLIMIT::max();
	vector < pair <ll, pll > > e;
	cin >> n >> m;
	FORI (x,1,n) {
		cin >> a[x];
		l[x] = a[x];
		gLeast = min (l[x], gLeast);
	}
	REP (x, m) {
		cin >> A >> B >> c;
		if (c < a[A]+a[B]) {
			e.pb (mp (c, mp (A, B)));
		}
	}
	sort (allof (e));
	REP (x, (int)e.size()) {
		A = e[x].pS.pF; B = e[x].pS.pS;
		c = e[x].pF;
		if (find (A) != find(B) && c+gLeast+min (l[find(A)],l[find(B)]) < 2*gLeast + l[find(A)]+l[find(B)]) {
			merge (A, B);
			ans += c;
			graph[A].pb (B);
			graph[B].pb (A);
			//cout << A << " " << B << " " << c << "\n";
		}
	}
	gLeast = -1;
	ll best;
	FORI (x,1,n) {
		if (!v[x]) {
			cnt++;
			l[cnt] = -1;
			dfs (x);
			//cout << cnt << " " << l[cnt] << "!\n";
			if (gLeast == -1 || l[cnt] < gLeast) {
				gLeast = l[cnt];
				best = cnt;
			}
		}
	}
	FORI (x,1,cnt) {
		if(x==best) {
			continue;
		} else {
			ans += l[x]+gLeast;
		}
	}
	cout << ans << "\n";
}


#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <limits>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <deque>
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0u == (x & (1<<y)))?0u:1u)
#define TOGGLE(x,y) (x ^= (1<<y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pF first
#define pS second
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORI(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define CL(a,b) memset(a,b,sizeof(a))
#define sqr(x) ((x)*(x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define GCD(a,b) __gcd((a),(b))
#define dbl(x) (double)(x)
#define vi vector <int>
#define vii vector < pair <int, int> >
#define vll vector <long long>
#define allof(x) (x).begin(), (x).end()
#define fastCin cin.sync_with_stdio(0);cin.tie(0)
#define larger(x,v) x=max((x),(v))
#define smaller(x,v) x=min((x),(v))
#define scanUnsigned(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#define printArr(a,l) cout<<#a<<": ";for(int i=0;i<(l);i++){cout<<a[i]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(int i=0;i<(r);i++){for(int j=0;j<(c);j++){cout<<a[i][j]<<" ";}cout<<endl;}
char _;
using namespace std;
void PRINT (int x, int y) { for (int i = y-1; i >= 0; i--) { cout << READ(x,i); } cout << endl; }

ll dsu[2*100005] = {0};
bool v[2*100005] = {0};
ll a[2*100005] = {0}, ans = 0, cnt = 0, l[2*100005] = {0};
vector < vll > graph(100005*2);

ll find (int pos) {
	if (dsu[pos] != pos) {
		int par = find (dsu[pos]);
		l[par] = min (l[par], l[pos]);
		dsu[pos] = par;
	} return dsu[pos];
}

void merge (int a, int b) {
	if (a < b) {
		l[find(a)] = min (l[find(a)], l[find(b)]);
		dsu[find(b)] = find (a);
	} else {
		l[find(b)] = min (l[find(a)], l[find(b)]);
		dsu[find(a)] = find (b);
	}
}

void dfs (int pos) {
	v[pos] = true;
	l[cnt] = min (l[cnt], a[pos]);
	REP (x, (int)graph[pos].size()) {
		if (!v[graph[pos][x]]) {
			dfs (graph[pos][x]);
		}
	}
}

int main () {
	REP (x,2*100005) {
		dsu[x] = x;
	}
	fastCin;
	ll n, m, A, B;
	ll c;
	vector < pair <ll, pll > > e;
	cin >> n >> m;
	FORI (x,1,n) {
		cin >> a[x];
		l[x] = a[x];
	}
	REP (x, m) {
		cin >> A >> B >> c;
		//if (c < a[A]+a[B]) {
			e.pb (mp (c, mp (A, B)));
		//}
	}
	sort (allof (e));
	REP (x, (int)e.size()) {
		A = e[x].pS.pF; B = e[x].pS.pS;
		c = e[x].pF;
		if (find (A) != find(B) && c < l[find(A)]+l[find(B)]) {
			merge (A, B);
			ans += c;
			graph[A].pb (B);
			graph[B].pb (A);
			//cout << A << " " << B << " " << c << "\n";
		}
	}
	ll gLeast = longLIMIT::max(), best;
	FORI (x,1,n) {
		if (!v[x]) {
			cnt++;
			l[cnt] = longLIMIT::max();
			dfs (x);
			//cout << cnt << " " << l[cnt] << "!\n";
			if (l[cnt] < gLeast) {
				gLeast = l[cnt];
				best = cnt;
			}
		}
	}
	FORI (x,1,cnt) {
		if(x==best) {
			continue;
		} else {
			ans += l[x]+gLeast;
		}
	}
	cout << ans << "\n";
}