#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int ans[1001], maxa, maxq;
bool exista = true;
int main() {
  int n, m, a, b, c;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ans[i] = N;
    cin >> a;
    if (a > maxa) {
      maxa = a;
      maxq = i;
    }
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    ans[b] = min(ans[b], c);
  }
  int s = 0;
  for (int i = 1; i <= n && exista; i++) {
    if (i == maxq) i++;
    if (ans[i] == N) {
      exista = false;
    } else {
      s += ans[i];
    }
  }
  if (exista) {
    cout << s;
  } else {
    cout << "-1";
  }
  return 0;
}
