#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, c = getchar(), f = 0;
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
    ;
  return f ? -x : x;
}
const int N = 1000 + 10, oo = 1e9;
int n, q[N], c[N], ans, cnt;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) q[i] = read(), *q = max(q[i], *q), c[i] = oo;
  for (int i = 1; i <= n; i++)
    if (q[i] == *q) c[i] = 0, cnt++;
  if (cnt > 1) return puts("-1"), 0;
  for (int m = read(); m--;) {
    int u = read(), v = read(), w = read();
    c[v] = min(c[v], w);
  }
  for (int i = 1; i <= n; i++)
    if (c[i] == oo)
      return puts("-1"), 0;
    else
      ans += c[i];
  printf("%d\n", ans);
}
