#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, m, a, b, c, arr[1001] = {}, cnt, sum, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &q);
  for (i = 1; i <= n; i++) arr[i] = 1000001;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    if (arr[b] > c) arr[b] = c;
  }
  cnt = 0;
  sum = 0;
  for (i = 1; i <= n; i++) {
    if (arr[i] == 1000001)
      cnt++;
    else
      sum += arr[i];
  }
  printf("%d\n", cnt == 1 ? sum : -1);
  return 0;
}
