#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> *q;
vector<int> *dprnt;
int x, y, d;
int main() {
  cin >> n;
  q = new pair<int, int>[n];
  dprnt = new vector<int>[n];
  for (int i = 0; i < n; i++) {
    cin >> q[i].first;
    q[i].second = i;
  }
  sort(q, q + n);
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> d;
    dprnt[--y].push_back(d);
  }
  int sum = 0;
  dprnt[q[n - 1].second].push_back(0);
  for (int i = n - 1; i >= 0; i--) {
    if (dprnt[q[i].second].empty()) {
      cout << -1 << endl;
      return 0;
    } else {
      int dd = dprnt[q[i].second][0];
      for (int j = 1; j < (int)(dprnt[q[i].second].size()); j++) {
        if (dd > dprnt[q[i].second][j]) dd = dprnt[q[i].second][j];
      }
      sum += dd;
    }
  }
  cout << sum << endl;
}
