#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0, num = 0;
int w[1005];
bool fa[1005];
struct request {
  int fr, to, cost;
} e[10005];
void search(int i) {
  int x = e[i].fr, y = e[i].to;
  if (!fa[y] and w[y] < w[x]) {
    fa[y] = true;
    ans += e[i].cost;
    num++;
  }
}
bool cmp(const request a, const request b) { return a.cost < b.cost; }
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> w[i];
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> e[i].fr >> e[i].to >> e[i].cost;
  }
  sort(e + 1, e + 1 + m, cmp);
  for (int i = 1; i <= m; ++i) {
    search(i);
    if (num == n - 1) break;
  }
  if (num == n - 1)
    cout << ans;
  else
    cout << -1;
  return 0;
}
