#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int v;
  for (int i = 0; i < n; i++) {
    cin >> v;
  }
  int weight[1000];
  for (int i = 0; i < n; i++) {
    weight[i] = -1;
  }
  int a, b, c;
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    b--;
    if (weight[b] == -1) {
      weight[b] = c;
    } else {
      weight[b] = min(weight[b], c);
    }
  }
  int numneg = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (weight[i] == -1) {
      numneg++;
    } else {
      sum += weight[i];
    }
  }
  if (numneg != 1) {
    cout << -1 << endl;
  } else {
    cout << sum << endl;
  }
  return 0;
}
