#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[1001], b[1001], i, j;
  int t;
  cin >> t;
  int num;
  for (i = 1; i <= t; i++) {
    cin >> a[i];
    if (i == 1)
      num = a[i];
    else if (a[i] > num)
      num = a[i];
    b[i] = 1000001;
  }
  int n;
  cin >> n;
  while (n--) {
    int x, y, z;
    cin >> x >> y >> z;
    if (a[x] > a[y]) {
      if (b[y] > z) b[y] = z;
    }
  }
  bool m = true;
  int sum = 0;
  int z = 0;
  for (i = 1; i <= t; i++) {
    if (z == 0 && a[i] == num) {
      z = 1;
      continue;
    }
    if (b[i] == 1000001) {
      m = false;
      break;
    } else
      sum += b[i];
  }
  if (!m)
    cout << -1 << endl;
  else
    cout << sum << endl;
  return 0;
}
