#include <bits/stdc++.h>
using namespace std;
int q[1005];
vector<int> b[1005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
  int m;
  scanf("%d", &m);
  while (m--) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (q[u] <= q[v]) continue;
    b[v].push_back(w);
  }
  int ans = 0, cnt = -1;
  for (int i = 1; i <= n; i++)
    if (b[i].size())
      ans += *min_element(b[i].begin(), b[i].end());
    else {
      cnt++;
      if (cnt) break;
    }
  cnt ? printf("-1") : printf("%d", ans);
}
