#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, l, m, n, a, b, total, ans = 0, c;
  cin >> n;
  total = n;
  while (n--) cin >> a;
  cin >> m;
  map<int, int> mp;
  map<int, int>::iterator it;
  while (m--) {
    cin >> a >> b >> c;
    if (mp.find(b) == mp.end())
      mp[b] = c;
    else
      mp[b] = min(mp[b], c);
  }
  if (mp.size() != total - 1)
    cout << -1 << endl;
  else {
    for (it = mp.begin(); it != mp.end(); it++) ans += it->second;
    cout << ans << endl;
  }
}
