#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
const long long MOD = 1e9 + 7;
int N;
int q[1010];
int cost[1010];
int M;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> q[i];
  cin >> M;
  memset(cost, 0x3f, sizeof cost);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    cost[b] = min(cost[b], c);
  }
  long long tot = 0;
  int cnt = 0;
  for (int i = 1; i <= N; i++) {
    if (cost[i] == 0x3f3f3f3f)
      cnt++;
    else
      tot += cost[i];
  }
  cout << (cnt > 1 ? -1 : tot) << endl;
  return 0;
}
