#include <bits/stdc++.h>
using namespace std;
struct node {
  int to, w;
  node() {}
  node(int to, int w) {
    this->to = to;
    this->w = w;
  }
};
int n, m, w[1005], ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    int tmp;
    cin >> tmp;
    w[i] = 1e9;
  }
  cin >> m;
  for (long long i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    ::w[v] = min(::w[v], w);
  }
  bool flag = 0;
  for (long long i = 1; i <= n; ++i) {
    if (w[i] != 1e9) {
      ans += w[i];
    } else {
      if (flag) {
        puts("-1"), exit(0);
        ;
      }
      flag = 1;
    }
  }
  cout << ans << endl;
}
