#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  if (fopen("input.in", "r")) {
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
  }
  int n;
  cin >> n;
  int qs[n];
  for (int i = 0; i < n; i++) {
    cin >> qs[i];
  }
  long long c = 0;
  vector<int> os[n];
  int a;
  cin >> a;
  for (int i = 0; i < a; i++) {
    int t, y, u;
    cin >> t >> y >> u;
    os[y - 1].push_back(u);
  }
  int f = 0;
  for (int i = 0; i < n; i++) {
    int m = 1e7;
    for (int j = 0; j < os[i].size(); j++) {
      if (os[i][j] < m) m = os[i][j];
    }
    if (m != 1e7)
      c += m;
    else
      f++;
    if (f == 2) {
      cout << -1;
      return 0;
    }
  }
  cout << c;
}
