#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:836777216")
using namespace std;
int q[1111];
vector<pair<int, int> > v[1111];
int main() {
  int n;
  cin >> n;
  for (int i(0); i < (n); i++) scanf("%d", q + i);
  int m;
  cin >> m;
  for (int i(0); i < (m); i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    x--, y--;
    v[y].push_back(pair<int, int>(z, x));
  }
  int sum = 0;
  int num = 0;
  for (int i(0); i < (n); i++) {
    sort((v[i]).begin(), (v[i]).end());
    if (v[i].size())
      sum += v[i][0].first;
    else
      num++;
  }
  if (num == 1)
    cout << sum << endl;
  else
    cout << -1 << endl;
  return 0;
}
