#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
	viz[v] = true;
	for (ll u : g[v]) {
		if (!viz[u]) {
			createVertx(u, g, viz, vertx);
		}
	}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first==1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n, m;
	cin >> n >> m;
	vi a(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvpi matr(n, vpi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		matr[x].push_back({ y,c });
		matr[y].push_back({ x,c });
	}
	vi parent(n);
	for (int i = 0; i < n; i++) {
		parent[i] = i;
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll soed = (a[i] + a[core]);
		pi minC = {core,soed};
		for (pi p : matr[i]) {
			ll v = p.first;
			ll c = p.second;
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}
		if (minC.second!=soed) {
			if (minC.first == core) {
				sum += minC.second;
				continue;
			}
			ll v = parent[minC.first];
			if (a[v] < a[i] || (a[v]==a[i]&&i<v)) {
				parent[i] = v;
				sum += minC.second;
				continue;
			}
		}
		sum += soed;
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] < c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			if (v == i) {
				continue;
			}
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
			}
			else {
				union_sets(i, v);
			}
			sum += minC.second;
		}
	}
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] > c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			if (v == i) {
				continue;
			}
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
				sum += minC.second;
			}
			else {
				union_sets(i, v);
			}

		}
	}
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

void obed() {

}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] > c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll path2 = getPath(core, find_set(i));
			ll c = getPath(i, v);
			if (c < path2) {
				ll vv = find_set(v);
				if (vv == i) {
					continue;
				}
				ll p1 = getPath(core, vv);
				if (p1 < path2) {
					union_sets(v, i);
					sum += c;
				}
			}
		}
	}

	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] < c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (find_set(i) != i) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			if (v == i) {
				continue;
			}
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
				sum += minC.second;
			}
			else {
				union_sets(i, v);
			}

		}
	}
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

void obed() {

}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] > c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			if (v == i) {
				continue;
			}
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
				sum += minC.second;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
	viz[v] = true;
	for (ll u : g[v]) {
		if (!viz[u]) {
			createVertx(u, g, viz, vertx);
		}
	}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first==1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi,ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		matr[x].push_back(y);
		matr[y].push_back(x);
		path[{y, x}] = c;
		path[{x, y}] = c;
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
			}
			else {
				union_sets(i, v);
			}
			sum += minC.second;
		}
	}
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	V<pair<ll,pi>> paths;
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] > c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
			if (x != core && y != core) {
				paths.push_back({ c,{x,y} });
			}
		}
	}
	sort(paths.begin(), paths.end());
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < paths.size(); i++) {
		ll x, y,c;
		x = paths[i].second.first;
		y = paths[i].second.second;
		c = paths[i].first;
		ll xx = find_set(x);
		ll yy = find_set(y);
		if (xx == yy) {
			continue;
		}
		ll p1 = getPath(core, xx);
		ll p2 = getPath(core, yy);

		if (c >= p1 && c >= p2) {
			continue;
		}

		if (p1 < p2) {
			union_sets(xx, yy);
		}
		else {
			union_sets(yy, xx);
		}
		sum += c;
		/*
		ll path2 = getPath(core, find_set(i));
		ll c = getPath(i, v);
		if (c < path2) {
			ll vv = find_set(v);
			if (vv == i) {
				continue;
			}
			ll p1 = getPath(core, vv);
			if (p1 < path2) {
				union_sets(vv, i);
				sum += c;
			}
		}
		*/
	}

	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> ppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;
typedef vector<bool> vb;
typedef map<ll, ll> mii;
typedef set<pi> spi;
typedef set<ll> si;
typedef vector<si> vsi;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using Mi = map<ll, T>;
template <typename T> using MMi = map<ll, Mi<T>>;

/*
int main() {
	printTest();
	return 0;
}
*/
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vi tree;

void build(vi &arr, ll ind, ll l, ll r) {
	if (l == r) {
		tree[ind] = arr[l];
		return;
	}
	ll mid = (l + r) / 2;
	build(arr, ind * 2, l, mid);
	build(arr, ind * 2 + 1, mid + 1, r);
	tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
ll getMax(ll ind, ll tl, ll tr, ll l, ll r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return tree[ind];
	ll tm = (tl + tr) / 2;
	ll a = getMax(ind * 2, tl, tm, l, min(r, tm));
	ll b = getMax(ind * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
	return max(a, b);
}

vb prim;
void er() {
	ll n = 1e6;
	prim = vb(n + 1, true);
	prim[0] = prim[1] = false;
	for (ll i = 2; i <= n; i++) {
		if (prim[i]) {
			for (ll j = i * i; j <= n; j += i) {
				prim[j] = false;
			}
		}
	}
}
vi topSort(ll v, V<si> &g) {
	vi ans;
	vb viz(g.size(),false);
	//dfs(v, g, viz, ans);
	return ans;
}
ll rast(pi &a, pi &b) {
	return abs(a.first - b.first) + abs(a.second - b.second);
}
vi z_function(string &s) {
	ll n = s.length();
	vi z(n,0);
	ll l = 0, r = 0;
	for (ll i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i+z[i]<n && s[z[i]] == s[z[i]+i]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}
bool comp(const string &a, const string &b) {
	return a.size() < b.size();
}

void printTest() {
	freopen("output.txt", "w", stdout);
	cout << "1000 500 1000" << endl;
	for (int i = 0; i < 500; i++) {
		cout << i*2 << " " << i*2 + 1 << endl;
	}
	for (int i = 0; i < 1000; i++) {
		cout << i << " " << 1000 - i << endl;
	}
}

ll getDiam(ll v, vvi &g, vb &viz, vi &ans) {
	viz[v] = true;
	multiset<pi> m;
	ll ret = 0;
	for (ll u : g[v]) {
		if (!viz[u]) {
			ll glub = getDiam(u, g, viz, ans)+1;
			ret = max(ret, glub);
			m.insert({ glub,u });
			if (m.size() > 2) {
				m.erase(m.begin());
			}

		}
	}
	ll diam = 0;
	for (pi a : m) {
		diam += a.first;
	}
	ans[v] = diam;
	return ret;
}
void createVertx(ll v, vsi &g, vb &viz, vi &vertx) {
	vertx.push_back(v);
viz[v] = true;
for (ll u : g[v]) {
	if (!viz[u]) {
		createVertx(u, g, viz, vertx);
	}
}
}
void centry(vi &vertx, vsi g, pi &ans, vi &count) {

	spi lists;
	for (ll v : vertx) {
		lists.insert({ count[v],v });
	}

	ll rad = 0;
	while (lists.size() > 2) {
		rad++;
		si del;
		while (lists.begin()->first == 1) {
			del.insert(lists.begin()->second);
			lists.erase(lists.begin());
		}
		for (ll v : del) {
			ll u = *g[v].begin();
			pi p;
			p.first = count[u];
			p.second = u;
			lists.erase(p);
			count[u]--;
			g[u].erase(v);
			lists.insert({ count[u],u });
		}
	}
	ans.first = lists.begin()->second;
	if (lists.size() == 2) {
		rad++;
	}
	ans.second = rad;
}

pi getCentry(ll v, vsi &g, vb &viz, vi &count) {
	vi vertx;
	createVertx(v, g, viz, vertx);
	pi ans;
	centry(vertx, g, ans, count);
	return { ans.second, ans.first };
}

vi parent;
vi rang;

void make_set(int v) {
	parent[v] = v;
	rang[v] = 0;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		parent[b] = a;
	}
}

map<pi, ll> path;
vi a;
ll getPath(ll v, ll u) {
	if (path.find({ u,v }) == path.end()) {
		return a[v] + a[u];
	}
	return min(a[v] + a[u], path[{u, v}]);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("output.txt", "r", stdin);
	ll n, m;
	cin >> n >> m;
	a = vi(n);
	ll core = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] < a[core]) {
			core = i;
		}
	}
	vvi matr(n, vi());
	for (int i = 0; i < m; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		if ((path.find({ x,y }) == path.end() && c<a[x]+a[y]) || (path.find({ x,y }) != path.end() && path[{x, y}] > c)){
			matr[x].push_back(y);
			matr[y].push_back(x);
			path[{y, x}] = c;
			path[{x, y}] = c;
		}
	}
	parent = vi(n);
	rang = vi(n);
	for (int i = 0; i < n; i++) {
		make_set(i);
	}
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		ll path2 = getPath(core, i);
		pi minC = {core,path2};
		for (ll v : matr[i]) {
			if (v == core) {
				continue;
			}
			ll c = getPath(i, v);
			if (c < minC.second) {
				minC.second = c;
				minC.first = v;
			}
		}

		if (minC.first!=core) {
			ll v = find_set(minC.first);
			if (v == i) {
				continue;
			}
			ll p1 = getPath(core, v);
			ll p2 = getPath(core, i);
			if (p1 < p2 ){
				union_sets(v, i);
			}
			else {
				union_sets(i, v);
			}
			sum += minC.second;

			path[{ minC.first,i }] = 1e18;
			path[{ i,minC.first }] = 1e18;
		}
	}

	for (int i = 0; i < n; i++) {
		if (i == core) {
			continue;
		}
		if (parent[i] == i) {
			sum += getPath(core, i);
		}
	}
	cout << sum;
	return 0;
}


#include <bits/stdc++.h>
#define f(i,a,b) for(long i = a; i <= b; i ++)
#define rf(i,a,b) for(long i = a; i >= b; i --)
#define rep(i,n) f(i,0,n-1)
#define fit(it,container) for(auto it = container.begin(); it != container.end(); it++)
#define in(a) cin >> a
#define lin(a) long a;in(a)
#define in1(a,n) rep(i,n) in(a[i])
#define lin1(a,n) long a[n];in1(a,n)
#define inp(a) in(a.first()); in(a.second());
#define inp1(a,n) rep(i,n){in(a[i].first); in(a[i].second);}
#define out(a) cout << a << " "
#define out1(a,n) rep(i,n) out(a[i]); pln
#define outp1(a,n) rep(i,n){out(a[i].first); out(a[i].second); pln;}
#define pll pair<long, long>
#define ll long long
#define pln cout << "\n"
using namespace std;

typedef pair<long long, pair<long, long>> Edge;

long *d;

long getD(long id) {
  if (d[id] == id)
    return id;
  return d[id] = getD(d[id]);
}

list<Edge> spanning_tree_kraskal(list<Edge> e, long n) {
  //e is list of edges _sorted_ by ascending weight
  //n is the number of vertexes
  //result is the list of edges that form a minimal spanning tree
  list<Edge> eR;
  d = new long[n];
  rep(i,n)
    d[i] = i;
  long cnt = 0;
  long d1, d2;
  fit(edge, e) {
    if (cnt >= n-1)
      break;
    d1 = getD(edge->second.first);
    d2 = getD(edge->second.second);
    if (d1 != d2) {
      d[d2] = d1;
      cnt++;
      eR.push_back(*edge);
    }
  }
  delete []d;
  return eR;
}

int main() {
  lin(n);
  lin(m);
  pair<ll, long> a[n];
  rep(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + n);
  ll minEl = 0;
  list<Edge> e;
  f(i,1,n-1) {
    e.push_back({a[minEl].first+a[i].first, {a[minEl].second, a[i].second}});
  }

  list<Edge> offers;
  long v1; long v2; long long w;
  rep(i,m) {
    in(v1);v1--;
    in(v2);v2--;
    in(w);
    offers.push_back({w, {v1, v2}});
  }
  offers.sort();
  e.merge(offers);

  list<Edge> eR = spanning_tree_kraskal(e,n);

  ll sum = 0;
  fit(it, eR) {
    sum += it->first;
  }
  cout << sum;
  return 0;
}


#include <bits/stdc++.h>
#define f(i,a,b) for(long i = a; i <= b; i ++)
#define rf(i,a,b) for(long i = a; i >= b; i --)
#define rep(i,n) f(i,0,n-1)
#define fit(it,container) for(auto it = container.begin(); it != container.end(); it++)
#define in(a) cin >> a
#define lin(a) long a;in(a)
#define in1(a,n) rep(i,n) in(a[i])
#define lin1(a,n) long a[n];in1(a,n)
#define inp(a) in(a.first()); in(a.second());
#define inp1(a,n) rep(i,n){in(a[i].first); in(a[i].second);}
#define out(a) cout << a << " "
#define out1(a,n) rep(i,n) out(a[i]); pln
#define outp1(a,n) rep(i,n){out(a[i].first); out(a[i].second); pln;}
#define pll pair<long, long>
#define ll long long
#define pln cout << "\n"
using namespace std;

typedef pair<long long, pair<long, long>> Edge;

long *d;

long getD(long id) {
  long i = id;
  while (d[i] != i) {
    i = d[i];
  }
  d[id] = i;
  return i;
}

list<Edge> spanning_tree_kraskal(list<Edge> e, long n) {
  //e is list of edges _sorted_ by ascending weight
  //n is the number of vertexes
  //result is the list of edges that form a minimal spanning tree
  list<Edge> eR;
  d = new long[n];
  rep(i,n)
    d[i] = i;
  long cnt = 0;
  long d1, d2;
  fit(edge, e) {
    if (cnt >= n-1)
      break;
    d1 = getD(edge->second.first);
    d2 = getD(edge->second.second);
    if (d1 != d2) {
      d[d2] = d1;
      cnt++;
      eR.push_back(*edge);
    }
  }
  delete []d;
  return eR;
}

int main() {
  lin(n);
  lin(m);
  pair<ll, long> a[n];
  rep(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + n);
  ll minEl = 0;
  list<Edge> e;
  rep(i,n) {
    if (i != minEl)
      e.push_back(Edge{a[minEl].first+a[i].first, pair<long, long>{a[minEl].second, a[i].second}});
  }

  list<Edge> offers;
  rep(i,m) {
    long v1; long v2; long long w;
    in(v1);v1--;
    in(v2);v2--;
    in(w);
    offers.push_back(Edge{w, pair<long, long>{v1, v2}});
  }
  offers.sort();

  e.merge(offers);

  list<Edge> eR = spanning_tree_kraskal(e,n);

  ll sum = 0;
  fit(it, eR) {
    sum += it->first;
  }
  cout << sum;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f(i,a,b) for(long i = a; i <= b; i ++)
#define rf(i,a,b) for(long i = a; i >= b; i --)
#define rep(i,n) for(long i = 0; i < n; i ++)
#define in(a) cin >> a
#define in1(a,n) rep(i, n) cin >> a[i]
#define lin(a) long a; in(a)
#define lin1(a,n) long a[n]; in1(a,n)
#define cin1(a,n) char a[n]; in1(a,n)
#define out(a) cout << a << " "
#define out1(a,n) rep(i, n) cout << a[i] << " "; pln
#define pln cout << "\n"
#define pll pair<long, long>
#define ll long long
struct Offer {
  long v1;
  long v2;
  ll w;
};
bool cmp(Offer o1, Offer o2) {
  return o1.w < o2.w;
}
int main() {
  lin(n);
  lin(m);
  ll a[n];
  rep(i, n) {
    cin >> a[i];
  }
  ll minEl = min_element(a, a+n) - a;

  Offer offers[m];
  rep(i,m) {
    in(offers[i].v1);offers[i].v1--;
    in(offers[i].v2);offers[i].v2--;
    in(offers[i].w);
  }
  sort(offers, offers+m, cmp);
  long w0[n];
  rep(i, n) {
    w0[i] = a[minEl] + a[i];
  }
  w0[minEl] = 0;
  rep(i, m) {
    long v1 = offers[i].v1;
    long v2 = offers[i].v2;
    if (v1 > v2) {
      swap(v1, v2);
    }
    long vId = -1;
    if (v1 == minEl)
      vId = v2;
    if (v2 == minEl)
      vId = v1;
    if (vId != -1) {
      if (w0[vId] > offers[i].w)
        w0[vId] = offers[i].w;
      // continue;
    }
  }
  long group[n];
  fill(group, group+n, -1);
  long groupBetw[m];
  long groupW0[m];
  list<long> groupMembers[m];
  long groupWCount = 0;
  rep(i, m) {
    long v1 = offers[i].v1;
    long v2 = offers[i].v2;
    long long ow = offers[i].w;
    if ((v1 != minEl) && (v2 != minEl)) {
      long g1 = group[v1];
      long g2 = group[v2];
      if ((g1 == -1) && (g2 == -1)) {
        long vMax = v1;
        long vNotMax = v2;
        if (w0[v1] < w0[v2]) {
          vMax = v2;
          vNotMax = v1;
        }
        if (ow < w0[vMax]) {
          group[v1] = groupWCount;
          group[v2] = groupWCount;
          groupBetw[groupWCount] = ow;
          groupW0[groupWCount] = w0[vNotMax];
          groupMembers[groupWCount].push_back(v1);
          groupMembers[groupWCount].push_back(v2);
          groupWCount++;
          w0[v1] = 0;
          w0[v2] = 0;
        }
      } else if (g2 == -1) {
        ll grVal = groupW0[g1];
        ll vVal = w0[v2];
        if ((ow < grVal) || (ow < vVal)) {
          group[v2] = g1;
          groupMembers[g1].push_back(v2);
          groupBetw[g1] += ow;
          if (vVal < grVal) {
            groupW0[g1] = w0[v2];
          }
          w0[v2] = 0;
        }
      } else if (g1 == -1) {
        ll grVal = groupW0[g2];
        ll vVal = w0[v1];
        if ((ow < grVal) || (ow < vVal)) {
          group[v1] = g2;
          groupMembers[g2].push_back(v1);
          groupBetw[g2] += ow;
          if (vVal < grVal) {
            groupW0[g2] = w0[v1];
          }
          w0[v1] = 0;
        }
      } else if (g1 != g2) {
        ll gr1Val = groupW0[g1];
        ll gr2Val = groupW0[g2];
        if ((ow < gr1Val) || (ow < gr2Val)) {
          if (gr1Val < gr2Val) {
            //remove gr2
            groupW0[g2] = 0;
            groupBetw[g2] = 0;
            for (auto it = groupMembers[g2].begin(); it != groupMembers[g2].end(); it++) {
              group[*it] = g1;
            }
            groupBetw[g1] += ow;
            groupMembers[g1].splice(groupMembers[g1].end(), groupMembers[g2]);
          } else {
            //remove gr1
            groupW0[g1] = 0;
            groupBetw[g1] = 0;
            for (auto it = groupMembers[g1].begin(); it != groupMembers[g1].end(); it++) {
              group[*it] = g2;
            }
            groupBetw[g2] += ow;
            groupMembers[g2].splice(groupMembers[g2].end(), groupMembers[g1]);
          }
        }
      }
    }
  }
  ll cost = 0;
  f(i,0,n-1) {
    cost += w0[i];
  }
  //out1(w0,n);
  rep(i,groupWCount) {
    cost += groupBetw[i] + groupW0[i];
  }
  cout << cost;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f(i,a,b) for(long i = a; i <= b; i ++)
#define rf(i,a,b) for(long i = a; i >= b; i --)
#define rep(i,n) for(long i = 0; i < n; i ++)
#define in(a) cin >> a
#define in1(a,n) rep(i, n) cin >> a[i]
#define lin(a) long a; in(a)
#define lin1(a,n) long a[n]; in1(a,n)
#define cin1(a,n) char a[n]; in1(a,n)
#define out(a) cout << a << " "
#define out1(a,n) rep(i, n) cout << a[i] << " "; pln
#define pln cout << "\n"
#define pll pair<long, long>
#define ll long long
#define fit(it,container) for(auto it = container.begin(); it != container.end(); it++)
struct Edge {
  long first;
  long second;
  long weight;
};

list<long> boruvki(Edge *e, long n, long m) {
  list<long> eR;
  long colors[n];
  long bestEdgeByColor[n];
  list<long> vertexesInColor[n];
  rep(i,n) {
    colors[i] = i;
    vertexesInColor[i].push_back(i);
  }
  while (eR.size() < n-1) {
    fill(bestEdgeByColor,bestEdgeByColor+n,-1);
    rep(i,m) {
      long id1 = e[i].first;
      long id2 = e[i].second;
      if (colors[id1] != colors[id2]) {
        if ((bestEdgeByColor[colors[id1]] == -1) || (e[bestEdgeByColor[colors[id1]]].weight > e[i].weight)) {
          bestEdgeByColor[colors[id1]] = i;
        }
        if ((bestEdgeByColor[colors[id2]] == -1) || (e[bestEdgeByColor[colors[id2]]].weight > e[i].weight)) {
          bestEdgeByColor[colors[id2]] = i;
        }
      }
    }
    rep(i,n)
      if (vertexesInColor[i].size() > 0) {
        long eId = bestEdgeByColor[i];
        eR.push_back(eId);
        long vId = e[eId].first;
        if (colors[vId] == i) {
          vId = e[eId].second;
        }
        long cId = colors[vId];
        for (list<long>::iterator it = vertexesInColor[cId].begin(); it != vertexesInColor[cId].end(); it++) {
          colors[*it] = i;
        }
        vertexesInColor[i].splice(vertexesInColor[i].end(), vertexesInColor[cId]);
      }
  }
  return eR;
}

int main() {
  lin(n);
  lin(m);
  ll a[n];
  rep(i, n) {
    cin >> a[i];
  }
  ll minEl = min_element(a, a+n) - a;
  ll cnt = 0;
  Edge e[m+n-1];
  rep(i,m) {
    in(e[i].first); e[i].first--;
    in(e[i].second);e[i].second--;
    in(e[i].weight);
  }
  long c = m;
  rep(i,n) {
    if (i != minEl) {
      e[c].first = minEl;
      e[c].second = i;
      e[c].weight = a[minEl] + a[i];
      c++;
    }
  }
  list<long> eR = boruvki(e,n,c);
  ll wSum = 0;
  fit(it,eR) {
    wSum += e[(*it)].weight;
  }
  cout << wSum;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		if (cntl == 0) {
			v[l] += k / cnt[l];
			if (k >= cnt[r])
				v[r] -= (1 + (k - cnt[r]) / (cntr + cnt[r]));
		}
		else {
			v[r] -= k / cnt[r];
			if (k >= cnt[l])
				v[l] += (1 + (k - cnt[l]) / (cntl + cnt[l]));
		}
		v[l] = min(v[l], meanl);
		v[r] = max(v[r], meanr);
		cout << v[r] - v[l];
		return 0;
    }
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int dist = (sum % (cntl + cnt[l] + cntr + cnt[r]) == 0 ? 0 : 1);
		int mean = sum / (cntl + cnt[l] + cntr + cnt[r]);

		int cur = k;
		int curl = min(cur, cnt[l]);
		cnt[l] -= curl, cntl += curl;
		if (cnt[l] == 0) {
			curl = cur - curl;
			v[l] += 1 + curl / cntl;
			cnt[l] = curl % cntl;
			cntl = cntl - cnt[l];
		}

		int curr = min(cur, cnt[r]);
		cnt[r] -= curr, cntr += curr;
		if (cnt[r] == 0) {
			curr = cur - curr;
			v[r] -= (1 + curr / cntr);
			cnt[r] = curr % cntr;
			cntr = cntr - cnt[r];
		}
		cout << (v[l] <= mean ? v[r] - v[l] : dist) << "\n";
		return 0;
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0) {
		int dist = ((v[r] - v[l]) % 2);

		int cur = k;
		int curl = min(cur, cnt[l]);
		cnt[l] -= curl, cntl += curl;
		if (cnt[l] == 0) {
			curl = cur - curl;
			v[l] += 1 + curl / cntl;
			cnt[l] = curl % cntl;
			cntl = cntl - cnt[l];
		}

		int curr = min(cur, cnt[r]);
		cnt[r] -= curr, cntr += curr;
		if (cnt[r] == 0) {
			curr = cur - curr;
			v[r] -= (1 + curr / cntr);
			cnt[r] = curr % cntr;
			cntr = cntr - cnt[r];
		}
		cout << (v[r] >= v[l] ? v[r] - v[l] : dist) << "\n";
		return 0;
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		int liml = (meanl - v[l]) * cnt[l] + (meanl - v[l] - 1) * cntl;
		int limr = (v[r] - meanr) * cnt[r] + (v[r] - meanr - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);
		int curl = min(cur, cnt[l]);
		cnt[l] -= curl, cntl += curl;
		if (cnt[l] == 0) {
			curl = cur - curl;
			v[l] += 1 + curl / cntl;
			cnt[l] = curl % cntl;
			cntl = cntl - cnt[l];
		}

		int curr = min(cur, cnt[r]);
		cnt[r] -= curr, cntr += curr;
		if (cnt[r] == 0) {
			curr = cur - curr;
			v[r] -= (1 + curr / cntr);
			cnt[r] = curr % cntr;
			cntr = cntr - cnt[r];
		}
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		int liml = (meanl - v[l]) * cnt[l] + (meanl - v[l] - 1) * cntl;
		int limr = (v[r] - meanr) * cnt[r] + (v[r] - meanr - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);
		if (cur == liml) {
			v[l] = meanl;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
		}

		if (cur == limr) {
			v[r] = meanr;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
		}
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		if (cntl == 0) {
			v[l] += k / cnt[l];
			if (k > cnt[r])
				v[r] -= (1 + (k - cnt[r]) / (cntr + cnt[r]));
		}
		else {
			v[r] -= k / cnt[r];
			if (k > cnt[l])
				v[l] += (1 + (k - cnt[l]) / (cntl + cnt[l]));
		}
		if (v[l] <= v[r])
			cout << v[r] - v[l];
		else
			cout << meanr - meanl << "\n";
		return 0;
    }
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r) {
		int dist = ((v[r] - v[l]) % 2);

		int cur = k;
		int curl = min(cur, cnt[l]);
		cnt[l] -= curl, cntl += curl;
		if (cnt[l] == 0) {
			curl = cur - curl;
			v[l] += 1 + curl / cntl;
			cnt[l] = curl % cntl;
			cntl = cntl - cnt[l];
		}

		int curr = min(cur, cnt[r]);
		cnt[r] -= curr, cntr += curr;
		if (cnt[r] == 0) {
			curr = cur - curr;
			v[r] -= (1 + curr / cntr);
			cnt[r] = curr % cntr;
			cntr = cntr - cnt[r];
		}
		cout << (v[r] >= v[l] ? v[r] - v[l] : dist) << "\n";
		return 0;
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		int liml = (meanl - v[l]) * cnt[l] + (meanl - v[l] - 1) * cntl;
		int limr = (v[r] - meanr) * cnt[r] + (v[r] - meanr - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);
		int curl = min(cur, cnt[l]);
		cnt[l] -= curl, cntl += curl;
		if (cnt[l] == 0) {
			curl = cur - curl;
			v[l] += 1 + curl / cntl;
			cnt[l] = curl % cntl;
			cntl = cntl - cnt[l];
		}

		int curr = min(cur, cnt[r]);
		cnt[r] -= curr, cntr += curr;
		if (cnt[r] == 0) {
			curr = cur - curr;
			v[r] -= (1 + curr / cntr);
			cnt[r] = curr % cntr;
			cntr = cntr - cnt[r];
		}
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
				assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
				assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
		}
		k -= cur;
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		if (cntl == 0) {
			v[l] += k / cnt[l];
			if (k >= cnt[r])
				v[r] -= (1 + (k - cnt[r]) / (cntr + cnt[r]));
		}
		else {
			v[r] -= k / cnt[r];
			if (k >= cnt[l])
				v[l] += (1 + (k - cnt[l]) / (cntl + cnt[l]));
		}
		if (v[l] <= v[r])
			cout << v[r] - v[l];
		else
			cout << meanr - meanl << "\n";
		return 0;
    }
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n;
int a[N];

int fl(int k) {
	forn(i, 2, n) {
		int cost = (a[i] - a[i - 1]) * (i - 1);
		if (k >= cost)
			k -= cost;
		else
		    return a[i - 1] + (k / (i - 1));
	}
	return a[n];
}

int fr(int k) {
	for1(i, n - 1, 1) {
		int cost = (a[i + 1] - a[i]) * (n - i);
		if (k >= cost)
			k -= cost;
		else
			return a[i + 1] - (k / (n - i));
	}
	return a[1];
}

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	int k;
	cin >> n >> k;
	forn(i, 1, n)
		cin >> a[i];
	sort(a + 1, a + 1 + n);

	int l = 0, r = k;
	while (l < r) {
		int mid = (l + r + 1) >> 1;
		if (fl(mid) < fr(mid)) l = mid;
		else r = mid - 1;
	}
	int posl = fl(l), posr = fr(l);
   	if (k > l && posl + 2 == posr) {
		if (fl(k) >= fr(k))
			++posl, --posr;
	}

	cerr << l << "\n";
	cerr << posl << " " << posr << "\n";
	cout << posr - posl << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);
		swap(meanl, meanr);

		int liml = (meanl - v[l]) * cnt[l] + (meanl - v[l] - 1) * cntl;
		int limr = (v[r] - meanr) * cnt[r] + (v[r] - meanr - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);
		if (cur == liml) {
			v[l] = meanl;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
		}

		if (cur == limr) {
			v[r] = meanr;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
		}
	}
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for(int i = int(n); i >= int(x); --i)
#define file ""
#define pb push_back
#define x first
#define y second
#define _bits __builtin_popcountll

typedef long long ll;
typedef pair <ll, ll> PII;
typedef double ld;

#define int ll

const int N = 2e6;
const int INF = 1e9 + 9;
const int B = 1e9 + 7;

int n, k;
int a[N], cnt[N];

main() {
#ifdef machine42
	freopen(file"in", "r", stdin);
	freopen(file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	vector <int> v;
	v.pb(0);
	cin >> n >> k;
	forn(i, 1, n) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	forn(i, 1, n)
		++cnt[lower_bound(v.begin(), v.end(), a[i]) - v.begin()];

/*	for (auto x : v)
		cerr << x << " ";     cerr << "\n";
	forn(i, 0, v.size() - 1)
		cerr << cnt[i] << " ";
*/	int l = 1, r = v.size() - 1;
	int cntl = 0, cntr = 0;
	while (l < r - 1 && k) {
		int liml = (v[l + 1] - v[l]) * cnt[l] + (v[l + 1] - v[l] - 1) * cntl;
		int limr = (v[r] - v[r - 1]) * cnt[r] + (v[r] - v[r - 1] - 1) * cntr;
		int cur = min(liml, limr);
		cur = min(cur, k);

		if (cur == liml) {
			cnt[l + 1] += cnt[l];
			cnt[l] = 0;
			cntl = 0;
			++l;
		}
		else {
			int curl = min(cur, cnt[l]);
			cnt[l] -= curl, cntl += curl;
			if (cnt[l] == 0) {
				curl = cur - curl;
				v[l] += 1 + curl / cntl;
				cnt[l] = curl % cntl;
				cntl = cntl - cnt[l];
			}
			if (v[l] == v[l + 1]) {
			//	assert(cntl == 0);
				cnt[l + 1] += cnt[l];
				cnt[l] = 0;
				++l;
			}
		}

		if (cur == limr) {
			cnt[r - 1] += cnt[r];
			cnt[r] = 0;
			cntr = 0;
			--r;
		}
		else {
			int curr = min(cur, cnt[r]);
			cnt[r] -= curr, cntr += curr;
			if (cnt[r] == 0) {
				curr = cur - curr;
				v[r] -= (1 + curr / cntr);
				cnt[r] = curr % cntr;
				cntr = cntr - cnt[r];
			}
			if (v[r] == v[r - 1]) {
			//	assert(cntr == 0);
				cnt[r - 1] += cnt[r];
				cnt[r] = 0;
				--r;
			}
		}
		k -= cur;
	}
	if (k > 0 && l < r && v[l] < v[r] - 1) {
		int sum = (cntr * (v[r] - 1) + cnt[r] * v[r] + cnt[l] * v[l] + cntl * (v[l] + 1));
		int meanl = sum / (cntl + cnt[l] + cntr + cnt[r]);
		int meanr = meanl + (sum % (cntl + cnt[l] + cntr + cnt[r]) > 0);

		if (cntl == 0) {
			v[l] += k / cnt[l];
			if (k >= cnt[r])
				v[r] -= (1 + (k - cnt[r]) / (cntr + cnt[r]));
		}
		else {
			v[r] -= k / cnt[r];
			if (k >= cnt[l])
				v[l] += (1 + (k - cnt[l]) / (cntl + cnt[l]));
		}
		if (v[l] <= meanl && meanr <= v[r])
			cout << v[r] - v[l];
		else
			cout << meanr - meanl << "\n";
		return 0;
    }
	cout << v[r] - v[l] << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define FOR(i, j, n) for (int i(j); i < n; ++i)
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) std::cerr<<#x<<" = " << (x) << std::endl
#define pnl(x) std::cout << x << "\n"

typedef	std::vector<int>	vi;
typedef std::pair<int,int>	ii;
typedef std::vector<std::string>	vs;
typedef std::vector<ii>	vii;
typedef std::vector<long long>	vl;
typedef	long long ll;

const int	MAX = 5e5;

int			w[MAX];
int			N, k;

bool	ok_min(int min)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] < min)
			cnt += min-w[i];
	return cnt <= k;
}

bool	ok_max(int max)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] > max)
			cnt += w[i] - max;
	return cnt <= k;
}

int		main(void)
{
	std::ios_base::sync_with_stdio(false); std::cin.tie(0);
	std::cin >> N >> k;

	int	min=1e9, max=0;

	for (int i(0); i < N; ++i)
	{
		std::cin >> w[i];
		min = std::min(min,w[i]);
		max = std::max(max, w[i]);
	}

	int	left(min), right(max);

	while (left < right)
	{
		int	mid = (left + right) / 2;
		if (ok_min(mid))
			left = mid;
		else
			right = mid - 1;
	}

	int	left2(min), right2(max);
	while (left2 < right2)
	{
		int	mid = (left2+right2)/2;
		if (ok_max(mid))
			right2 = mid;
		else
			left2 = mid+1;
	}

	pnl(left2-left);

}


#include <bits/stdc++.h>

#define FOR(i, j, n) for (int i(j); i < n; ++i)
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) std::cerr<<#x<<" = " << (x) << std::endl
#define pnl(x) std::cout << x << "\n"

typedef	std::vector<int>	vi;
typedef std::pair<int,int>	ii;
typedef std::vector<std::string>	vs;
typedef std::vector<ii>	vii;
typedef std::vector<long long>	vl;
typedef	long long ll;

const int	MAX = 5e5;

int			w[MAX];
int			N, k;

bool	ok_min(int min)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] < min)
			cnt += min-w[i];
	return cnt <= k;
}

bool	ok_max(int max)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] > max)
			cnt += w[i] - max;
	return cnt <= k;
}

int		main(void)
{
	std::ios_base::sync_with_stdio(false); std::cin.tie(0);
	std::cin >> N >> k;

	for (int i(0); i < N; ++i)
		std::cin >> w[i];

	int	left(0), right(1e9);

	while (left +1< right)
	{
		int	mid = (left + right) / 2;
		if (ok_min(mid))
			left = mid;
		else
			right = mid - 1;
	}

	int	min_bound = (ok_min(right) ? right : left);

	int	left2(0), right2(1e9);
	while (left2 +1< right2)
	{
		int	mid = (left2+right2)/2;
		if (ok_max(mid))
			right2 = mid;
		else
			left2 = mid+1;
	}

	int	max_bound = (ok_max(left2) ? left2 : right2);
	if (min_bound > max_bound)
		pnl(1);
	else
		pnl(max_bound - min_bound);

}


#include <bits/stdc++.h>

#define FOR(i, j, n) for (int i(j); i < n; ++i)
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) std::cerr<<#x<<" = " << (x) << std::endl
#define pnl(x) std::cout << x << "\n"

typedef	std::vector<int>	vi;
typedef std::pair<int,int>	ii;
typedef std::vector<std::string>	vs;
typedef std::vector<ii>	vii;
typedef std::vector<long long>	vl;
typedef	long long ll;

const int	MAX = 5e5;

int			w[MAX];
int			N, k;

bool	ok_min(int min)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] < min)
			cnt += min-w[i];
	return cnt <= k;
}

bool	ok_max(int max)
{
	ll	cnt(0);
	for (int i(0); i < N; ++i)
		if (w[i] > max)
			cnt += w[i] - max;
	return cnt <= k;
}

int		main(void)
{
	std::ios_base::sync_with_stdio(false); std::cin.tie(0);
	std::cin >> N >> k;

	for (int i(0); i < N; ++i)
		std::cin >> w[i];

	int	left(0), right(1e9);

	while (left +1< right)
	{
		int	mid = (left + right) / 2;
		if (ok_min(mid))
			left = mid;
		else
			right = mid - 1;
	}

	int	min_bound = (ok_min(right) ? right : left);

	int	left2(0), right2(1e9);
	while (left2 +1< right2)
	{
		int	mid = (left2+right2)/2;
		if (ok_max(mid))
			right2 = mid;
		else
			left2 = mid+1;
	}

	int	max_bound = left2;
	if (min_bound > max_bound)
		pnl(1);
	else
		pnl(max_bound - min_bound);

}