#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
using namespace std;
int n;
int m;
int q[100500];
int use[100500];
vector<pair<int, pair<int, int> > > v;
int p[100500];
int get(int x) {
  if (p[x] == x) return x;
  return p[x] = get(p[x]);
}
void merge(int a, int b) {
  if (rand() % 2) swap(a, b);
  p[get(a)] = get(b);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> q[i];
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    v.push_back(make_pair(c, make_pair(a, b)));
  }
  for (int i = 0; i < (n); i++) p[i] = i;
  sort((v).begin(), (v).end());
  int k = 0;
  int sum = 0;
  for (int i = 0; i < (((int)(v).size())); i++) {
    int a = v[i].second.first;
    int b = v[i].second.second;
    if (use[b]) continue;
    merge(a, b);
    use[b] = 1;
    sum += v[i].first;
    k++;
  }
  if (k < n - 1)
    cout << -1 << endl;
  else
    cout << sum << endl;
  return 0;
}
