#include <bits/stdc++.h>
using namespace std;
const int maxn = 1111;
int A[maxn][maxn], dis[maxn], D[maxn];
int main() {
  int N, M;
  scanf("%d", &N);
  int Mx = 0, sta = 1;
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &M);
    if (M > Mx) Mx = M, sta = i;
  }
  scanf("%d", &M);
  memset(A, 0x3f, sizeof(A));
  for (int i = 1; i <= M; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (A[a][b] == A[0][0]) ++D[b];
    A[a][b] = min(A[a][b], c);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[sta] = 0;
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    int Mn = dis[0], id = 0;
    for (int j = 1; j <= N; ++j)
      if (!D[j] && dis[j] < Mn) Mn = dis[j], id = j;
    if (!id) {
      puts("-1");
      return 0;
    }
    ans += Mn, D[id] = -1;
    for (int j = 1; j <= N; ++j)
      if (A[id][j] != A[0][0] && D[j]) --D[j], dis[j] = min(dis[j], A[id][j]);
  }
  printf("%d", ans);
  return 0;
}
