#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b;
  int w;
  bool operator<(edge const& e) const { return w < e.w; }
};
int n, m;
int p[1500];
bool has[1500];
edge e[10 * 1500];
int ans;
int get(int a) { return (a == p[a] ? a : p[a] = get(p[a])); }
void unite(int a, int b) { p[get(a)] = p[get(b)]; }
int main() {
  cin >> n;
  for (int q, i = 0; i < n; ++i) {
    cin >> q;
    p[i] = i;
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> e[i].a >> e[i].b >> e[i].w;
    --e[i].a;
    --e[i].b;
  }
  sort(e, e + m);
  for (int i = 0; i < m; ++i) {
    if (!has[e[i].b]) {
      if (get(e[i].a) != get(e[i].b)) {
        unite(e[i].a, e[i].b);
        ans += e[i].w;
        has[e[i].b] = true;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (i == p[i]);
  }
  cout << (cnt > 1 ? -1 : ans);
  return 0;
}
