#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> G[int(3 * 1e5)];
bool visited[int(3 * 1e5)];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[b].push_back({c, a});
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (G[i].size() == 0) continue;
    sort(G[i].begin(), G[i].end());
    auto x = *min_element(G[i].begin(), G[i].end());
    ans += x.first;
    visited[i] = true;
  }
  int nodes_visited = 0;
  for (int i = 1; i <= n; i++)
    if (visited[i]) nodes_visited++;
  if (nodes_visited != n - 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
