#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, a, b, c, q[1005], i, j, m, na = 0, an[1005];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> q[i];
    an[i] = 1000000000007;
  }
  cin >> m;
  while (m--) {
    cin >> a >> b >> c;
    an[b] = min(an[b], c);
  }
  for (i = 1; i <= n; i++) {
    na += an[i];
  }
  if (na >= 2 * 1000000000007) {
    cout << -1;
  } else {
    cout << na - 1000000000007;
  }
  return 0;
}
