#include <bits/stdc++.h>
using namespace std;
int main() {
  int employ[1000];
  for (int i = 0; i < 1000; i++) employ[i] = -1;
  int n, m, max = 0, maxi;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    if (tmp >= max) {
      max = tmp;
      maxi = i;
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    b--;
    if (employ[b] == -1 || employ[b] > c) employ[b] = c;
  }
  m = 0;
  for (int i = 0; i < n; i++) {
    if (i != maxi && employ[i] == -1) {
      cout << -1 << endl;
      return 0;
    } else if (i != maxi)
      m += employ[i];
  }
  cout << m << endl;
}
