#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, c;
} q[10005];
int cmp(node x, node y) { return x.c < y.c; }
int n, m, ans, num, a[1005];
bool fa[1005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> q[i].x >> q[i].y >> q[i].c;
  sort(q + 1, q + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    int x = q[i].x, y = q[i].y, c = q[i].c;
    if (!fa[y] && a[x] > a[y]) {
      fa[y] = true;
      ans += c;
      num++;
    }
    if (num == n - 1) break;
  }
  if (num == n - 1)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
