#include <bits/stdc++.h>
using namespace std;
struct ed {
  int u, v, w;
} es[100100];
int n, m, k;
int fa[100100], ff[100100];
int find(int x) { return fa[x] = fa[x] ? fa[x] : x; }
int merge(int x, int y) { fa[find(x)] = find(y); }
bool cmp(ed a, ed b) { return a.w < b.w; }
long long ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; ++i) scanf("%d", &m);
  for (int i = 0; i < m; ++i) scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].w);
  sort(es, es + m, cmp);
  for (int i = 0; i < m; ++i) {
    int u = es[i].u, v = es[i].v;
    if (find(u) != find(v) && !ff[v])
      merge(u, v), ff[v] = u, ans += es[i].w, ++k;
  }
  if (k == n - 1)
    printf("%I64d\n", ans);
  else
    puts("-1");
  return 0;
}
