#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, md = 1000000007;
int n, m, x;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", &x);
  cin >> m;
  vector<int> deg(n + 1);
  vector<int> best(n + 1, md);
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    ++deg[v];
    best[v] = min(best[v], w);
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (deg[i] == 0) ++cnt;
  }
  if (cnt > 1)
    puts("-1");
  else {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (deg[i]) ans += best[i];
    }
    cout << ans << endl;
  }
}
