#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > superd[1003];
int quali[1003];
int n;
int ap;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> quali[i];
  cin >> ap;
  for (int i = 0; i < ap; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    superd[v].push_back(pair<int, int>(c, u));
  }
  int c = 0;
  for (int i = 1; i <= n; i++) {
    if (superd[i].empty()) c++;
    sort((superd[i]).begin(), (superd[i]).end());
  }
  if (c > 1) return cout << -1 << "\n", 0;
  int cost = 1e9;
  for (int i = 1; i <= n; i++) {
    c = 0;
    for (int j = 1; j <= n; j++) {
      if (j == i) continue;
      if (superd[j].empty()) {
        c = 1e9;
        break;
      }
      c += superd[j][0].first;
    }
    cost = min(cost, c);
  }
  cout << cost << "\n";
}
