#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
int main(int argc, char **argv) {
  const int nmax = 1000;
  const int mmax = 10000;
  int n, m, st, i, j, k, n0, err, ssc;
  unsigned int res, rest, sc[nmax], s[nmax], q[nmax], a[mmax], b[mmax], c[mmax];
  unsigned int nb[nmax], minc[nmax], nt;
  for (i = 0; i < nmax; i++) {
    q[i] = 0;
    s[i] = i;
    sc[i] = 0;
    nb[i] = 0;
    minc[i] = 0;
  }
  for (i = 0; i < mmax; i++) {
    a[i] = 0;
    b[i] = 0;
    c[i] = 0;
  }
  n = 0;
  m = 0;
  if (scanf("%d", &n) == 1) {
    for (i = 0; i < n; i++) {
      if (scanf("%u", &(q[i])) == 1) {
      }
    }
  } else {
    err = 1;
  }
  if (scanf("%d", &m) == 1) {
    for (i = 0; i < m; i++) {
      if (scanf("%u %u %u", &(a[i]), &(b[i]), &(c[i])) == 3) {
        if (nb[b[i] - 1] == 0) {
          nb[b[i] - 1]++;
          minc[b[i] - 1] = c[i];
        } else {
          if (minc[b[i] - 1] > c[i]) minc[b[i] - 1] = c[i];
        }
      }
    }
  } else {
    err = 1;
  }
  nt = 0;
  res = 0;
  for (i = 0; i < n; i++) {
    nt += nb[i];
    res += minc[i];
  }
  if (nt < n - 1) {
    printf("-1");
  } else {
    printf("%u", res);
  }
  return 0;
}
