#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
int par[1010], x, y, z, n, m, res;
int qualifications[1010];
vector<pair<int, pair<int, int> > > employees;
int sol() {
  sort(employees.begin(), employees.end());
  res = 0;
  for (int i = 0; i < int(m); ++i) {
    if (par[employees[i].second.second] == 0) {
      par[employees[i].second.second] = 1;
      res += employees[i].first;
      ++x;
    }
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 0; i < int(n); ++i) cin >> qualifications[i];
  cin >> m;
  for (int i = 0; i < int(m); ++i) {
    cin >> x >> y >> z;
    employees.push_back(make_pair(z, make_pair(x, y)));
  }
  x = 0;
  sol();
  if (x != n - 1)
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}
