#include <bits/stdc++.h>
using namespace std;
long long int n, i, j, ans, m, vis[100005], q[100005];
struct tree {
  long long int a;
  long long int b;
  long long int c;
};
tree node[100005];
bool cmp(tree aa, tree ab) {
  if (aa.c < ab.c) return 1;
  return 0;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> q[i];
  }
  cin >> m;
  j = 0;
  for (i = 0; i < m; i++) {
    cin >> node[j].a >> node[j].b >> node[j].c;
    if (q[node[j].a] <= q[node[j].b]) continue;
    j++;
  }
  sort(node, node + j, cmp);
  long long int ctr = 0;
  for (i = 0; i < j; i++) {
    if (vis[node[i].b]) continue;
    vis[node[i].b] = 1;
    ans += node[i].c;
    ctr++;
  }
  if (ctr != n - 1)
    cout << "-1";
  else
    cout << ans;
  return 0;
}
