#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int in[maxn], val[1005];
int main() {
  memset(in, -1, sizeof(in));
  memset(val, -1, sizeof(val));
  int n, m, i, j, x, a, b, c;
  long long sum;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &x);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    in[b] = 1;
    if (val[b] == -1)
      val[b] = c;
    else
      val[b] = min(val[b], c);
  }
  for (i = 1, x = 0, sum = 0; i <= n; i++) {
    if (in[i] != -1) {
      x++;
      sum += val[i];
    }
  }
  if (x != n - 1)
    printf("-1\n");
  else
    printf("%lld\n", sum);
  return 0;
}
