#include <bits/stdc++.h>
using namespace std;
int temp, i, j, k, T;
long long pow_r(long long d, long long n) {
  long long x = 1ll;
  while (n > 0) {
    if (n & 1) x = (x * d);
    d = (d * d);
    n >>= 1;
  }
  return x;
}
int expe[1005], e, mxexp, key[1005];
int n, m, a, b, c, cand;
long long ans;
bool done[1005];
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    key[i] = (int)1e9;
  }
  for (i = 0; i < n; ++i) scanf("%d", &expe[i]);
  scanf("%d", &m);
  for (i = 0; i < m; ++i) {
    scanf("%d", &a);
    scanf("%d", &b);
    scanf("%d", &c);
    --a;
    --b;
    key[b] = min(c, key[b]);
  }
  c = 0;
  for (i = 0; i < n; ++i) {
    if (key[i] == (int)1e9)
      ++c;
    else
      ans += key[i];
  }
  if (c > 1)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}
