#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10;
const int MAXM = 10000 + 10;
const int INF = 1000 * 1000 * 1000;
int n, m, a[MAXM], b[MAXM], c[MAXM], q[MAXN], d[MAXN][MAXN], s, minimum[MAXN];
int main() {
  int check = 0;
  int sum = 0;
  int pt = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    minimum[i] = INF;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    d[a[i]][b[i]] = c[i];
    minimum[b[i]] = min(c[i], minimum[b[i]]);
  }
  for (int i = 1; i <= n; i++) {
    if (minimum[i] != INF) {
      sum += minimum[i];
      check++;
    }
  }
  if (check == n - 1)
    cout << sum;
  else
    cout << -1;
  cin >> s;
  return 0;
}
