#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n;
  vector<int> q(n);
  for (int i = 0; i < n; i++) cin >> q[i];
  vector<int> c(n, INT_MAX);
  cin >> m;
  for (int i = 0; i < m; i++) {
    int sp, sb, p;
    cin >> sp >> sb >> p;
    c[sb - 1] = min(c[sb - 1], p);
  }
  int co = 0;
  for (int i : c)
    if (i == INT_MAX) co++;
  if (co > 1) {
    cout << -1 << endl;
    return 0;
  }
  int p = 0;
  for (int i : c)
    if (i != INT_MAX) p += i;
  cout << p << endl;
  return 0;
}
