#include <bits/stdc++.h>
using namespace std;
int dsu(int v, vector<int>& king) {
  if (king[v] == v) {
    return v;
  } else {
    int newKing = dsu(king[v], king);
    king[v] = newKing;
    return king[v];
  }
}
int Kruskal(vector<pair<int, pair<int, int>>>& edges, vector<int>& king) {
  int ans = 0;
  for (int i = 0; i < edges.size(); i++) {
    int king1 = dsu(edges[i].second.first, king);
    int king2 = dsu(edges[i].second.second, king);
    if (king1 != king2 &&
        king[edges[i].second.second] == edges[i].second.second) {
      ans += edges[i].first;
      king[king2] = king1;
    }
  }
  for (int i = 0; i < king.size(); i++) {
    if (dsu(i, king) != dsu(0, king)) {
      return -1;
    }
  }
  return ans;
}
int main() {
  int n, q[100000], m, ans, a, b, c;
  vector<int> king;
  vector<pair<int, pair<int, int>>> edges;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
    king.push_back(i);
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    edges.push_back({c, {--a, --b}});
  }
  sort(edges.begin(), edges.end());
  ans = Kruskal(edges, king);
  cout << ans;
}
