#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> q;
  for (long long i = 0; i < n; i++) {
    long long p;
    cin >> p;
    q.push_back(1e9);
  }
  q.push_back(1e9);
  long long m;
  cin >> m;
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    q[b] = min(q[b], c);
  }
  long long cnt = 0, sum = 0;
  for (long long i = 1; i <= n; i++) {
    if (q[i] != 1e9) {
      cnt++;
      sum = sum + q[i];
    }
  }
  if (cnt + 1 == n)
    cout << sum << endl;
  else
    cout << -1 << endl;
}
