#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     int n;     cin >> n;     vector<int> q(n);     for (int i = 0; i < n; ++i) cin >> q[i];     vector<int> cost(n, -1);     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       int a, b, c;       cin >> a >> b >> c;       if (cost[b - 1] == -1 || cost[b - 1] > c) {         cost[b - 1] = c;       }     }     int ans = 0, head = 0;     for (int i = 0; i < n; ++i) {       if (cost[i] != -1)         ans += cost[i];       else         head++;     }     if (head > 1)       cout << -1 << endl;     else {       if (!head) {         int maxx = cost[0];         for (int i = 1; i < n; ++i) maxx = max(maxx, cost[i]);         ans -= maxx;       }       cout << ans << endl;     }     return 0;   }   