#include <bits/stdc++.h>
using namespace std;
int N, M;
int Q[1000];
pair<int, pair<int, int> > app[10000];
bool marked[1000];
int solve() {
  sort(app, app + M);
  int root = 0;
  bool bad = false;
  for (int i = 1; i < N; i++) {
    if (Q[root] < Q[i]) {
      root = i;
      bad = false;
    } else if (Q[root] == Q[i]) {
      bad = true;
    }
  }
  if (bad) {
    return -1;
  }
  int cost = 0;
  fill(marked, marked + N, false);
  marked[root] = true;
  int X = 1;
  for (int i = 0; i < N; i++) {
    if (i != root) {
      bool found = false;
      for (int j = 0; j < M; j++) {
        if (app[j].second.second == i) {
          cost += app[j].first;
          found = true;
          break;
        }
      }
      if (!found) {
        return -1;
      }
    }
  }
  return cost;
}
inline void init() {}
int main() {
  init();
  while (cin >> N) {
    for (int i = 0; i < N; i++) {
      cin >> Q[i];
    }
    cin >> M;
    for (int i = 0; i < M; i++) {
      cin >> app[i].second.first >> app[i].second.second >> app[i].first;
      app[i].second.first--;
      app[i].second.second--;
    }
    cout << solve() << endl;
  }
  return 0;
}
