#include <bits/stdc++.h>
using namespace std;
int d8x[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int d8y[] = {0, 1, 1, 1, 0, -1, -1, -1};
struct info {
  int Ai, Bi, Ci;
  bool operator<(const info& p) const {
    if (Ci != p.Ci) return Ci < p.Ci;
    return Bi < p.Bi;
  }
} Q[1000007];
bool mark[1000007];
int quali[1000007];
int main(void) {
  int num;
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) scanf("%d", &quali[i]);
  int app;
  scanf("%d", &app);
  for (int i = 0; i < app; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    Q[i].Ai = x;
    Q[i].Bi = y;
    Q[i].Ci = z;
  }
  sort(Q, Q + app);
  long long ans = 0;
  int counter = 0;
  for (int i = 0; i < app; i++) {
    int value = Q[i].Ci;
    int a = Q[i].Ai;
    int b = Q[i].Bi;
    if (!mark[b] && quali[a] > quali[b]) {
      mark[b] = true;
      ans += value;
      counter++;
      if (counter == num - 1) break;
    }
  }
  if (counter == num - 1)
    printf("%lld\n", ans);
  else
    printf("-1");
  return 0;
}
