#include <bits/stdc++.h>
using namespace std;
int ma[1001][1001];
int main() {
  int q[1001], n, m, tmp1, tmp2, tmp3, k1 = 0, c = 0;
  bool k2;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> tmp1;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> tmp1 >> tmp2 >> tmp3;
    if (tmp3 == 0) tmp3 = -1;
    if (!ma[tmp2][tmp1] || tmp3 < ma[tmp2][tmp1]) ma[tmp2][tmp1] = tmp3;
  }
  for (int i = 1; i <= n; i++) {
    int mc = 10000001;
    k2 = 1;
    for (int q = 1; q <= n; q++)
      if (ma[i][q]) {
        k2 = 0;
        mc = min(mc, ma[i][q]);
      }
    if (mc == -1) mc = 0;
    if (k2)
      k1++;
    else
      c += mc;
  }
  if (k1 > 1)
    cout << -1 << endl;
  else
    cout << c << endl;
  return 0;
}
