#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
set<int> st[maxn];
int n, m, s, p = -1;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int q;
    cin >> q;
    if (q > s) {
      p = i;
      s = q;
    }
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    st[b].insert(c);
  }
  s = 0;
  for (int i = 1; i <= n; i++) {
    if (i != p && !st[i].size()) {
      cout << "-1" << endl;
      return 0;
    }
    if (i != p) s += *st[i].begin();
  }
  cout << s << endl;
  return 0;
}
