Problem:
最短ルート
English text is not available in this practice contest.
あなたの所属する知的プログラミングサークル (通称 Intelligent Clever Programming Circle, ICPC)では，今大掃除の最中である．
部室には，歴代の先輩たちの残していったさまざまなアイテムが所狭しと放置されている．
あなたが棚を整理していると，奥に大量のレトロゲームが封印されているのを発見した．
その中のいくつかに見覚えがあったあなたは，久々にそのゲームを遊んでみることにした．
見つけたゲームの詳細は次の通りである．
このゲームには1番から
N
番までの
N
個のステージがあり，任意の順に攻略することができる．
また，1から
N
まで番号付けられた装備があり，それらの装備を使用することでステージの攻略時間を短縮することができる．
ゲームを始めた段階では装備は一つも持っていないが，
i
番のステージを攻略すると
i
番の装備を入手することができ，一度入手した後は何度でも使用できる．
装備は一ステージで一種類だけしか使用することができないが，異なるステージで同じ装備を使用することはできる．
あなたは昔このゲームをクリアしたことがあるので，各装備に対して，その装備を使用したときに各ステージを攻略するのにかかる時間をすべて把握している．
ただ普通にクリアするだけではつまらないので，あなたは全てのステージを攻略し終わるまでにかかる時間を最小にしようと考えた．
そのため，あなたはICPCでの経験を生かして，各情報を与えたときに全てのステージを攻略し終わるまでにかかる時間の最小値を計算するプログラムを書くことにした．
Input
入力は複数のデータセットからなる．
入力の終わりは1つのゼロからなる行によって与えられる．
各データセットは一つのゲームに関する情報を表し，その形式は以下の通りである．
N
t
10
t
11
...
t
1N
t
20
t
21
...
t
2N
...
t
N0
t
N1
...
t
NN
データセットの最初の行は1つの整数
N
からなり，ステージの数を表す．
続く
N
行は
N+1
個の整数からなり，ステージの攻略時間を表す．
t
i0
は
i
番のステージを装備なしで攻略するのにかかる時間である．
t
i j
(
j
> 0)は
i
番のステージを
j
番の装備で攻略するのにかかる時間である．
それぞれの値は次の制約を満たしている．
1 ≤
N
≤ 16
1 ≤
t
i j
≤ 100,000
Output
それぞれのデータセットに対して，全てのステージを攻略し終わるまでにかかる時間の最小値を表す整数を1行に出力せよ．出力行にはこの数値以外の文字を含んではならない．
Sample Input
3
100 100 100 100
100 1 100 100
100 1 1 100
3
100 100 1 100
200 102 100 102
100 100 1 100
7
100 100 60 60 70 70 70 90
100 50 100 55 45 45 55 44
100 50 60 100 51 50 55 30
100 70 10 20 1 10 10 40
200 90 10 30 10 10 10 30
150 200 12 1 11 11 1 30
10000 1200 1100 1100 1200 1200 1090 1
0
Output for the Sample Input
102
202
1301

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    while(cin >> N, N) {
        vector<vector<int>> t(N, vector<int>(N + 1));
        for(int i = 0; i < N; ++i) {
            for(int j = 0; j < N + 1; ++j) {
                cin >> t[i][j];
            }
        }
        vector<int> dp(1 << N, 1e9);
        dp[0] = 0;
        for(int S = 0; S < (1 << N); ++S) {
            for(int i = 0; i < N; ++i) {
                if((S >> i) & 1) {
                    continue;
                }
                int mi = t[i][0];
                for(int j = 0; j < N; ++j) {
                    if((S >> j) & 1) {
                        mi = min(mi, t[i][j + 1]);
                    }
                }
                dp[S | (1 << i)] = min(dp[S | (1 << i)], dp[S] + mi);
            }
        }
        cout << dp[(1 << N) - 1] << endl;
    }
}
accuracy: 1
time_ms: 340
memory_kb: 3588
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

void init() {}

ll n;
vector<vector<ll>> v;
void input() {
    cin >> n;
    v = vector<vector<ll>>(n, vector<ll>(n+1, 0));

    for(auto &x : v)
        for(auto &y : x)
            cin >> y;
}
void solve() {
    vector<ll> dp((1 << n), 1e18);
    dp[0] = 0;

    for(int i = 0; i < (1 << n); i++) {
        vector<ll> nxt;
        vector<ll> already;
        for(int j = 0; j < n; j++) {
            if((i & (1 << j)) == 0) {
                nxt.push_back(j);
            } else {
                already.push_back(j);
            }
        }

        for(auto &x : nxt) {
            ll add = v[x][0];
            for(auto &y : already) {
                chmin(add, v[x][y + 1]);
            }
            chmin(dp[i | (1 << x)], dp[i] + add);
        }
    }
    cout << dp.back() << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // init();
    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 3820
final_rank: 4

code_snippet: //line 2 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/std.hpp"
#include <bits/stdc++.h>
#ifndef LOCAL_TEST
#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif // LOCAL_TEST
using namespace std;
using std::cout;
// shorten typenames
using ll = long long;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vi = vector<int>;  using vvi = vector<vi>; using vvvi = vector<vvi>;
using vl = vector<ll>;  using vvl = vector<vl>; using vvvl = vector<vvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
using vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>;
template<typename T> vector<vector<T>> vv(int h, int w, T val = T()) { return vector(h, vector<T>(w, val)); }
template<typename T> vector<vector<vector<T>>> vvv(int h1, int h2, int h3, T val = T()) { return vector(h1, vector(h2, vector<T>(h3, val))); }
template<typename T> vector<vector<vector<vector<T>>>> vvvv(int h1, int h2, int h3, int h4, T val = T()) { return vector(h1, vector(h2, vector(h3, vector<T>(h4, val)))); }
template <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>;
// define CONSTANTS
constexpr double PI = 3.14159265358979323;
constexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL;
float EPS = 1e-8; double EPSL = 1e-10;
template<typename T> bool eq(const T x, const T y) { return x == y; }
template<> bool eq<double>(const double x, const double y) { return (abs(x - y) < EPSL * x || abs(x - y) < EPSL); }
template<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS * x; }
template<typename T> bool neq(const T x, const T y) { return !(eq<T>(x, y)); }
template<typename T> bool ge(const T x, const T y) { return (eq<T>(x, y) || (x > y)); }
template<typename T> bool le(const T x, const T y) { return (eq<T>(x, y) || (x < y)); }
template<typename T> bool gt(const T x, const T y) { return !(le<T>(x, y)); }
template<typename T> bool lt(const T x, const T y) { return !(ge<T>(x, y)); }
constexpr int MODINT998244353 = 998244353;
constexpr int MODINT1000000007 = 1000000007;
// fasten io
struct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan;
// define macros
#define all(a) (a).begin(), (a).end()
#define sz(x) ((ll)(x).size())
#define rep1(n) for(ll dummy_iter = 0LL; dummy_iter < n; ++dummy_iter) // 0,1,...,n-1
#define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // i=0,1,...,n-1
#define rep3(i, s, t) for(ll i = ll(s), i##_counter = ll(s); i##_counter < ll(t); ++(i##_counter), (i) = (i##_counter)) // i=s,s+1,...,t-1
#define rep4(i, s, t, step) for(ll i##_counter = step > 0 ? ll(s) : -ll(s), i##_end = step > 0 ? ll(t) : -ll(t), i##_step = abs(step), i = ll(s); i##_counter < i##_end; i##_counter += i##_step, i = step > 0 ? i##_counter : -i##_counter) // i=s,s+step,...,<t
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define repe(a, v) for(auto& a : (v)) // iterate over all elements in v
#define smod(n, m) ((((n) % (m)) + (m)) % (m))
#define sdiv(n, m) (((n) - smod(n, m)) / (m))
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());}
int Yes(bool b=true) { cout << (b ? "Yes\n" : "No\n"); return 0; };
int YES(bool b=true) { cout << (b ? "YES\n" : "NO\n"); return 0; };
int No(bool b=true) {return Yes(!b);};
int NO(bool b=true) {return YES(!b);};
template<typename T, size_t N> T max(array<T, N>& a) { return *max_element(all(a)); };
template<typename T, size_t N> T min(array<T, N>& a) { return *min_element(all(a)); };
template<typename T> T max(vector<T>& a) { return *max_element(all(a)); };
template<typename T> T min(vector<T>& a) { return *min_element(all(a)); };
template<typename T> vector<T> vec_slice(const vector<T>& a, int l, int r) { vector<T> rev; rep(i, l, r) rev.push_back(a[i]); return rev; };
template<typename T> T sum(vector<T>& a, T zero = T(0)) { T rev = zero; rep(i, sz(a)) rev += a[i]; return rev; };
template<typename T> bool in_range(const T& val, const T& s, const T& t) { return s <= val && val < t; };

template <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; }

ll powm(ll a, ll n, ll mod=INFL) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * a) % mod;
        if (n > 1) a = (a * a) % mod;
        n >>= 1;
    }
    return res;
}
ll sqrtll(ll x) {
    assert(x >= 0);
    ll rev = sqrt(x);
    while(rev * rev > x) --rev;
    while((rev+1) * (rev+1)<=x) ++rev;
    return rev;
}
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; }
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; }
int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; }
/**
 * @brief std.hpp
 * @docs docs/std/std.md
 */
//line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp"
// overload operators (prototypes)
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p);
template <class T> inline istream& operator>>(istream& is, vector<T>& v);
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v);
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp);
template <typename T> ostream &operator<<(ostream &os, const set<T> &st);
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st);
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st);
template <typename T> ostream &operator<<(ostream &os, queue<T> q);
template <typename T> ostream &operator<<(ostream &os, deque<T> q);
template <typename T> ostream &operator<<(ostream &os, stack<T> st);
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq);

// overload operators
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; }
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; }
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << " "; } return os; }
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << ":" << val << " "; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { ll cnt = 0; for (auto &e : st) { os << e << (++cnt != (int)st.size() ? " " : ""); } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << " "; q.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front(); q.pop_front(); if (q.size()) os << " "; } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << " "; st.pop(); } return os; }
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << " "; pq.pop(); } return os; }

template <typename T> int print_sep_end(string sep, string end, const T& val) { (void)sep; cout << val << end; return 0; };
template <typename T1, typename... T2> int print_sep_end(string sep, string end, const T1 &val, const T2 &...remain) {
    cout << val << sep;
    print_sep_end(sep, end, remain...);
    return 0;
};
template <typename... T> int print(const T &...args) { print_sep_end(" ", "\n", args...); return 0; };
template <typename... T> void flush() { cout << flush; };
template <typename... T> int print_and_flush(const T &...args) { print(args...); flush(); return 0; };
#define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print
template <typename T> void input(T &a) { cin >> a; };
template <typename T1, typename... T2> void input(T1&a, T2 &...b) { cin >> a; input(b...); };
#ifdef LOCAL_TEST
template <typename T> void debug_func(int i, const T name) { (void)i; (void)name; cerr << endl; }
template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) {
    int scope = 0;
    for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) {
        cerr << name[i];
        if (name[i] == '(' || name[i] == '{') scope++;
        if (name[i] == ')' || name[i] == '}') scope--;
    }
    cerr << ":" << a << " ";
    debug_func(i + 1, name, b...);
}
template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, T2 &a, T3 &...b) {
    int scope = 0;
    for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) {
        cerr << name[i];
        if (name[i] == '(' || name[i] == '{') scope++;
        if (name[i] == ')' || name[i] == '}') scope--;
    }
    cerr << ":" << a << " ";
    debug_func(i + 1, name, b...);
}
#endif
#ifndef LOCAL_TEST
template <typename... T>
void debug_func(T &...) {}
template <typename... T>
void debug_func(const T &...) {}
#endif
/**
 * @brief io.hpp
 * @docs docs/std/io.md
 */
//line 3 "answer.cpp"
int main() {
    while (true) {
        ll n; input(n);
        if (n == 0) break;
        vector a(n, vector<ll>(n+1, 0));
        rep(i, n) rep(j, n+1) cin >> a[i][j];
        vector<ll> dp((1LL << n), INFL);
        dp[0] = 0;
        rep(bit, (1LL << n)) {
            rep(i, n) {
                if ((bit >> i) & 1) continue;
                ll bn = bit ^ (1LL << i);
                ll add = a[i][0];
                rep(j, n) {
                    if ((bit >> j) & 1) {chmin(add, a[i][j+1]);}
                }
                chmin(dp[bn], dp[bit] + add);
            }
        }
        cout << dp[(1LL << n) - 1] << '\n';
    }
}
accuracy: 1
time_ms: 350
memory_kb: 3868
final_rank: 7

code_snippet: //#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i=0;i<(ll)n;i++)
#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)
#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)
#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<" ";}cout<<endl;
#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<" ";}cout<<endl;}
#define setdbg(a) for(const auto & ii:a){cout<<ii<<" ";}cout<<endl;
#define inf 4000000000000000000LL
#define mod 998244353LL
#define eps 0.000000001
//#define mod 1000000007LL
random_device rnd;// 非決定的な乱数生成器
mt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード

//#include<boost/multiprecision/cpp_int.hpp>
//#define bbi boost::multiprecision::cpp_int

//#include<atcoder/lazysegtree>

//√の値が整数かを調べる
bool isSqrt(ll n) {
	if (n < 0) return false;
	ll sqrtN = static_cast<ll>(sqrt(n));
	return sqrtN * sqrtN == n;
}

//整数同士の累乗の計算をする。
ll power(ll A, ll B) {
	ll result = 1;
	for (ll i=0;i<B;i++){
		result *= A;
	}
	return result;
}

//素因数分解
vector<ll> makePrime(ll n){
	vector<ll> factors;
	while (n % 2 == 0) {
		factors.push_back(2);
		n /= 2;
	}
	for (ll i=3; i*i<=n;i+=2) {
		while (n%i == 0) {
			factors.push_back(i);
			n /= i;
		}
	}
	if (n > 2) {
		factors.push_back(n);
	}
	return factors;
}

//map形式で、nを素因数分解した値を返す
map<ll,ll> makeMapPrime(ll n){
	map<ll,ll> factors;
	while (n % 2 == 0) {
		factors[2]++;
		n /= 2;
	}
	for (ll i=3; i*i<=n;i+=2) {
		while (n%i == 0) {
			factors[i]++;
			n /= i;
		}
	}
	if (n > 2) {
		factors[n]++;
	}
	return factors;
}

// nのk乗をmodで割った余りを計算
ll power_mod(ll n, ll k){
	long long result = 1;
	while (k > 0){
		if ((k&1) ==1)result=(result*n)%mod;
		n=n*n%mod;
		k >>= 1;
	}
	return result;
}

//mod mにおけるaの逆元を計算
ll modinv(ll a, ll m) {
	ll b = m, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m; 
	if (u < 0) u += m;
	return u;
}

//場合の数 nCr を求める
ll ncr(ll n,ll r) {
	if(n<r)return 0;
	vvl dp(n+1,vl(r+1));
	rep (i,n+1)dp[i][0] = 1;
	rep (i,r+1)dp[i][i] = 1;
	loop (i,1,n){
		loop (j,1,min((ll)i-1,r)) {
			//nCr= n-1Cr-1 + n-1Cr
			dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
		}
	}
	return dp[n][r];
}

//受け取った文字列を、第2引数が0なら全て小文字に、1なら大文字に変換する関数
string cnvString(const string &str, int mode) {
	string result = str;
	if (mode == 0) {
		// 小文字に変換
		for (char &c : result) {
			c = tolower(c);
		}
	} else if (mode == 1) {
		// 大文字に変換
		for (char &c : result) {
			c = toupper(c);
		}
	}
	return result;
}

//第一引数で受け取った数を、第二引数で受け取った数の進数と見做して、第三引数の進数へ変換する。
string cnvBase(const string &str, ll from_base, ll to_base) {
	ll num = 0;
	//小文字があったら大文字に変換
	string num_str=cnvString(str,1);
	// 数値を10進数に変換
	for (char digit : num_str) {
		num = num * from_base + (isdigit(digit) ? digit - '0' : digit - 'A' + 10);
	}
	string result;
	// 数値を目的の基数に変換
	while (num > 0) {
		ll remainder = num % to_base;
		result.push_back(remainder < 10 ? remainder + '0' : remainder - 10 + 'A');
		num /= to_base;
	}
	// 結果を逆順にして返す
	reverse(result.begin(), result.end());
	return result.empty() ? "0" : result;
}

//底がaの対数xを計算。ただし小数点は繰り上げ。
ll logax(ll a, ll x){
	if(x<=1)return 0;
	ll result = 1;
	ll power = 1;
	while (power < (x+a-1) / a){
		power *= a;
		result++;
	}
	return result;
}

//第一引数を第二引数で割った余りを計算、割る数はint範囲
ll bigmd(const string &num, int md) {
	ll ans = 0;
	ll SIZ = 9; //9桁のチャンク
	ll base = 1000000000;//SIZ個の0
	rep(i,(num.size()-1)/SIZ+1){
		ll chunk = 0;
		ll l = i*SIZ;
		ll r = min((ll)num.size(),l+SIZ);
		if(r!=num.size()){
			ans = (ans*base+stoll(num.substr(l,r-l)))%md;
		}else{
			rep(i,r-l)ans*=10;
			ans=(ans+stoll(num.substr(l,r-l)))%md;
		}
	}
	return ans;
}

//受け取った2次元文字の外側に、文字pをコーティングする。
vector<string> pad(vector<string> &s,char p){
	ll h=s.size();
	ll w=s[0].size();
	vector<string> res(h+2,string(w+2,p));
	rep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];
	return res;
}

//ax+by=cの整数解を得る ただし、cはgcd(a,b)の倍数でない場合、0,0になる
pair<ll,ll> ex_euclid(ll a,ll b,ll c){
	if(a<0||b<0||c<0){
		pair<ll,ll>ans=ex_euclid(abs(a),abs(b),abs(c));
		if(a<0)ans.first*=-1;
		if(b<0)ans.second*=-1;
		if(c<0)ans.first*=-1,ans.second*=-1;
		return ans;
	}
	if(c!=1){
		ll d=gcd(a,b);
		if(c%d!=0)return make_pair(0,0);
		pair<ll,ll>ans = ex_euclid(a/d,b/d,1);
		ans.first*=c/d;
		ans.second*=c/d;
		return ans;
	}
	if(a<b){
		pair<ll,ll>ans=ex_euclid(b,a,c);
		swap(ans.first,ans.second);
		return ans;
	}
	if(a==1&&b==0)return make_pair(1,0);
	else if(b==0) return make_pair(0,0);
	ll x,y;
	tie(x,y)=ex_euclid(b,a%b,c);
	pair<ll,ll> ans=make_pair(y,x-(a/b)*y);
	return ans;
}

//オイラーのトーシェント関数。N以下のNと互いに素な物の数を返す。
ll euler(ll n){
	unordered_map<ll,ll> factors;
	ll tmp=n;
	while (tmp % 2 == 0) {
		factors[2]++;
		tmp /= 2;
	}
	for (ll i=3; i*i<=tmp;i+=2) {
		while (tmp%i == 0) {
			factors[i]++;
			tmp/= i;
		}
	}
	if (tmp > 2)factors[tmp]++;
	ll ans=1;
	for(const auto & val:factors){
		ans*=power(val.first,val.second-1)*(val.first-1);
	}
	return ans;
}

// Union-Find
struct UnionFind {
	vector<int> par, siz;
	UnionFind(int n) : par(n, -1) , siz(n, 1) { }
	// 根を求める
	int root(int x) {
		if (par[x] == -1) return x;
		else return par[x] = root(par[x]);
	}
	// x と y が同じグループに属するかどうか (根が一致するかどうか)
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	// x を含むグループと y を含むグループとを併合する
	bool unite(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) return false; 
		if (siz[x] < siz[y]) swap(x, y);
		par[y] = x;
		siz[x] += siz[y];
		return true;
	}
	// x を含むグループのサイズ
	int size(int x) {
		return siz[root(x)];
	}
};

//重み付きUF
struct PotentialUnionFind {
	ll n;
	vl par, siz, pot;
	PotentialUnionFind(ll N) : par(N,-1) , siz(N,1) , pot(N,0){n=N;}
	// 根を求める
	ll root(ll x) {
		if (par[x] == -1) return x;
		ll tmp = root(par[x]);
		pot[x] += pot[par[x]];
		par[x] = tmp;
		return par[x];
	}
	// x と y が同じグループに属するかどうか (根が一致するかどうか)
	bool issame(ll x, ll y) {
		return root(x) == root(y);
	}
	//x よりいくつ大きい所に y があるか。根が一致しない場合は"0"
	ll potential(ll x,ll y){
		if(root(x) != root(y)) return 0;
		else return pot[y]-pot[x];
	}
	//x より w だけ大きい状態として y を併合。
	bool unite(ll x, ll y, ll w) {
		ll rx = root(x),ry = root(y);
		if (rx == ry) return false;
		w += pot[x]-pot[y];
		if (siz[rx] < siz[ry]) swap(rx, ry),w*=-1;
		par[ry] = rx;
		siz[rx] += siz[ry];
		siz[ry] = 0;
		pot[ry] = w;
		return true;
	}
	// x を含むグループのサイズ
	ll size(ll x) {
		return siz[root(x)];
	}
	//小さい順にUnionFindグラフを調整、O(n log n)
	void regulation(){
		vvl r(n);
		rep(i,n)r[root(i)].push_back(i);
		rep(i,n){
			if(r[i].size()==0)continue;
			ll mn = i;
			rep(j,r[i].size())if(pot[mn]>pot[r[i][j]])mn=r[i][j];
			siz[mn]=siz[i];
			siz[i]=0;
			ll tmp = pot[mn];
			rep(j,r[i].size()){
				pot[r[i][j]]-=tmp;
				par[r[i][j]] = mn;
			}
			par[mn]=-1;
		}
	}
	void debug(){
		rep(i,n)cout<<setw(4)<<left<<par[i]<<" ";
		cout<<endl;
		rep(i,n)cout<<setw(4)<<left<<pot[i]<<" ";
		cout<<endl;
	}
};

//分離可能UnionFind、経路圧縮をしない。
struct CuttingFind{
	vector<int> par, siz;
	CuttingFind(int n) : par(n, -1) , siz(n, 1) { }
	// 根を求める
	int root(int x) {
		if (par[x] == -1) return x;
		else return root(par[x]);
	}
	// x と y が同じグループに属するかどうか (根が一致するかどうか)
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	//根x と 根y のグループを併合する(お互い根ではない時、falseで何もしない)
	bool unite(int x, int y) {
		if (issame(x,y) || par[x] != -1 || par[y] != -1) {
			cout<<"error"<<endl;
			return false;
		}
		if (siz[x] < siz[y]) swap(x, y);
		par[y] = x;
		siz[x] += siz[y];
		return true;
	}
	//根の側から、その直系の子供を分離する。片方が根でもう片方が直系の子でなければならない。
	bool separate(int x,int y){
		if(par[y]==-1)swap(x,y);
		if(par[y]!=x||par[x]!=-1){
			cout<<"error2"<<endl;
			return false;
		}
		siz[x] -= siz[y];
		par[y]=-1;
		return true;
	}
	// x を含むグループのサイズを求める
	int size(int x) {
		return siz[root(x)];
	}
};
//セグ木,乗せる値の型が必要
template<typename T>
struct SegTree{
	ll size;
	ll tall;
	vector<T> data;
	function<T(T,T)> p;
	//セグ木に乗せる値の初期値をa配列にし、putの関数をセグ木に乗せる、dをデフォルト値に。
	SegTree(vector<T> a,function<T(T,T)> put,T d) : data(power(2,logax(2,a.size())+1)) {
		size = data.size()/2;
		tall=logax(2,size)+1;
		p=put;
		ll tmp=size;
		data = vector<T>(size*2,d);
		while(tmp!=0){
			if(tmp==size)rep(i,a.size())data[tmp+i]=a[i];
			else rep(i,tmp) data[tmp+i]=p(data[2*(tmp+i)],data[2*(tmp+i)+1]);
			tmp/=2;
		}
	}
	//更新、t番目の値をxにする。
	void update(ll t,T x){
		t+=size;
		while(t!=0){
			if(t>=size)data[t]=x;
			else data[t]=p(data[2*t],data[2*t+1]);
			t/=2;
		}
	}
	//取得、l~r区間内の評価値を取得する。
	T get(ll l,ll r){
		//lとrが範囲外なら範囲内に正す
		l=max(0LL,l);
		r=min(r,size-1);
		r++;
		T ans=data[0];
		ll pos=l+size;
		ll wid=1;
		//出来る限り上に上げきる。
		while(l+(wid*2)<=r){
			while(l%(wid*2)==0&&l+(wid*2)<=r)pos/=2,wid*=2;
			ans=p(ans,data[pos]);
			pos++;
			l+=wid;
		}
		//上げ終わったので今度は下げる
		while(l!=r){
			while(l+wid>r)pos*=2,wid/=2;
			ans=p(ans,data[pos]);
			pos++;
			l+=wid;
		}
		return ans;
	}
	//セグ木デバッグ用、丸ごと出力
	void print(){
		rep(i,size)cout<<setw(7)<<left<<i;
		cout<<endl;
		ll pos=size;
		rep(i,tall){
			rep(j,size){
				if(j%power(2,i)==0)cout<<setw(7)<<left<<data[pos],pos++;
				else cout<<"       ";
			}
			pos/=4;
			cout<<endl;
		}
	}
};

//グリッド問題等用
vl dx={1,0,-1,0};
vl dy={0,1,0,-1};


ll solve(){
	ll n;
	cin>>n;
	if(n==0){return 1;}
	vvl a(n,vl(n+1));	
	rep(i,n)rep(j,n+1){
		cin>>a[i][j];
	}

	vl dp(1<<n,inf);
	dp[0]=0;

	rep(i,1<<n){
		rep(j,n){
			if(i&(1LL<<j))continue;
			ll mn=a[j][0];
			loop(k,1,n){
				if(!(i&(1LL<<(k-1))))continue;
				mn=min(mn,a[j][k]);
			}
			dp[i+(1LL<<j)]=min(dp[i+(1LL<<j)],dp[i]+mn);
		}
	}
	cout<<dp[(1LL<<n)-1]<<endl;
	return 0;
}

//メイン
int main(){
	while(solve()==0);
	return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 4180
final_rank: 14

code_snippet: // #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lll = __int128_t;
using ld = long double;
using P = pair<ll, ll>;
using vi = vector<ll>;
using vd = vector<ld>;
using vP = vector<P>;
using vS = vector<string>;
using vvi = vector<vi>;
using vvd = vector<vd>;
using vvP = vector<vP>;
using v3i = vector<vvi>;
using v3d = vector<vvd>;
using v3P = vector<vvP>;
using v4i = vector<v3i>;
using v5i = vector<v4i>;
using v6i = vector<v5i>;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using Mint = modint1000000007;
using vm = vector<mint>;
using vM = vector<Mint>;
using vvm = vector<vm>;
using vvM = vector<vM>;
using v3m = vector<vvm>;
using v3M = vector<vvM>;
using v4m = vector<v3m>;
using v5m = vector<v4m>;
#endif
template <typename T>
using rp_Q = priority_queue<T, vector<T>, greater<T>>;
#define rrep(i, n) for (ll i = (n) - 1; (i) >= 0; --(i))
#define rep(i, n) for (ll i = 0; (i) < (n); ++(i))
#define loop(i, a) for (ll i = a; true; ++(i))
#define reps(i, n) for (ll i = 1; (i) <= (n); ++(i))
#define Rep(i, a, b) for (ll i = (a); i <= (b); i++)
#define all(a) (a).begin(), (a).end()
const ll MOD = 998244353;
const ll Hash = 10000000000000061;
#define Yes(b) ((b) ? cout << "Yes" << endl : cout << "No" << endl)
#define YES(b) ((b) ? cout << "YES" << endl : cout << "NO" << endl)
#define Aoki(b) ((b) ? cout << "Aoki" << endl : cout << "Takahashi" << endl)
#define flush cout << endl;
#define out(x) cout << (x) << endl
#define vout(v)      \
  for (auto x : (v)) \
    cout << x << ' ';
#define vin(v) rep(i, (v).size()) cin >> (v)[i]
#define vin_(v)      \
  ;                  \
  rep(i, (v).size()) \
  {                  \
    cin >> (v)[i];   \
    (v)[i]--;        \
  }
#define vvin(v, w)                    \
  rep(i, (v).size())                  \
  {                                   \
    rep(k, (w)) { cin >> (v)[i][k]; } \
  }
#define ft first
#define sd second
#define vInP(v) rep(i, (v).size()) cin >> (v)[i].ft >> (v)[i].sd
#define vInP_(v)                   \
  rep(i, (v).size())               \
  {                                \
    cin >> (v)[i].ft >> (v)[i].sd; \
    (v)[i].ft--;                   \
    (v)[i].sd--;                   \
  }
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
vi s90 = {0, 1, 0, -1}, c90 = {1, 0, -1, 0};
vi s45 = {0, 1, 1, 1, 0, -1, -1, -1}, c45 = {1, 1, 0, -1, -1, -1, 0, 1};
const int INF = 1'000'000'000;

int main()
{
  while (true)
  {
    int N;
    cin >> N;
    if (N == 0){
      break;
    }
    vector<int> t0(N);
    vector t(N, vector<int>(N, 0));
    for (int i = 0; i < N; i++)
    {
      cin >> t0[i];
      for (int j = 0; j < N; j++)
      {
        cin >> t[i][j];
      }
    }
    vector dp(1 << N, vector<int>(N, INF));
    for (int i = 0; i < N; i++)
    {
      dp[0][i] = t0[i];
    }
    for (int b = 0; b < (1 << N); b++)
    {
      for (int i = 0; i < N; i++)
      {
        dp[b][i] = t0[i];
        for (int j = 0; j < N; j++)
        {
          if (b & (1 << j))
            dp[b][i] = min(dp[b][i], t[i][j]);
        }
      }
    }
    vector<int> time(1 << N, INF);
    time[0] = 0;
    rep(bit, (1 << N))
    {
      rep(k, N)
      {
        if (bit & (1 << k))
        {
          int Bit = bit - (1 << k);
          chmin(time[bit], time[Bit] + dp[Bit][k]);
        }
      }
    }
    cout << time[(1 << N) - 1] << endl;
  }
}
accuracy: 1
time_ms: 520
memory_kb: 10576
final_rank: 18

code_snippet: // https://codeforces.com/blog/entry/96344
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;

#if __cplusplus >= 202002L
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
#endif

template<typename T>
bool chmax(T& a, const T& b) {
    bool res = a < b;
    a = max(a, b);
    return res;
}
template<typename T>
bool chmin(T& a, const T& b){
    bool res = a > b;
    a = min(a, b);
    return res;
}

typedef vector<long long> vl;
typedef pair<ll,ll> pll;
typedef vector<pair<ll, ll>> vll;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef pair<int,int> pii;

const int inf = 1000000009;
const ll linf = 4000000000000000009;


// https://trap.jp/post/1224/
template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

void print(){
    cout << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template<typename T, typename U>
T SUM(const vector<U> &A){
    T ret = 0;
    for (auto &&a: A) ret += a;
    return ret;
}

#define rep1(a)          for(int i = 0; i < a; i++)
#define rep2(i, a)       for(int i = 0; i < a; i++)
#define rep3(i, a, b)    for(int i = a; i < b; i++)
#define rep4(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep(i, a, b)    for(int i = a; i >= b; i--)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(v) v.begin(), v.end()
//---------------------------------


ll dp[1<<20];
ll t[20][20];

void solve(int n){
    rep(i, 1<<n){
        dp[i] = linf;
    }
    dp[0] = 0;
    rep(i, n) rep(j, n+1){
        cin >> t[i][j];
    }
    for(ll s = 0; s < (1<<n); s++){
        rep(i, n){
            if((s & (1<<i)) != 0) continue;
            ll mn = t[i][0];
            rep(j, 1, n+1){
                if(s & (1<<(j-1))){
                    chmin(mn, t[i][j]);
                }
            }
            chmin(dp[s | (1<<i)], dp[s] + mn);
        }
    }
    print(dp[(1<<n)-1]);
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    while(n > 0){
        solve(n);
        cin >> n;
    }
}
accuracy: 1
time_ms: 380
memory_kb: 4096
final_rank: 10

code_snippet: /**
	author:  shobonvip
	created: 2025.04.24 17:46:57
**/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}



void solve(int N) {
	vector<vector<ll>> T(N);
	vector<ll> normal(N);
	rep(i,0,N){
		ll t;
		vector<ll> l(N);
		cin>>normal[i];
		rep(i,0,N){
			cin>>l[i];
		}
		T[i]=l;
	}
	auto time=[&](ll b,ll stage){
		ll res=normal[stage];
		rep(i,0,N){
			if ((b>>i)&1){
				chmin(res,T[stage][i]);
			}
		}
		return res;
	};
	vector<ll> dist(1<<N,1LL<<50);
	dist[0]=0;
	rep(b,0,1<<N){
		rep(i,0,N){
			if (((b>>i)&1)==0){
				ll nb=b|(1<<i);
				chmin(dist[nb],dist[b]+time(b,i));
			}
		}
	}
	ll ans=dist[(1<<N)-1];
	cout<<ans<<endl;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
	while(true) {
		int n; cin >> n;
		if (n==0) break;
		solve(n);
	}
}
accuracy: 1
time_ms: 330
memory_kb: 3864
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll ILL=2167167167167167167;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}
template<class T> T square(T a){return a * a;}

int N;

void solve();
// CITRUS CURIO CITY / FREDERIC
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (cin >> N, N) solve();
}

void solve(){
    vector T(N, vector<int>(1 << N, INF));
    rep(i, 0, N){
        int a;
        cin >> a;
        for (auto &x : T[i]) x = a;
        rep(j, 0, N){
            cin >> a;
            chmin(T[i][1 << j], a);
        }
        rep(j, 0, N) rep(k, 0, 1 << N) if (k & (1 << j)) chmin(T[i][k], T[i][k - (1 << j)]);
    }
    vector dp(1 << N, INF);
    dp[0] = 0;
    rep(i, 0, 1 << N) rep(j, 0, N) if ((i & (1 << j)) == 0){
        chmin(dp[i + (1 << j)], dp[i] + T[j][i]);
    }
    cout << dp.back() << "\n";
}
accuracy: 1
time_ms: 370
memory_kb: 7636
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<vector<int>> T(N,vector<int>(N));
    vector<int> X(N);
    rep(i,0,N) {
        cin >> X[i];
        rep(j,0,N) cin >> T[i][j];
    }
    vector<vector<int>> C(1<<N,vector<int>(N));
    rep(i,0,1<<N) {
        rep(j,0,N) {
            C[i][j] = X[j];
            rep(k,0,N) {
                if ((i>>k)&1) chmin(C[i][j],T[j][k]);
            }
        }
    }
    vector<int> DP(1<<N,inf);
    DP[0] = 0;
    rep(i,0,1<<N) {
        rep(k,0,N) {
            if (((i>>k)&1)==0) {
                chmin(DP[i|(1<<k)],DP[i]+C[i][k]);
            }
        }
    }
    cout << DP[(1<<N)-1] << endl;;
}
}
accuracy: 1
time_ms: 1240
memory_kb: 10328
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,k,n) for (int i = k; i < (n); ++i)
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<ll, ll> P;
const long long INF = 1e17;

int main(){
    while(true){
        int n;
        cin >> n;
        if(n==0){
            break;
        }
        //t[i][j]:i番目のステージをj番目の装備で
        //攻略する時間(j=0は装備なし)
        vector t(n+10,vector<int>(n+10));

        for(int i=1;i<=n;i++){
            for(int j=0;j<=n;j++){
                cin >> t[i][j];
            }
        }

        //dp[S]:Sをクリアするのに必要な最小時間
        vector<int> dp(1<<n,1e9);
        dp[0] = 0;
        for(int s=0;s<1<<n;s++){
            for(int x=0;x<n;x++){
                if(s&(1<<x)){
                    int ss = s^(1<<x);
                    dp[s] = min(dp[s],dp[ss]+t[x+1][0]);
                    for(int y=0;y<n;y++){
                        if(ss&(1<<y))dp[s] = min(dp[s],dp[ss]+t[x+1][y+1]);
                    }
                }
            }
        }

        cout << dp[(1<<n)-1] << endl;


    }
    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 3712
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>; using vs = vector<string>;
using pl = pair<long long, long long>;
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;
template <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}
template <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}
const ll INF = 1LL << 60;
const ld PI = acos(-1);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);
 
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
void solve();
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
  int num_tc = 1;
  //cin >> num_tc;
  rep(tc,num_tc){
    //cout << "Case #" << tc+1 << ": " ;// << endl;
    solve();
  }
}
//見切り発車で実装しては行けない．頭の中でコードが完全に出来上がるまで書き始めるな．
ll t[20][20];
ll dp[1ll<<16];
int N;
void solve(){
  while(true){
    cin>>N;
    if(N==0)return;
    rep(i,(1ll<<16))dp[i]=INF;
    rep(i,N)rep(j,N+1)cin>>t[i][j];
    //dp[S] = 集合Sの装備を持っている時の最小値
    //dp[(1ll<<N)-1]
    dp[0] = 0;
    rep(S,(1ll<<N)){
      rep(i,N){
        if(S&(1ll<<i))continue;
        ll now = t[i][0];
        rep(j,N)if(S&(1ll<<j)){
          chmin(now,t[i][j+1]);
        }
        chmin(dp[S|(1ll<<i)],now+dp[S]);
      }
    }
    cout<<dp[(1ll<<N)-1]<<endl;
  }
}
accuracy: 1
time_ms: 430
memory_kb: 3896
final_rank: 12

code_snippet: #include<bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

bool solve(){
    ll N;cin>>N;
    if(N==0) return 0;
    vvll T(N,vll(N+1));
    rep(i,N) rep(j,N+1) cin>>T[i][j];

    vll dp(1<<N,INF);
    dp[0]=0;
    rep(i,(1<<N)){
        rep(j,N){
            if(i>>j&1) continue;
            ll mi=T[j][0];
            rep(k,N){
                if(i>>k&1) chmin(mi,T[j][k+1]);
            }
            chmin(dp[i|(1<<j)],dp[i]+mi);
        }
    }
    cout << dp[(1<<N)-1] << endl;
    return 1;
}


int main(){
   cin.tie(0);
   ios::sync_with_stdio(false);
   while(solve());
}
accuracy: 1
time_ms: 370
memory_kb: 3672
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    ios::sync_with_stdio(false); //
    cin.tie(nullptr); //入力高速化

    while (true){
        int n;
        cin >> n;
        if (n==0)break;
        vector<vector<int>> T(n+1, vector<int>(n + 1));
        for (int i = 1; i < n+1;i++){
            for (int j = 0; j < n + 1; j++){
                cin >> T[i][j];
            }
        }
        vector<ll> dp;

        ll inf = 100000000000;



        ll num=(1<<(n+1));

        for (int S= 0; S< num; S++){
            dp.emplace_back(inf);
        }
        dp[1] = 0;
    

        for (int S = 0; S < num; S++){
            if (S%2==0)
                continue;
            for (int v = 0; v < n+1; v++){
                if ((S&(1<<v))==0)continue;
                for (int u = 0; u < n + 1; u++){
                    if ((S&(1<<u))!=0)continue;
                    dp[S+(1<<u)]=min(dp[S+(1<<u)],dp[S]+T[u][v]);

                }
            }
        }
        cout << dp[num - 1] << endl;
    }




    
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 5168
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 1001010;
const ll llINF = (long long)4e18 + 22000020;
const string endn = "\n";
template <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}
const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b){if (b < 0) a = -a, b = -b; if(a >= 0) return a / b; else return (a + 1) / b - 1;}
ll ceil(ll a, ll b){if (b < 0) a = -a, b = -b; if(a > 0) return (a - 1) / b + 1; else return a / b;}
ll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}
#ifdef DEBUG
#include <debug_slephy.cpp>
#else
#define debug(...)
#endif
// ================================== ここまでテンプレ ==================================

void solve(){
    ll n; cin >> n;
    if(n == 0) exit(0);
    vector<ll> default_t(n);
    auto t = vector2<ll>(n, n);
    for(int i = 0; i < n; i++){
        cin >> default_t[i];
        cin >> t[i];
    }

    auto dp = vector<ll>(1<<n, llINF);
    dp[0] = 0;
    for(int bt = 0; bt < (1<<n); bt++){
       for(int add = 0; add < n; add++){
            if(bit(bt, add)) continue;
            ll cost = default_t[add];

            for(int j = 0; j < n; j++){
                if(bit(bt, j)){
                    chmin(cost, t[add][j]);
                }
            }

            ll nbt = bt | (1<<add);
            chmin(dp[nbt], dp[bt] + cost);
       }
    }

    ll ans = dp[(1<<n)-1];
    cout << ans << endl;
}

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(true) solve();
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 3904
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
void chmin(int&a, int b){if(a>b)a=b;}
int N,T[17][17];
int dp[1<<16];
bool solve()
{
    int N;
    cin>>N;
    if(N==0)return 0;
    for(int i=0;i<N;i++)for(int j=0;j<=N;j++)cin>>T[i][j];
    for(int i=0;i<(1<<N);i++)dp[i]=1<<30;
    dp[0]=0;
    for(int i=1;i<(1<<N);i++)
    {
        for(int j=0;j<N;j++)if(i>>j&1)
        {
            int J=1<<j;
            chmin(dp[i],dp[i^J]+T[j][0]);
            for(int k=0;k<N;k++)if((i>>k&1)==0)chmin(dp[i],dp[i^J]+T[j][k+1]);
        }
    }
    cout<<dp[(1<<N)-1]<<endl;
    return 1;
}
int main()
{
    while(solve()){}
}
accuracy: 1
time_ms: 480
memory_kb: 3564
final_rank: 13

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

int solve(int N){
    vvi T(N, vi(N + 1)); rep(i, N) rep(j, N + 1) cin >> T[i][j];
    vi dp(1 << N, inf);
    dp[0] = 0;
    rep(cmb, 1 << N){
        rep(i, N) if (~cmb >> i & 1){
            dp[cmb | (1 << i)] = min(dp[cmb | (1 << i)], dp[cmb] + T[i][0]);
            rep(j, N) if (cmb >> j & 1) dp[cmb | (1 << i)] = min(dp[cmb | (1 << i)], dp[cmb] + T[i][j + 1]);
        }
    }
    return dp[(1 << N) - 1];
}

int main(){
    vc<int> ans;
    while (true){
        int N; cin >> N;
        if (N == 0) break;
        ans.push_back(solve(N));
    }
    for (auto x : ans) cout << x << endl;
}
accuracy: 1
time_ms: 440
memory_kb: 3696
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool solve() {
    int n; std::cin >> n;
    if (!n) return false;
    std::vector t(n, std::vector<int>(n + 1));
    for (auto& v : t) for (auto& x : v) std::cin >> x;
    const int INF{(int)1e9};
    std::vector dp(1 << n, INF);
    dp[0] = 0;
    for (int bit{} ; bit < (1 << n) ; bit++) {
        for (int i{} ; i < n ; i++) if (!(bit & (1 << i))) {
            int nxt{bit | (1 << i)};
            dp[nxt] = std::min(dp[nxt], dp[bit] + t[i][0]);
            for (int j{} ; j < n ; j++) if (bit & (1 << j)) {
                dp[nxt] = std::min(dp[nxt], dp[bit] + t[i][j + 1]);
            }
        }
    }
    int ans{dp[(1 << n) - 1]};
    std::cout << ans << '\n';
    return true;
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    for (int i{} ; solve() ; i++) {
    }
}
accuracy: 1
time_ms: 400
memory_kb: 3716
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int INF = (1<<29);
int n, t[20][20], memo[(1<<17)];
int dfs(int num, int bit){
    if(num == 0) return 0;
    if(memo[bit]>=0) return memo[bit]; 
    int res = INF;
    for(int i=0;i<n;i++){
        if((bit&1<<i)>0) continue;
        int prt = t[i][0];
        for(int j=0;j<n;j++){
            if((bit&1<<j)>0) prt = min(prt, t[i][j+1]);
        }
        res = min(res, dfs(num-1, (bit|1<<i))+prt);
    }
    return memo[bit] = res;
}
int main(){
    while(cin >> n, n){
        for(int i=0;i<n;i++){
            for(int j=0;j<=n;j++){
                cin >> t[i][j];
            }
        }
        memset(memo, -1, sizeof(memo));
        cout << dfs(n, 0) << endl;
    }
    return(0);
}
accuracy: 1
time_ms: 350
memory_kb: 3584
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int n;

    auto solve = [&]() {
        vector a(n, vector<int>(n + 1));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= n; j++) {
                cin >> a[i][j];
            }
        }

        constexpr int INF = 1e9;

        vector dp(1 << n, INF);
        dp[0] = 0;

        for (int s = 0; s < 1 << n; s++) {
            for (int i = 0; i < n; i++) {
                if (s >> i & 1) {
                    continue;
                }
                int ns = s | 1 << i;
                int mn = a[i][0];
                for (int j = 0; j < n; j++) {
                    if (s >> j & 1) {
                        mn = min(mn, a[i][j + 1]);
                    }
                }
                dp[ns] = min(dp[ns], dp[s] + mn);
            }
        }

        cout << dp.back() << '\n';
    };

    while (cin >> n && n != 0) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3608
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)
#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)
#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)
#define ll long long
#define ld long double
#define print(t) cout << t << endl 
#define all(a)  (a).begin(),(a).end()
// << std::fixed << std::setprecision(0)
const ll INF = 1LL << 60;
 
template<class T> bool chmin(T& a, T b){
 if(a > b){
	a = b;
	return true;
 }
	return false;
}
 
template<class T> bool chmax(T& a, T b){
 if(a < b){
	a = b;
	return true;
 }
	return false;
}
	
ll lpow(ll x,ll n){
	ll ans = 1;
	while(n >0){
		if(n & 1)ans *= x;
		x *= x;
		n >>= 1;
	}
	return ans;
}
 
int main(){
	while(true){
		ll n;cin >> n;
		if(n == 0){
			break;
		}
		vector<ll> base_time(n);
		vector<vector<ll>> use_time(n,vector<ll>(n));
		rep(i,n){
			rep(j,n+1){
				if(j == 0)cin >> base_time[i];
				else cin >> use_time[i][j-1];
			}
		}
		vector<vector<ll>> dp(1 << n,vector<ll>(n,INF));
		dp[0][0] = 0;
		rep(i,n){
			dp[1 << i][i] = base_time[i];
		}
		reps(i,1,n){
			rep(j,1<<n){
				if(__builtin_popcount(j) != i)continue;
				rep(k,n){
					if(~j >> k  & 1)continue;
					//行き先
					rep(l,n){
						if(~j >> l & 1){
							ll best_choice = base_time[l];
							rep(z,n){
								if(j >> z & 1){
									chmin(best_choice,use_time[l][z]);
								}
							}
							chmin(dp[j + (1LL<<l)][l],dp[j][k] + best_choice);
						}
					}
				}
			}
		}
		ll ans = INF;
		rep(i,n){
			chmin(ans,dp[(1LL <<n) - 1][i]);
		}
		cout << ans << endl;

	}
}
accuracy: 1
time_ms: 1880
memory_kb: 13928
final_rank: 20