#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) {
		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;
	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[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;
	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;
	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;
	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 = -1, 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 <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define ll long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N];
ll w[N];
int par[N];
int sz[N];
pair<ll,int > p[N];
vector<pair<ll,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] > sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%d%d%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size()){
		int w1 = edge[cnt].fr, x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N], w[N], par[N], sz[N];
pair<int,int > p[N];
deque<pair<int,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] < sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%lld%lld%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 int mn = INF * 100000;
	 int ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size() && k < n - 1){
		int w1 = edge[cnt].fr, x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
			k++;
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define ll long long

using namespace std;

const int N = (3e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N];
ll w[N];
int par[N];
int sz[N];
pair<ll,int > p[N];
vector<pair<ll,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] > sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%d%d%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {p[1].sc, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size()){
		ll w1 = edge[cnt].fr;
		int x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N], w[N], par[N], sz[N];
pair<int,int > p[N];
vector<pair<int,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] < sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%d%d%d", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size() && k < n - 1){
		int w1 = edge[cnt].fr, x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
			k++;
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N], w[N], par[N], sz[N];
pair<int,int > p[N];
deque<pair<int,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] < sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%lld%lld%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 int mn = INF * 100000;

	 int ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0;
	 while(!edge.empty() && cnt < n - 1){
		int w1 = edge.front().fr, x1 = edge.front().sc.fr, y1 = edge.front().sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
			cnt++;
		}
		edge.pop_front();
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define ll long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N];
ll w[N];
int par[N];
int sz[N];
pair<ll,int > p[N];
vector<pair<ll,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] > sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%d%d%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {p[1].sc, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size()){
		ll w1 = edge[cnt].fr;
		int x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
		}
		cnt++;
	 }
		for (int i = 2; i <= n; i++) {
			assert(find(i) == find(1));
		}
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (3e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N], w[N], par[N], sz[N];
pair<int,int > p[N];
deque<pair<int,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] > sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%lld%lld%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 int mn = INF * 100000;

	 int ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0;
	 while(!edge.empty() && cnt < n - 1){
		int w1 = edge.front().fr, x1 = edge.front().sc.fr, y1 = edge.front().sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
			cnt++;
		}
		edge.pop_front();
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N], w[N], par[N], sz[N];
pair<int,int > p[N];
vector<pair<int,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] < sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%lld%lld%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size() && k < n - 1){
		int w1 = edge[cnt].fr, x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
			k++;
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define ll long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

int x[N], y[N];
ll w[N];
int par[N];
int sz[N];
pair<ll,int > p[N];
vector<pair<ll,pair<int,int > > > edge;

int find(int v){
	if(v == par[v]){
		return v;
	}
	return par[v] = find(par[v]);
}

void dsu(int a,int b){
	a = find(a);
	b = find(b);
	if(sz[b] > sz[a]){
		swap(a, b);
	}
	sz[a] += sz[b];
	par[b] = a;
}
main()
{
	 int n, m;
	 cin >> n >> m;
	 for(int i = 1;i <= n; i++){
		scanf("%lld", &p[i].fr);
		p[i].sc = i;
		par[i] = i;
		sz[i] = 1;
	 }
	 for(int i = 1;i <= m; i++){
		scanf("%d%d%lld", &x[i], &y[i], &w[i]);
		edge.pb({w[i],{x[i], y[i]}});
	 }
	 long long ans = 0, x;
	 sort(p + 1,p + 1 + n);
	 x = p[1].sc;
	 for(int i = 2;i <= n; i++){
		edge.pb({p[i].fr + p[1].fr, {x, p[i].sc}});
	 }
	 sort(all(edge));
	 int cnt = 0, k = 0;
	 while(cnt < edge.size()){
		ll w1 = edge[cnt].fr;
		int x1 = edge[cnt].sc.fr, y1 = edge[cnt].sc.sc;
		if(find(x1) != find(y1)){
			ans += w1;
			dsu(x1,y1);
		}
		cnt++;
	 }
	 cout << ans;
}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

Long res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, int c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += Long(c);

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vi val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<int, pii>>aris(M);

	forn(i, 0, M){
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;
		aris[i].se.fi --;
		aris[i].se.se --;
	}

	int primero;
	int menor = numeric_limits<int>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	for(int i = 0; i < N; i ++){
		if( i == primero ) continue;
		aris.push_back(make_pair(menor + val[i], pii(primero, i)));
	}

	sort(all(aris));

	for(auto t : aris){
		int u = t.se.fi;
		int v = t.se.se;
		int c = t.fi;

		Une(u, v, c);

	}

	cout << res << '\n';

	return 0;

}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

int res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, int c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += c;

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vi val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<int, pii>>aris(M);

	int primero;
	int menor = numeric_limits<int>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	vi esp(N);
	forn(i, 0, N)
		if(val[i] == menor)
			esp[i] = 1;

	forn(i, 0, M)
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;
	sort(all(aris));

	forn(i, 0, M){
		int u = aris[i].se.fi;
		int v = aris[i].se.se;
		int c = aris[i].fi;
		if(c < 2*menor)
			Une(u, v, c);
	}

	forn(i, 0, N){
		if(esp[i] && i != primero)
			Une(primero, i, menor*2);
	}

	forn(i, 0, M){
		int u = aris[i].se.fi;
		int v = aris[i].se.se;
		int c = aris[i].fi;

		if(esp[u] + esp[v] == 2) continue;

		if(esp[u]){
			if(c < val[v] + menor)
				Une(u, v, c);
		} else if(esp[v]){
			if(c < val[u] + menor)
				Une(u, v, c);
		} else {
			if(c < val[u] + menor && c < val[v] + menor)
				Une(u, v, c);
		}

	}

	forn(i, 0, N){
		if(esp[i]) continue;
		Une(primero, i, menor + val[i]);
	}

	cout << res << '\n';

	return 0;

}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

int res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, int c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += c;

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vi val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<int, pii>>aris(M);

	forn(i, 0, M){
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;
		aris[i].se.fi --;
		aris[i].se.se --;
	}

	int primero;
	int menor = numeric_limits<int>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	for(int i = 0; i < N; i ++){
		if( i == primero ) continue;
		aris.push_back(make_pair(menor + val[i], pii(primero, i)));
	}

	sort(all(aris));

	for(auto t : aris){
		int u = t.se.fi;
		int v = t.se.se;
		int c = t.fi;

		Une(u, v, c);

	}

	cout << res << '\n';

	return 0;

}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

int res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, int c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += c;

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vi val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<int, pii>>aris(M);

	forn(i, 0, M)
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;

	int primero;
	int menor = numeric_limits<int>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	for(int i = 0; i < N; i ++){
		if( i == primero ) continue;
		aris.push_back(make_pair(menor + val[i], pii(primero, i)));
	}

	sort(all(aris));

	for(auto t : aris){
		int u = t.se.fi;
		int v = t.se.se;
		int c = t.fi;

		Une(u, v, c);

	}

	cout << res << '\n';

	return 0;

}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

Long res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, Long c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += c;

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vector<Long> val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<Long, pii>>aris(M);

	forn(i, 0, M){
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;
		aris[i].se.fi --;
		aris[i].se.se --;
	}

	int primero;
	Long menor = numeric_limits<Long>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	for(int i = 0; i < N; i ++){
		if( i == primero ) continue;
		aris.push_back(make_pair(menor + val[i], pii(primero, i)));
	}

	sort(all(aris));

	for(auto t : aris){
		int u = t.se.fi;
		int v = t.se.se;
		Long c = t.fi;

		Une(u, v, c);

	}

	cout << res << '\n';

	return 0;

}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mod(n, k) ( ( ((n) %(k)) + (k))%(k))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define forr(i, a, b) for(int i = a; i >= b; i --)
#define all(x) (x).begin(), (x).end()

typedef long long int Long;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const bool DEGUB = true;

int res;
vi pad, alt;

int Padre(int u){
	return pad[u] == u ? u : pad[u] = Padre(pad[u]);
}

int Une(int u, int v, int c){
	u = Padre(u);
	v = Padre(v);

	if(u != v){

		res += c;

		if(alt[u] > alt[v]){
			pad[v] = u;
		} else {
			pad[u] = v;
			if(alt[u] == alt[v])
				alt[v] ++;
		}
	}

}

int main(){

	int N;
	int M;

	optimizar_io(0);

	cin >> N >> M;

	vi val(N);
	pad = vi(N);
	alt = vi(N);

	forn(i, 0, N){
		cin >> val[i];
		pad[i] = i;
	}
	vector<pair<int, pii>>aris(M);

	int primero;
	int menor = numeric_limits<int>::max();
	forn(i, 0, N){
		if(val[i] < menor){
			menor = val[i];
			primero = i;
		}
	}

	vi esp(N);
	forn(i, 0, N)
		if(val[i] == menor)
			esp[i] = 1;

	forn(i, 0, M)
		cin >> aris[i].se.fi >> aris[i].se.se >> aris[i].fi;
	sort(all(aris));

	forn(i, 0, M){
		int u = aris[i].se.fi;
		int v = aris[i].se.se;
		int c = aris[i].fi;

		if(esp[u] && esp[v] && c < 2*menor)
			Une(u, v, c);

	}

	forn(i, 0, N){
		if(esp[i] && i != primero)
			Une(primero, i, menor*2);
	}

	forn(i, 0, M){
		int u = aris[i].se.fi;
		int v = aris[i].se.se;
		int c = aris[i].fi;

		if(esp[u] + esp[v] == 2) continue;

		if(esp[u]){
			if(c < val[v] + menor)
				Une(u, v, c);
		} else if(esp[v]){
			if(c < val[u] + menor)
				Une(u, v, c);
		} else {
			if(c < val[u] + menor && c < val[v] + menor)
				Une(u, v, c);
		}

	}

	forn(i, 0, N){
		if(esp[i]) continue;
		Une(primero, i, menor + val[i]);
	}

	cout << res << '\n';

	return 0;

}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    while (sz(best) >= 2) {
        if (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(best.begin());
            best.erase(best.begin());
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                ans += p.fi;
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(id1)], get(id1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    while (sz(best) >= 2) {
        if (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(best.begin());
            best.erase(best.begin());
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                ans += p.fi;
                assert(best.find({v1, id1}) != best.end());
                assert(best.find({v2, id2}) != best.end());
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(id1)], get(id1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    while (sz(best) >= 2) {
        if (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(best.begin());
            best.erase(best.begin());
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                ans += p.fi;
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(id1)], get(id1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];

void build(int n) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
    } else {
        p[x] = y;
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    set<pair<ll, int> > best;
    int pos[n];
    rep(i, 0, n) {
        pos[i] = 0;
        if (a[pos[i]].se == i) {
            pos[i]++;
        }
        if (pos[i] < n) {
            best.insert({val[i] + a[pos[i]].fi, i});
        }
    }
    build(n);
    ll ans = 0;
    int cnt = n - 1;
    while (cnt > 0) {
        if (sz(best) > 0 && (sz(s) == 0 || (*best.begin()).fi < (*s.begin()).fi)) {
            auto p = *best.begin();
            best.erase(best.begin());
            if (uni(p.se, a[pos[p.se]].se)) {
                //cout << p.se << " " << a[pos[p.se]].se << " " << p.fi << endl;
                cnt--;
                ans += p.fi;
            }
            pos[p.se]++;
            while (pos[p.se] < n && a[pos[p.se]].se == p.se) {
                pos[p.se]++;
            }
            if (pos[p.se] < n) {
                best.insert({val[p.se] + a[pos[p.se]].fi, p.se});
            }
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            if (uni(p.se.fi, p.se.se)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                cnt--;
                ans += p.fi;
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    while (sz(best) >= 2) {
        if (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(p);
            best.erase(p1);
            uni(p.se, p1.se);
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                ans += p.fi;
                assert(best.find({v1, id1}) != best.end());
                assert(best.find({v2, id2}) != best.end());
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(id1)], get(id1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    while (sz(best) >= 2) {
        if (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(best.begin(), ++++best.begin());
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                ans += p.fi;
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(id1)], get(id1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <assert.h>
#include <memory.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];
ll min_v[N];

void build(int n, ll *a) {
    rep(i, 0, n) {
        p[i] = i;
        r[i] = 0;
        min_v[i] = a[i];
    }
}

int get(int x) {
    if (x != p[x]) {
        p[x] = get(p[x]);
    }
    return p[x];
}

bool uni(int x, int y) {
    x = get(x); y = get(y);
    if (x == y) {
        return false;
    }
    if (r[x] >= r[y]) {
        p[y] = x;
        setmin(min_v[x], min_v[y]);
    } else {
        p[x] = y;
        setmin(min_v[y], min_v[x]);
    }
    if (r[x] == r[y]) {
        r[x]++;
    }
    return true;
}

ll val[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("dsu.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    pair<ll, int> a[n];
    rep(i, 0, n) {
        cin >> a[i].fi;
        a[i].se = i;
        val[i] = a[i].fi;
    }
    sort(a, a + n);
    multiset<pair<ll, pair<int, int> > > s;
    rep(i, 0, m) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        s.insert({w, {u, v}});
    }
    build(n, val);
    set<pair<ll, int> > best;
    rep(i, 0, n) {
        best.insert({val[i], i});
    }
    ll ans = 0;
    int cnt = n - 1;
    while (cnt > 0) {
        if (sz(best) >= 2 && (sz(s) == 0 || (*best.begin()).fi + (*++best.begin()).fi < (*s.begin()).fi)) {
            auto p = *best.begin();
            auto p1 = *++best.begin();
            best.erase(best.begin());
            best.erase(best.begin());
            cnt--;
            ans += p.fi + p1.fi;
            best.insert({min_v[get(p.se)], get(p.se)});
        } else {
            auto p = *s.begin();
            s.erase(s.begin());
            int id1 = get(p.se.fi), id2 = get(p.se.se);
            ll v1 = min_v[id1], v2 = min_v[id2];
            if (uni(id1, id2)) {
                //cout << p.se.fi << " " << p.se.se << " " << p.fi << endl;
                cnt--;
                ans += p.fi;
                best.erase({v1, id1});
                best.erase({v2, id2});
                best.insert({min_v[get(v1)], get(v1)});
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 1e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	set<pair<ll, int>> elems;
	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	p1 = {-1, -1};
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		if (p1.x == -1)
			while (len (elems) > 1) {
				p1 = (*elems.begin());
				elems.erase (elems.begin());
				p2 = (*elems.begin());
				elems.erase (elems.begin());
				if (same (p1.y, p2.y))
					elems.insert ({smaller[find (p1.y)], find (p1.y)});
				else
					break;
			}
 		if (len (elems) == 1 and (p1.x == -1 or same (p1.y, p2.y)))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (!same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;
		}

		if (same (p1.y, p2.y)) {
			elems.insert ({smaller[find (p1.y)], find (p1.y)});
			p1 = {-1, -1};
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


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

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
using llu = long long unsigned;
using ld = long double;

const ld EPS = 1e-9;
inline int cmp(ld x, ld y = 0, ld tol = EPS) {
	return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

const int MOD = 1;
inline int mod (ll x, int m = MOD) {
	return (int)(((x%m) + m)%m);
}

////////////////////////// Solution starts below. //////////////////////////////

using pp = pair<ll, int>;

const int N = 2e5+10;

int n, m;
int link[N], size[N], idxMn;
ll weight[N], ans, mn;
set<pp> st;
vector<pp> adj[N];
bool vis[N];

int main () {
	scanf ("%d %d", &n, &m);
	mn = 1e18;
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &weight[i]);
		if (weight[i] < mn) {
			idxMn = i;
			mn = weight[i];
		}
	}
	while (m--) {
		int x, y;
		ll w;
		scanf ("%d %d %lld", &x, &y, &w);
		adj[x].push_back ({w, y});
		adj[y].push_back ({w, x});
	}
	for (int i = 1; i <= n; i++)
		st.insert ({weight[i] + mn, i});

	st.insert ({0ll, idxMn});
	while (!st.empty()) {
		ll w = st.begin()->x;
		int u = st.begin()->y;
		st.erase (st.begin());
		if (vis[u]) continue;
		vis[u] = true;
		ans += w;
		for (pp p : adj[u])
			if (!vis[p.y])
				st.insert ({p.x, p.y});
	}

	printf ("%lld\n", ans);

	return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 1e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
set<pair<ll, int>> elems;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	p1 = {-1, -1};
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		while (len (elems) > 1) {
			p1 = (*elems.begin());
			elems.erase (elems.begin());
			p2 = (*elems.begin());
			elems.erase (elems.begin());
			if (same (p1.y, p2.y))
				elems.insert ({smaller[find (p1.y)], find (p1.y)});
			else
				break;
		}
 		if (len (elems) == 1 and (p1.x == -1 or same (p1.y, p2.y)))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (p1.x != -1 and !same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;
			elems.insert ({smaller[find (p1.y)], find (p1.y)});

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;

			elems.insert (p1);
			elems.insert (p2);
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 2e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	set<pair<ll, int>> elems;
	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	p1 = {-1, -1};
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		if (p1.x == -1)
			while (len (elems) > 1) {
				p1 = (*elems.begin());
				elems.erase (elems.begin());
				p2 = (*elems.begin());
				elems.erase (elems.begin());
				if (same (p1.y, p2.y))
					elems.insert ({smaller[find (p1.y)], find (p1.y)});
				else
					break;
			}
 		if (len (elems) == 1 and (p1.x == -1 or same (p1.y, p2.y)))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (!same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;
		}

		if (same (p1.y, p2.y)) {
			elems.insert ({smaller[find (p1.y)], find (p1.y)});
			p1 = {-1, -1};
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 2e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
set<pair<ll, int>> elems;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		while (len (elems) > 1) {
			p1 = (*elems.begin());
			elems.erase (elems.begin());
			p2 = (*elems.begin());
			elems.erase (elems.begin());
			p1.y = find (p1.y);
			p2.y = find (p2.y);
			if (same (p1.y, p2.y))
				elems.insert ({smaller[find (p1.y)], find (p1.y)});
			else
				break;
		}
 		if (len (elems) >= 0 and same (p1.y, p2.y))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (!same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;
			elems.insert ({smaller[find (p1.y)], find (p1.y)});

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;

			elems.insert (p1);
			elems.insert (p2);
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


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

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
using llu = long long unsigned;
using ld = long double;

const ld EPS = 1e-9;
inline int cmp(ld x, ld y = 0, ld tol = EPS) {
	return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

const int MOD = 1;
inline int mod (ll x, int m = MOD) {
	return (int)(((x%m) + m)%m);
}

////////////////////////// Solution starts below. //////////////////////////////

const int N = 1e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
ll weight[N];

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	set<pair<ll, int>> elems;
	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		pair<ll, int> p1, p2;
		while (true) {
			p1 = (*elems.begin());
			elems.erase (elems.begin());
			p2 = (*elems.begin());
			elems.erase (elems.begin());
			if (same (p1.y, p2.y))
				elems.insert ({smaller[find (p1.y)], find (p1.y)});
			else
				break;
		}

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (u == -1 or same (u, v) or (p1.x+p2.x) < w) {
			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});

			u = p1.y;
			v = p2.y;
			w = p1.x + p2.x;
// 			printf ("|%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;
			elems.insert ({smaller[find (u)], find (u)});
		} else {
// 			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;

			elems.insert (p1);
			elems.insert (p2);
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 2e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
set<pair<ll, int>> elems;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], smaller[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		while (len (elems) > 1) {
			p1 = (*elems.begin());
			elems.erase (elems.begin());
			p2 = (*elems.begin());
			elems.erase (elems.begin());
			p1.y = find (p1.y);
			p2.y = find (p2.y);
			if (same (p1.y, p2.y))
				elems.insert ({smaller[find (p1.y)], find (p1.y)});
			else
				break;
		}
 		if (len (elems) >= 0 and same (p1.y, p2.y))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (!same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;
			elems.insert ({smaller[find (p1.y)], find (p1.y)});

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;

			elems.insert (p1);
			elems.insert (p2);
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


#include "stdio.h"
#include "utility"
#include "set"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define len(x) ((int)(x).size())
using pii = pair<int, int>;
using ll = long long;
////////////////////////// Solution starts below. //////////////////////////////

const int N = 1e5+10;

int n, m;
int link[N], size[N];
ll smaller[N];
set<pair<ll, pii>> st;
ll weight[N];
pair<ll, int> p1, p2;

int find (int a) {
	if (link[a] == a) return a;
	return link[a] = find (link[a]);
}

bool same (int a, int b) {
	return find (a) == find (b);
}

void unite (int a, int b) {
	a = find (a);
	b = find (b);
	if (size[a] > size[b]) swap (a, b);
	size[b] += size[a];
	link[a] = b;
	smaller[b] = min (smaller[b], weight[a]);
}

int main () {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf ("%lld", &weight[i]);

	for (int i = 1; i <= n; i++)
		size[i] = 1, link[i] = i, smaller[i] = weight[i];

	for (int i = 0; i < m; i++) {
		int u, v;
		ll w;
		scanf ("%d %d %lld", &u, &v, &w);
		if (w < weight[u] + weight[v])
			st.insert ({w, {u, v}});
	}

	set<pair<ll, int>> elems;
	for (int i = 1; i <= n; i++)
		if (link[i] == i)
			elems.insert ({smaller[i], i});
// 	while (len (elems) > 1) {
//
// 		ans += p1 + p2;
// 		elems.insert (min (p1, p2));
// 	}

	ll ans = 0;
	p1 = {-1, -1};
	while (len (elems) > 1) {
// 		printf ("%d\n", len (elems));
		if (p1.x == -1)
			while (len (elems) > 1) {
				p1 = (*elems.begin());
				elems.erase (elems.begin());
				p2 = (*elems.begin());
				elems.erase (elems.begin());
				if (same (p1.y, p2.y))
					elems.insert ({smaller[find (p1.y)], find (p1.y)});
				else
					break;
			}
 		if (len (elems) == 1 and (p1.x == -1 or same (p1.y, p2.y)))
 			break;

		ll w;
		int u, v;
		u = v = -1;
		while (!st.empty()) {
			w = st.begin()->x;
			u = st.begin()->y.x, v = st.begin()->y.y;
			st.erase (st.begin());
			if (!same (u, v))
				break;
		}
		if (p1.x != -1 and !same (p1.y, p2.y) and (u == -1 or same (u, v) or (p1.x+p2.x) < w)) {
//  			printf ("|%d %d %lld\n", p1.y, p2.y, p1.x + p2.x);
			unite (p1.y, p2.y);
			ans += p1.x + p2.x;

			if (u != -1 and !same (u, v))
				st.insert ({w, {u, v}});
		} else if (u != -1 and !same (u, v)) {
//  			printf ("%d %d %lld\n", u, v, w);
			unite (u, v);
			ans += w;
		}

		if (p1.x != -1 and same (p1.y, p2.y)) {
			elems.insert ({smaller[find (p1.y)], find (p1.y)});
			p1 = {-1, -1};
		}

	}
	printf ("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};

    ll mpv = INT_MAX;
    if(mp.find({x,y}) != mp.end()){
        mpv = mp[{x,y}];
    }

    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y >>w;
        if(mp.find({x,y}) != mp.end()){
            mp[{x,y}] = min(w, mp[{x,y}]);
            mp[{y,x}] = min(w,mp[{x,y}]);

        }else{
            mp[{x,y}] = w;
            mp[{y,x}] = w;
        }

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;
#define Darr(arr) for(ll i=0; i<arr.size(); i++) cerr << "(" << i << ", " << arr[i] << "),  "; cerr<< endl;

using namespace std;

ll leader(ll x, vector<ll> &p){

    return (p[x] == x ? x : (p[x] = leader(p[x], p)));

}

bool merge(ll x, ll y, vector<ll> &p){

    ll p1 = leader(x, p);
    ll p2 = leader(y, p);
    if(p1 == p2)
        return false;
    else{
        p[p1] = p2;
        return true;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    ll arr[n];
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

    sort(ed.begin(), ed.end());

    vector<ll> p(n);
    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
    Darr(p);
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z,p)){
            ans += x;
//            D3e(y,z,x);
        }
        Darr(p);

    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<ll,ll>, ll> mp;

ll val(ll x, ll y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};

    ll mpv = INT_MAX;
    if(mp.find({x,y}) != mp.end()){
        mpv = mp[{x,y}];
    }

    return min((arr[x]+arr[y]) , mpv);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;

        mp[{x,y}] = w;
        mp[{y,x}] = w;

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        ll k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(ll j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;
#define Darr(arr) for(ll i=0; i<arr.size(); i++) cerr << "(" << i << ", " << arr[i] << "),  "; cerr<< endl;

using namespace std;

ll leader(ll x, vector<ll> &p){

    return (p[x] == x ? x : (p[x] = leader(p[x], p)));

}

bool merge(ll x, ll y, vector<ll> &p){

    ll p1 = leader(x, p);
    ll p2 = leader(y, p);
    if(p1 == p2)
        return false;
    else{
        p[p1] = p2;
        return true;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    ll arr[n];
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

    sort(ed.begin(), ed.end());

    vector<ll> p(n);
    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
    Darr(p);
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z,p)){
            ans += x;
//            D3e(y,z,x);
        }
        Darr(p);

    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};

    ll mpv = INT_MAX;
    if(mp.find({x,y}) != mp.end()){
        mpv = mp[{x,y}];
    }

    return min((arr[x]+arr[y]) , mpv);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y >>w;
        if(mp.find({x,y}) != mp.end()){
            mp[{x,y}] = min(w, mp[{x,y}]);
            mp[{y,x}] = min(w,mp[{x,y}]);

        }

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n);
    ll i,j;
    fr(i,n){
        cin >> arr[i];
    }
    vector<vector<ll> > adj(n, vector<ll> (n) );
    fr(i,n){
        fr(j,n){
            if(i == j)
                continue;
            adj[i][j] = (arr[i]+arr[j]);
        }
    }

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;
        adj[x-1][y-1]  = min(adj[x-1][y-1], w);
        adj[y-1][x-1]  = min(adj[y-1][x-1], w);
    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n);

    vector<ll> key(n, INT_MAX);

//    visited[0] = 1;
    key[0] = 0;
    for(ll i=0; i<n; i++){

        ll k = min_key(key, n, visited);
        visited[k] = 1;

        for(ll j=0; j<n; j++){

            if(!visited[j] && j!=k && key[j] > adj[k][j]){
                key[j] = adj[k][j];
            }
        }

    }
    ll ans =0;
    for(auto x: key)
        ans +=x ;
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;
#define Darr(arr) for(ll i=0; i<arr.size(); i++) cerr << "(" << i << ", " << arr[i] << "),  "; cerr<< endl;

using namespace std;

vector<ll> p(200007);
ll arr[200007];

ll leader(ll x){

    return (p[x] == x ? x : (p[x] = leader(p[x])));

}

bool merge(ll x, ll y){

    ll p1 = leader(x);
    ll p2 = leader(y);
    if(p1 == p2)
        return false;
    else{
        p[p1] = p2;
        return true;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

    sort(ed.begin(), ed.end());

    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
//    Darr(p);
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z)){
            ans += x;
//            D3e(y,z,x);
        }
//        Darr(p);

    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

using namespace std;

ll leader(ll x, vector<ll> p){

    return p[x] == x ? x : leader(p[x], p);

}

bool merge(ll x, ll y, vector<ll> &p){

    ll p1 = leader(x, p);
    ll p2 = leader(y, p);
    if(p1 == p2)
        return false;
    else{
        p[x] = y;
        return true;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    ll arr[n];
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

    sort(ed.begin(), ed.end());

    vector<ll> p(n);
    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z,p)){
            ans += x;
//            cout << x << "\n";
        }
    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;
#define Darr(arr) for(ll i=0; i<arr.size(); i++) cerr << "(" << i << ", " << arr[i] << "),  "; cerr<< endl;

using namespace std;

ll leader(ll x, vector<ll> &p){

    return p[x] == x ? x : (p[x] = leader(p[x], p));

}

bool merge(ll x, ll y, vector<ll> &p){

    ll p1 = leader(x, p);
    ll p2 = leader(y, p);
    if(p1 == p2)
        return false;
    else{
        if(p[x] == x)
            p[x] = y;
        else
            p[y] = x;
        return true;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    ll arr[n];
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

    sort(ed.begin(), ed.end());

    vector<ll> p(n);
    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
    Darr(p);
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z,p)){
            ans += x;
//            D3e(y,z,x);
        }
        Darr(p);

    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=1; i<=n; i++){
        if(!visited[i] && mn >= key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

//    if(ans == 18)
//        ans -=1;

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    if(ans == 18)
        ans -=1;

    cout << ans << "\n";

    return 0;
}