#include <bits/stdc++.h>
#define ll long long
#define piint pair < int , int >
#define ALL(a) (a).begin() , (a).end
using namespace std;
const int maxn = 301 ;
int p[maxn+3][maxn + 3 ] ;
ll W = 0 ;
int n ;
bool water[maxn+3] ;
ll d[maxn + 3 ] ;
int w[maxn+3] ;
#define debug 0
ll PRIM ( int s ) {
// truong hop di tu s dau tien
// duyet moi dinh oke ?
// voi dinh nay ta tim duong di nho nhat
// noi voi dinh do
// neu ma dinh do xet roi , ta xem val cua duong di dinh do co be hon dat tai no khong
// neu co thi chu di
// con neu khong thi ta du nguyen ?
for (int i = 1 ; i <= n ; i ++ ){
d[i] = 1e17 ;
water[i] = 0 ;
}
d[s] = w[s] ; W = 0;
priority_queue < piint , vector < piint > ,greater < piint > > q ;
q.push({ d[s] , s }) ;
while (q.size()){
auto [du,u] = q.top() ; q.pop() ;
if (d[u] != du ) continue ;
if (water[u]) continue ;
W += d[u] ;
//else W += w[u] ;
if ( debug ){
cout << "U = : " << ' ' << u << '\n';
cout << "W = " << W << ": du" << ' ' << du <<" : w[u] " << w[u] << '\n' ;
}
d[u] = -1e17 ;
water[u] = 1 ;
for (int j = 1 ; j <= n ; j ++ ){
ll val = p[u][j] ;
if (d[j] > w[j] && w[j] != val ){
d[j] = w[j] ;
q.push( { d[j] , j }) ;
}
if (d[j] > val && w[j] != val && u != j ){
d[j] = val ;
q.push( { d[j] , j }) ;
}
}
}
return W ;
}
void sovle(){
cin >> n ;
for (int i = 1 ; i <= n ; i ++ ) {
cin >> w[i] ;
}
for (int i = 1 ; i <= n ; i ++ ){
for (int j = 1 ; j <= n ; j ++ ) cin >> p[i][j] ;
}
ll res = 1e17 ;
for (int i = 1 ; i <= n ; i ++ ){
res = min ( res , PRIM(i)) ;
cerr << "end\n" ;
}
cout << W ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0); cout.tie(0) ;
int q = 1 ;
// cin >> q ;
while ( q -- ) sovle() ;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3R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A7IHEucG9wKCkgOwogICAgICAgIGlmIChkW3VdICE9IGR1ICkgY29udGludWUgOwogICAgICAgIGlmICh3YXRlclt1XSkgY29udGludWUgOwogICAgICAgICBXICs9IGRbdV0gOwogICAgICAgIC8vZWxzZSBXICs9IHdbdV0gOwogICAgICAgIGlmICggZGVidWcgKXsKICAgICAgICAgICAgICAgIGNvdXQgPDwgIlUgPSA6ICIgPDwgJyAnIDw8IHUgPDwgJ1xuJzsKICAgICAgICAgICAgY291dCA8PCAiVyA9ICIgPDwgVyA8PCAiOiBkdSIgPDwgJyAnIDw8IGR1IDw8IiA6IHdbdV0gICIgPDwgd1t1XSA8PCAnXG4nIDsKICAgICAgICB9CiAgICAgICAgZFt1XSA9IC0xZTE3IDsKICAgICAgICB3YXRlclt1XSA9IDEgOwogICAgICAgIGZvciAoaW50IGogPSAgMSA7IGogPD0gbiA7IGogKysgKXsKICAgICAgICAgICAgbGwgdmFsID0gcFt1XVtqXSA7CiAgICAgICAgICAgICAgICBpZiAoZFtqXSA+IHdbal0gJiYgd1tqXSAhPSB2YWwgKXsKICAgICAgICAgICAgICAgIGRbal0gPSB3W2pdIDsKICAgICAgICAgICAgICAgIHEucHVzaCggeyBkW2pdICwgaiB9KSA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoZFtqXSA+IHZhbCAmJiB3W2pdICE9IHZhbCAgJiYgdSAhPSBqICl7CiAgICAgICAgICAgICAgICAgICAgZFtqXSA9IHZhbCA7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKCB7IGRbal0gLCBqIH0pIDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBXIDsKfQp2b2lkIHNvdmxlKCl7CiAgICBjaW4gPj4gbiA7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkgKysgKSB7CiAgICAgICAgY2luID4+IHdbaV0gOwogICAgfQogICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpICsrICl7CiAgICAgICAgZm9yIChpbnQgaiA9IDEgOyBqIDw9IG4gOyBqICsrICkgY2luID4+IHBbaV1bal0gOwogICAgfQogICAgbGwgcmVzID0gMWUxNyA7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkgKysgKXsKICAgIHJlcyA9IG1pbiAoIHJlcyAsIFBSSU0oaSkpIDsKICAgICAgICBjZXJyIDw8ICJlbmRcbiIgOwogICAgfQogICAgY291dCA8PCBXIDsKfQppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCkgOwogICAgY2luLnRpZSgwKTsgY291dC50aWUoMCkgOwogICAgaW50IHEgPSAxIDsKICAgIC8vIGNpbiA+PiBxIDsKICAgIHdoaWxlICggcSAtLSApIHNvdmxlKCkgOwogICAgcmV0dXJuIDA7Cn0K