#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1001];
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  int n, w;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
  }
  cin >> w;
  for (int i = 0; i < w; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[b].push_back(c);
  }
  int ans = 0, mm = 0, m;
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty())
      mm++;
    else {
      m = INT_MAX;
      for (auto x : adj[i]) {
        m = min(x, m);
      }
      ans += m;
    }
    if (mm > 1) {
      cout << -1;
      return 0;
    }
  }
  cout << ans;
  return 0;
}
