#include <bits/stdc++.h>
const int inf = (int)1e9;
using namespace std;
int qual[1111];
int edge[1111];
int main() {
  int n, m, i, j, max_q = 0, max_idx = 0, x, y, z;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &qual[i]);
  }
  for (i = 0; i <= n; i++) edge[i] = inf;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &x, &y, &z);
    edge[y] = min(edge[y], z);
  }
  int sum = 0, t = 0;
  for (i = 1; i <= n; i++) {
    if (edge[i] == inf) {
      t += 1;
    } else
      sum += edge[i];
  }
  if (t >= 2 || t == 0)
    printf("-1\n");
  else
    printf("%d\n", sum);
  return 0;
}
