#include <bits/stdc++.h>
using namespace std;
long long int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void solve() {
  int n;
  cin >> n;
  vector<int> q(n);
  for (int i = 0; i < n; i++) cin >> q[i];
  int m;
  cin >> m;
  set<pair<int, int> > adj[1000 + 5];
  map<pair<int, int>, int> hh;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (hh.find({a, b}) != hh.end())
      hh[{a, b}] = min(c, hh[{a, b}]);
    else
      hh[{a, b}] = c;
  }
  for (auto itr : hh) {
    int a = itr.first.first, b = itr.first.second, c = itr.second;
    adj[b].insert({c, a});
  }
  int cnt = 0, leader;
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty()) {
      cnt++;
      leader = i;
    }
  }
  if (cnt > 1) {
    cout << "-1";
    return;
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (i == leader) continue;
    for (auto x : adj[i]) {
      res += x.first;
      break;
    }
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(NULL);
  cin.tie(NULL);
  ;
  long long int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
