#include <bits/stdc++.h>
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T lcm(T a, T b) {
  return a / __gcd(a, b) * b;
}
template <class T>
T minimize(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
T maximize(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const long long mod = 1e9 + 7, oo = 1e12, N = 1005;
using namespace std;
long long n, m, capacity[N], a[N][N];
vector<long long> parent[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n;
  for (__typeof(n) i = (1); i <= (n); ++i) cin >> capacity[i];
  for (__typeof(n) i = (1); i <= (n); ++i) {
    for (__typeof(n) j = (1); j <= (n); ++j) a[i][j] = oo;
  }
  cin >> m;
  for (long long u, v, c; m > 0; m--) {
    cin >> u >> v >> c;
    if (capacity[u] <= capacity[v]) continue;
    if (a[u][v] == oo) parent[v].push_back(u);
    a[u][v] = min(a[u][v], c);
  }
  long long dem = 0;
  long long res = 0;
  for (__typeof(n) u = (1); u <= (n); ++u) {
    if (((long long)(parent[u]).size()) != 0) {
      long long pos = parent[u][0];
      for (long long i = 0; i < ((long long)(parent[u]).size()); ++i)
        if (a[pos][u] > a[parent[u][i]][u]) pos = parent[u][i];
      res += a[pos][u];
    } else {
      ++dem;
      if (dem > 1) return cout << -1, 0;
    }
  }
  cout << res << '\n';
  return 0;
}
