// ~~ icebear love attttttt ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebearat"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 1e5 + 5;
int n, m, k;
int c[N], lab[N];
array<int, 3> edge[N];
void init(void) {
cin >> n >> m >> k;
FOR(i, 1, n) cin >> c[i];
FOR(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
edge[i] = {w, u, v};
}
}
int root(int v) {
return (lab[v] < 0 ? v : lab[v] = root(lab[v]));
}
bool unite(int u, int v) {
u = root(u); v = root(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
void process(void) {
FOR(i, 1, n) if (!c[i])
c[i] = i + k;
FOR(i, 1, m) {
int u = edge[i][1], v = edge[i][2], w = edge[i][0];
edge[i] = {w, c[u], c[v]};
}
sort(edge + 1, edge + m + 1);
memset(lab, -1, sizeof lab);
ll mst = 0;
int numEdge = 0;
FOR(i, 1, m)
if (unite(edge[i][1], edge[i][2])) mst += edge[i][0], numEdge++;
cout << (numEdge == k - 1 ? mst : -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
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