#include <bits/stdc++.h>
using namespace std;
long long n, a, b, c, res, i, m;
const int DiM = 1000020;
long long ans[DiM];
const int inf = (1 << 30);
int ma, boss;
int main() {
  cin >> n;
  for (i = 1; i <= n; ++i) {
    ans[i] = inf;
  }
  int rahat;
  for (i = 1; i <= n; ++i) {
    cin >> rahat;
    if (rahat > ma) {
      ma = rahat;
      boss = i;
    }
  }
  cin >> m;
  for (i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    ans[b] = min(ans[b], c);
  }
  for (i = 1; i <= n; i++) {
    if (i == boss) continue;
    if (ans[i] == inf) {
      cout << -1;
      return 0;
    }
    res += ans[i];
  }
  cout << res << endl;
}
