#include <bits/stdc++.h>
using namespace std;
const long long MAX = LLONG_MAX, MOD = 1000000007, MODA = 1e9 - 1;
const long long MIN = LLONG_MIN;
const long double PI = 3.1415926535;
const long long N = 100009;
bool visited[N] = {0};
vector<long long> adj[N];
void dfs(long long node) {
  if (!visited[node]) {
    visited[node] = 1;
    for (auto u : adj[node]) {
      dfs(u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> v;
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      v.push_back({x, i + 1});
    }
    sort(v.begin(), v.end());
    vector<pair<long long, long long>> master[n + 1];
    long long m;
    cin >> m;
    for (long long i = 0; i < m; i++) {
      long long a, b, c;
      cin >> a >> b >> c;
      master[b].push_back({c, a});
    }
    long long ans = 0;
    for (long long i = 0; i < n - 1; i++) {
      if (master[v[i].second].size() == 0) break;
      sort(master[v[i].second].begin(), master[v[i].second].end());
      ans += master[v[i].second][0].first;
      adj[v[i].second].push_back(master[v[i].second][0].second);
      adj[master[v[i].second][0].second].push_back(v[i].second);
    }
    dfs(v[n - 1].second);
    bool flag = 0;
    for (long long i = 1; i < n + 1; i++) {
      if (!visited[i]) {
        flag = 1;
        break;
      }
    }
    if (flag)
      cout << -1;
    else
      cout << ans;
  }
  return 0;
}
