#include <bits/stdc++.h>
const long long MaxN = 1e3 + 10;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
using namespace std;
long long i, j, k, m, n;
long long res[MaxN], q[MaxN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> q[i];
  for (int i = 1; i <= n; i++) res[i] = INF;
  cin >> m;
  while (m--) {
    cin >> i >> j >> k;
    if (q[i] > q[j] && res[j] > k) res[j] = k;
  }
  k = m = 0;
  for (int i = 1; i <= n; i++)
    if (res[i] == INF)
      k++;
    else
      m += res[i];
  if (k > 1) m = -1;
  cout << m;
  return 0;
}
