#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int main() {
  long n, q[10000], a[10000], b[10000], c[10000], m, min_st[10000];
  int IsUch[10000], IsValid = 0, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> q[i];
    IsUch[i] = 0;
    min_st[i] = LONG_MAX - 1;
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    if (q[a[i] - 1] > q[b[i] - 1]) {
      IsUch[b[i] - 1] = 1;
      min_st[b[i] - 1] = min(min_st[b[i] - 1], c[i]);
    }
  }
  for (i = 0; i < n; i++) IsValid += (IsUch[i] == 0 ? 1 : 0);
  if (IsValid > 1) {
    cout << -1 << endl;
    return 0;
  }
  long min_sum = 0;
  for (i = 0; i < n; i++)
    min_sum += min_st[i] != (LONG_MAX - 1) ? min_st[i] : 0;
  cout << min_sum << endl;
  return 0;
}
