#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int a[N];
int vis[N];
int n;
struct edge {
  int from, to, w;
  bool operator<(const edge& e) const { return w > e.w; }
};
vector<edge> adj[N];
priority_queue<edge> q;
int MST() {
  int mstCost = 0;
  int edges = 0;
  while (!q.empty()) {
    edge e = q.top();
    q.pop();
    if (vis[e.to]) continue;
    vis[e.to] = 1;
    mstCost += e.w;
    ++edges;
    for (int j = 0; j < adj[e.to].size(); j++) {
      edge ne = adj[e.to][j];
      if (!vis[ne.to]) q.push(ne);
    }
  }
  if (edges != n - 1) return -1;
  return mstCost;
}
int main() {
  int m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    --u, --v;
    adj[u].push_back(edge{u, v, w});
    q.push(edge{u, v, w});
  }
  printf("%d\n", MST());
}
