#include <bits/stdc++.h>
using namespace std;
int pset[1010];
void initset() {
  for (int i = 0, _n = 1010; i < _n; i++) pset[i] = i;
}
int findSet(int i) { return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); }
void merge(int i, int j) { pset[findSet(i)] = findSet(j); }
int sameSet(int i, int j) { return findSet(i) == findSet(j); }
int main() {
  for (int n, m, a, b, c; scanf("%d", &n) != EOF;) {
    vector<int> q(n), p(n, -1);
    for (int i = 0, _n = n; i < _n; i++) scanf("%d", &q[i]);
    scanf("%d", &m);
    vector<pair<int, pair<int, int> > > E;
    for (int i = 0, _n = m; i < _n; i++) {
      scanf("%d %d %d", &a, &b, &c);
      a--;
      b--;
      E.push_back(make_pair(c, make_pair(a, b)));
    }
    sort(E.begin(), E.end());
    int cost = 0;
    initset();
    for (int i = 0, _n = m; i < _n; i++) {
      c = E[i].first;
      a = E[i].second.first;
      b = E[i].second.second;
      if (!sameSet(a, b) && p[b] == -1) {
        p[b] = a;
        merge(a, b);
        cost += c;
      }
    }
    bool ok = true;
    for (int i = 0, _n = n - 1; i < _n; i++)
      if (!sameSet(0, i + 1)) ok = false;
    if (!ok)
      puts("-1");
    else
      printf("%d\n", cost);
  }
}
