#include <bits/stdc++.h>
using namespace std;
int n, m, a[1003], q[1005], x, y, c, sum = 0, mx, i1;
vector<int> v;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i] = 1e7;
  }
  for (int i = 1; i <= n; i++) {
    cin >> q[i];
    if (q[i] > mx) {
      mx = q[i];
      i1 = i;
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> c;
    a[y] = min(a[y], c);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] <= 1e6) sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > 1e6) {
      v.push_back(i);
    }
  }
  if (v.size() > 1) {
    cout << -1;
    return 0;
  }
  int k = v[0];
  if (k == i1) {
    cout << sum;
  } else
    cout << -1;
}
