#include <bits/stdc++.h>
const double PI = acos(-1.0);
using namespace std;
int mincost[1001];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int tmp;
    scanf("%d", &tmp);
  }
  for (int i = 1; i <= n; ++i) mincost[i] = 1000000001;
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    mincost[b] = min(mincost[b], c);
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (mincost[i] == 1000000001) ++cnt;
  if (cnt > 1)
    printf("-1\n");
  else {
    int ans = -1000000001;
    for (int i = 1; i <= n; ++i) ans += mincost[i];
    printf("%d\n", ans);
  }
  return 0;
}
