#include <bits/stdc++.h>
using namespace std;
vector<long long> papa;
vector<long long> cost;
void init(long long n) {
  for (int i = 0; i < n; i++) {
    papa.push_back(i);
    cost.push_back(INT_MAX);
  }
}
long long Find(long long j) {
  while (papa[j] != j) {
    j = papa[j];
  }
  return j;
}
void Union(long long b, long long a, long long c) {
  papa[a] = Find(b);
  cost[a] = min(c, cost[a]);
}
int main(int argc, char const *argv[]) {
  long long n;
  cin >> n;
  init(n);
  vector<long long> q(n);
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  long long m;
  cin >> m;
  long long x, y, c;
  while (m--) {
    cin >> x >> y >> c;
    x--, y--;
    Union(x, y, c);
  }
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (papa[i] == i) cnt++;
  }
  if (cnt != 1) {
    cout << -1;
  } else {
    long long co = INT_MAX * -1;
    for (int i = 0; i < n; i++) {
      co += cost[i];
    }
    cout << co;
  }
  return 0;
}
