#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int maxn = 1111;
const int maxm = 1e6 + 10;
int arr[maxn];
int in[maxn];
int n, m;
void init() {
  for (int i = 0; i < (maxn); i++) {
    in[i] = INF;
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
  scanf("%d", &m);
  for (int i = 0; i < (m); i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (arr[u] <= arr[v]) continue;
    in[v] = min(in[v], w);
  }
  int cnt = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (in[i] == INF) {
      cnt++;
      if (cnt >= 2) {
        printf("-1\n");
        return 0;
      }
      continue;
    }
    ans += in[i];
  }
  printf("%I64d\n", ans);
  return 0;
}
