#include <bits/stdc++.h>
using namespace std;
int a[1001], q[1001];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  while (cin >> n) {
    for (int i = (0); i < ((n)); ++i) cin >> q[i];
    for (int i = (0); i < ((n)); ++i) a[i] = 20000000;
    int m;
    cin >> m;
    for (int i = (0); i < ((m)); ++i) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--;
      if (q[x] > q[y] && a[y] > z) a[y] = z;
    }
    long long res = 0;
    int c = 0;
    for (int i = (0); i < ((n)); ++i) {
      if (a[i] == 20000000) {
        c++;
      } else
        res += a[i];
    }
    cout << (c == 1 ? res : -1) << endl;
  }
  return 0;
}
