#include <bits/stdc++.h>
using namespace std;
const int INFI = 2000000000;
const int goX[4] = {-1, 0, 1, 0};
const int goY[4] = {0, 1, 0, -1};
const string PROBLEM_NAME = "test";
const string INPUT_FILE = PROBLEM_NAME + ".in";
const string OUTPUT_FILE = PROBLEM_NAME + ".out";
void read_input() {}
const int MAXN = 1005;
int n;
int Q[MAXN];
int a[MAXN][MAXN];
int m;
int deg[MAXN];
long long solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> Q[i];
  cin >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = INFI;
  for (int k = 0; k < m; k++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x][y] = min(a[x][y], z);
    deg[y]++;
  }
  int mcount = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 0) mcount++;
  if (mcount != 1) return -1;
  long long res = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i]) {
      int _min = INFI + 1;
      for (int j = 1; j <= n; j++) _min = min(_min, a[j][i]);
      res += _min;
    }
  return res;
}
void m_main() { cout << solve() << endl; }
int main() {
  read_input();
  m_main();
  return 0;
}
