#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";
ofstream fout(OUTPUT_FILE.c_str());
ifstream fin(INPUT_FILE.c_str());
void read_input() {}
const int MAXN = 1005;
int a[MAXN][MAXN];
int deg[MAXN];
int n, m;
void m_main() {
  cin >> n;
  int x;
  for (int i = 0; i < n; i++) cin >> x;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = INFI;
  cin >> m;
  for (int i = 0; i < m; i++) {
    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) {
    cout << -1 << endl;
    return;
  }
  long long res = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i]) {
      int t = INFI;
      for (int j = 1; j <= n; j++)
        if (a[j][i] != INFI) t = min(t, a[j][i]);
      res += t;
    }
  cout << res << endl;
}
int main() {
  read_input();
  m_main();
  return 0;
}
