#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long double eps = 1e-6;
const int maxn = 1024;
pair<int, int> q[maxn];
struct edge {
  int from, to, cost;
};
vector<edge> edges;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> q[i].first;
    q[i].first = -q[i].first;
    q[i].second = i;
  }
  sort(q, q + n);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int q, w, ee;
    cin >> q >> w >> ee;
    edge e;
    e.from = q - 1;
    e.to = w - 1;
    e.cost = ee;
    edges.push_back(e);
  }
  int res = 0;
  for (int i = 1; i < n; i++) {
    int mn = inf;
    for (int j = 0; j < edges.size(); j++)
      if (edges[j].to == q[i].second) {
        mn = min(mn, edges[j].cost);
      }
    if (mn == inf) {
      puts("-1");
      return 0;
    }
    res += mn;
  }
  cout << res << endl;
  return 0;
}
