#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
long long modpow(long long a, long long b) {
  if (!b) return 1;
  a %= mod;
  return modpow(a * a % mod, b / 2) * (b & 1 ? a : 1) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int p[n];
  for (int i = 0; i < n; i++) cin >> p[i];
  int best = 0;
  for (int i = 0; i < n; i++)
    if (p[best] < p[i]) best = i;
  bool vis[n];
  memset(vis, false, sizeof(vis));
  int dist[n];
  for (int i = 0; i < n; i++) dist[i] = int(1e9 + 1);
  dist[best] = 0;
  int m;
  cin >> m;
  vector<int> adj[n];
  int U[m], V[m], W[m];
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    adj[b].push_back(i);
    U[i] = a, V[i] = b, W[i] = c;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == best) continue;
    if (adj[i].empty() && i != best) {
      cout << -1 << endl;
      exit(0);
    }
    int mini = int(1e9);
    for (int e : adj[i]) mini = min(mini, W[e]);
    ans += mini;
  }
  cout << ans << endl;
  return 0;
}
