//2018 农场缺水
//使用prim求最小生成树,开始的起点为挖井处
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100;
int g[N][N];
int dist[N];
int w[N];
int n;
bool st[N];
int INF = 0x3f;
int prim()
{
memset(dist, 0x3f, sizeof dist);
int res = 0;//最小生成树所有边权和,即连接所有田地所需最少金额
for (int i = 0; i < n; i++)
{
int t = -1;
//找到当前集合外距离集合最近的点
for (int j = 1; j <= n; j++)
{
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j; //此后t存的一直是当前距离集合最近的点
}
if (i && dist[t] == INF)
return INF; //说明当前离集合最近的点的距离为正无穷,即不存在最小生成树
if (i)
{
res += dist[t];
}
for (int j = 1; j <= n; j++)
dist[j] = min(dist[j], g[t][j]); //用t更新其他点到集合的距离
st[t] = true;
}
return res;
}
int main()
{
memset(g, 0x3f, sizeof g);
cin >> n;
getchar();
for (int i = 0; i < n; i++)
cin >> w[i];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cin >> g[i][j];
}
int t = prim();
sort(w, w + n);
cout << t + w[0] << endl;
return 0;
}