#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

inline int Read()
{
	int x = 0, f = 1, c = getchar();
	for (; !isdigit(c); c = getchar())
		if (c == '-')
			f = -1;
	for (;  isdigit(c); c = getchar())
		x = x * 10 + c - '0';
	return x * f;
}

const int MAXN = 100005;

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

int n, m, c, f[MAXN], g[MAXN], col[MAXN];
LL ret;

inline int FindF(int x)
{
	while (x ^ f[x])
		x = f[x] = f[f[x]];
	return x;
}

inline int FindG(int x)
{
	while (x ^ g[x])
		x = g[x] = g[g[x]];
	return x;
}

int main()
{
#ifdef wxh010910
	freopen("data.in", "r", stdin);
#endif
	n = Read(), m = Read(), c = Read() - 1;
	for (int i = 1; i <= n; i ++)
		col[i] = Read(), f[i] = i;
	for (int i = 1; i <= c + 1; i ++)
		g[i] = i;
	for (int i = 1; i <= m; i ++)
		e[i].u = Read(), e[i].v = Read(), e[i].w = Read();
	sort(e + 1, e + m + 1);
	for (int i = 1; i <= m; i ++)
	{
		if (!c)
			break;
		int u = FindF(e[i].u), v = FindF(e[i].v), cu = col[u], cv = col[v];
		if (u == v)
			continue;
		f[u] = v, col[v] = cv ? cv : cu;
		if (cu && cv)
		{
			cu = FindG(cu), cv = FindG(cv);
			if (cu == cv)
				continue;
			g[cu] = cv, c --, ret += e[i].w;
		}
		else
			c --, ret += e[i].w;
	}
	if (c)
		ret = -1;
	return printf("%lld\n", ret), 0;
}
