#include <bits/stdc++.h>
using namespace std;
int a[1010];
int main() {
  ios::sync_with_stdio(false);
  int n, m, u, v, w;
  int i, maxi, maxq = 0, q;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> q;
    if (maxq < q) {
      maxq = q;
      maxi = i;
    }
    a[i] = 0x7FFFFFFF;
  }
  a[maxi] = 0;
  cin >> m;
  for (i = 0; i < m; ++i) {
    cin >> u >> v >> w;
    if (a[v] > w) a[v] = w;
  }
  int ans = 0;
  for (i = 1; i <= n; ++i) {
    if (a[i] == 0x7FFFFFFF) {
      ans = -1;
      break;
    }
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
