#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n;
  cin >> n;
  vector<long long int> q(n + 1);
  for (long long int i = 1; i <= n; i++) cin >> q[i];
  long long int m;
  cin >> m;
  vector<pair<long long int, long long int>> adj[n + 1];
  for (long long int i = 1; i <= m; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    adj[b].push_back({c, a});
  }
  long long int cnt = 0;
  for (long long int i = 1; i <= n; i++) {
    sort(adj[i].begin(), adj[i].end());
    if (adj[i].size() > 0) cnt++;
  }
  if (cnt == n - 1) {
    long long int ans = 0;
    for (long long int i = 1; i <= n; i++) {
      if (adj[i].size() > 0) ans += adj[i][0].first;
    }
    cout << ans;
  } else {
    cout << -1;
  }
}
