ソースコード
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = (1e+9)+7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef pair<P, int> PP;
int dp[1 << 16];
int main(){
int n; cin >> n;
int t[16], a[16][16];
rep(i, n) {
cin >> t[i];
}
rep(i, n) {
rep(j, n) {
cin >> a[i][j];
}
}
fill(dp, dp + (1 << n), (int)MOD);
dp[0] = 0;
rep(i, (1 << n)-1) {
vector<int> used; vector<int> notu;
rep(j, n) {
if (i&(1 << j)) {
used.push_back(j);
}
else {
notu.push_back(j);
}
}
int len = notu.size(); int l = used.size();
rep(j, len) {
int v = notu[j];
int sum = 0;
rep(k, l) {
sum += a[used[k]][v];
}
dp[i | (1 << v)] = min(dp[i | (1 << v)], dp[i]+t[v] - sum);
}
}
cout << dp[(1 << n) - 1] << endl;
return 0;
}