#include <bits/stdc++.h>   using namespace std;   int n, a[200005];   void slove() {     int temp, u, v, w, m;     for (int i = 1; i <= n; i++) cin >> temp, a[i] = 0x3f3f3f3f;     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> u >> v >> w;       if (a[v] > w) a[v] = w;     }     int flag = 1, k = 1;     int ans = 0;     for (int i = 1; i <= n; i++) {       if (a[i] == 0x3f3f3f3f && k) {         ans -= 0x3f3f3f3f;         k = 0;       } else if (a[i] == 0x3f3f3f3f) {         flag = 0;         break;       }       ans += a[i];     }     if (flag)       cout << ans <<   n ;     else       cout << -1 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     slove();     return 0;   }   