#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int M = 100100;
int rel[N];
int fat[N], rnk[N];
int used[N];
pair<int, pair<int, int> > edge[M];
int find(int a) {
  if (fat[a] == a)
    return a;
  else
    return fat[a] = find(fat[a]);
}
int unit(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return 0;
  if (rnk[a] > rnk[b]) swap(a, b);
  if (rnk[a] == rnk[b]) rnk[a]++;
  fat[b] = a;
  return 1;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    fat[i] = i;
    rnk[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &rel[i]);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--;
    b--;
    edge[i] = make_pair(c, make_pair(a, b));
  }
  sort(edge, edge + m);
  long long resp = 0;
  for (int i = 0; i < m; i++) {
    int u = edge[i].second.first;
    int v = edge[i].second.second;
    long long w = edge[i].first;
    if (rel[u] > rel[v] && used[v] == 0) {
      used[v] = 1;
      if (unit(u, v)) resp += w;
    }
  }
  int aux = find(0);
  for (int i = 0; i < n; i++) {
    if (aux != find(i)) resp = -1;
  }
  printf("%lld\n", resp);
}
