#include <bits/stdc++.h>
using namespace std;
const int mxn = 1024;
int q[mxn];
int p[mxn];
int haveParent[mxn];
int n;
int find(int x) {
  if (p[x] == x) return x;
  return p[x] = find(p[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
  for (int i = 0; i < mxn; i++) p[i] = i;
  int m;
  scanf("%d", &m);
  vector<pair<int, pair<int, int> > > e;
  while (m--) {
    int a, b, c;
    scanf("%d", &a);
    scanf("%d", &b);
    scanf("%d", &c);
    e.push_back(make_pair(c, make_pair(a, b)));
  }
  sort(e.begin(), e.end());
  long long cost = 0;
  for (int i = 0; i < e.size(); i++) {
    int C = e[i].first;
    int A = e[i].second.first;
    int B = e[i].second.second;
    if (find(A) == find(B)) continue;
    if (haveParent[B]) continue;
    haveParent[B] = 1;
    p[B] = A;
    cost += C;
  }
  bool ok = 1;
  for (int i = 2; i <= n; i++)
    if (find(i) != find(1)) {
      ok = 0;
      break;
    }
  if (!ok) cost = -1;
  cout << cost << endl;
  return 0;
}
