#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int q[1001];
int best[1001];
int main() {
  int n;
  cin >> n;
  bool awlmra = false;
  memset(best, -1, sizeof(best));
  for (int i = 1; i <= n; i++) {
    cin >> q[i];
  }
  int m, a, b, c;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    if (best[b] == -1)
      best[b] = c;
    else
      best[b] = min(best[b], c);
  }
  for (int i = 1; i <= n; i++) {
    if (best[i] == -1 && !awlmra) {
      awlmra = true;
    } else if (best[i] == -1 && awlmra) {
      cout << -1 << endl;
      return 0;
    } else {
      ans += best[i];
    }
  }
  cout << ans << endl;
  return 0;
}
