#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 5;
int main() {
  int T = 1;
  while (T--) {
    vector<priority_queue<pair<int, int>, vector<pair<int, int>>,
                          greater<pair<int, int>>>>
        G;
    int n;
    scanf("%d", &n);
    G.resize(n + 1);
    int q[1005];
    for (int i = 1; i <= n; i++) {
      scanf("%d", &q[i]);
    }
    int m;
    scanf("%d", &m);
    int u, v, w;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &u, &v, &w);
      if (q[u] > q[v]) {
        G[v].push(make_pair(w, u));
      }
    }
    int edges = 0;
    long long ans = 0;
    for (int i = 1; i <= n && edges < n - 1; i++) {
      if (!G[i].empty()) {
        ans += G[i].top().first;
        edges++;
      }
    }
    if (edges != n - 1)
      printf("-1");
    else
      printf("%lld", ans);
  }
  return 0;
}
