#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a, b, c;
  cin >> n;
  for (int i = 0; i <= n; ++i) cin >> m;
  vector<vector<pair<int, int> > > E(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    --a, --b;
    auto it = E[b].begin();
    for (; it != E[b].end(); ++it)
      if (it->first == a) {
        break;
      }
    if (it == E[b].end())
      E[b].push_back(make_pair(a, c));
    else
      it->second = min(it->second, c);
  }
  const int inf = 1 << 30;
  vector<int> used(n, 0);
  int ans = 0;
  for (int it = 1; it < n; ++it) {
    int v = -1;
    bool find = false;
    int min_c = inf;
    for (int u = 0; u < n && !find; ++u)
      if (!used[u])
        for (auto i = E[u].begin(); i != E[u].end() && !find; ++i)
          if (i->second < min_c) {
            min_c = i->second;
            v = u;
          }
    if (min_c == inf) {
      cout << -1 << endl;
      return 0;
    }
    used[v] = true;
    ans += min_c;
  }
  cout << ans << endl;
  return 0;
}
