#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #include using namespace std; #include using namespace atcoder; // #include // using namespace boost; // using rat = rational; using mint = modint998244353; // using mint = modint1000000007; // using mint = mint; using ll = long long; using ld = long double; using ull = uint64_t; using pll = pair; using vll = vector; using vvll = vector; using vvvll = vector; using vpll = vector; using vvpll = vector; using vm = vector; using vvm = vector; using vvvm = vector; using vstr = vector; #define v(T) vector #define vv(T) vector> #define vvv(T) vector>> #define vvvv(T) vector>>> istream &operator>>(istream &is, mint &a){ll tmp; is >> tmp; a = tmp; return is;} ostream &operator<<(ostream &os, const mint &a){ os << a.val(); return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, vector &vec){ for(T &e : vec){is >> e;} return is; } template ostream &operator<<(ostream &os, const vector &vec) { for(int i = 0; i < (int)vec.size(); i++) { os << vec[i] << (i + 1 != (int)vec.size() ? " " : ""); } return os; } template constexpr auto min (T... a) { return min(initializer_list>{a...}); } template constexpr auto max (T... a) { return max(initializer_list>{a...}); } template using pqg = priority_queue, greater>; template T opmin(T x, T y) { return min(x, y); } template T einf() { return numeric_limits::max(); } template T opmax(T x, T y) { return max(x, y); } template T eminf() { return numeric_limits::min(); } template T opsum(T x, T y) { return x + y; } template T ezero() { return (T)0; } // #define maxseg(T) segtree // #define minseg(T) segtree // #define sumseg(T) segtree template using minseg = segtree, einf>; template using maxseg = segtree, eminf>; template using sumseg = segtree, ezero>; // template struct v : vector { using vector :: vector; }; // template struct vv : vector> { using vector> :: vector; }; // template struct vvv : vector> { using vector> :: vector; }; template inline bool chmin(T& a, T b) {if(a > b){a = b; return true;} else {return false;}}; template inline bool chmax(T& a, T b) {if(a < b){a = b; return true;} else {return false;}}; #define rep(i,n) for(ll i = 0; i < (ll)(n); i++) #define repr(i,n) for(ll i = (ll)(n) - 1; i >= 0; i--) #define REP(i, l, r) for(ll i = (ll)l; i <= (ll)(r); i++) #define REPR(i, l, r) for(ll i = (ll)r; i >= (ll)(l); i--) const ll inf = (1 << 30); const ll INF = (1LL << 60); const vector> DIJ = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}}; void out(){cout<<'\n';} template void out(const T& a, const Ts&... b){ cout< void outf(const T& a, const Ts&... b){ cout< void outp(pair a){ out((a).first, (a).second); } template void outpf(pair a){ outf((a).first, (a).second); } template void outv(T a){rep(i, (a).size()){ cout << (a)[i] << " "; } cout << endl;} template void outvL(T a){rep(i, (a).size()){out((a)[i]);} cout << flush; } // template void outvv(T a){rep(i, a.size()){ rep(j, a.at(i).size()){cout << a.at(i).at(j) << " "; } cout << endl; }} // template void outvp(T a){rep(i, a.size()){ out2(a.at(i).first, a.at(i).second); }} void setpre(int a){cout << fixed << setprecision(a);} #define outN out("No") #define outY out("Yes") #define outYN(flag) out(flag ? "Yes" : "No") #define dame(...) {outf(__VA_ARGS__);return 0;} template void read(vector& vec){ for(int i = 0; i < (int)vec.size(); i++) { cin >> vec[i]; } } template void read(T&... a){(cin >> ... >> a);} #define readll(...) ll __VA_ARGS__; read(__VA_ARGS__) #define readvll(a, n) vector a(n); read(a) #define readvt(type, a, n) vector a(n); read(a) #define readvll2(a, b, n) vector a(n), b(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi] #define readvll3(a, b, c, n) vector a(n), b(n), c(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi] >> (c)[lopi] #define readstr(...) string __VA_ARGS__; read(__VA_ARGS__) #define readundirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1); G[b-1].push_back(a-1);} #define readdirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1);} #define readundirwghG(G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1,c); G[b-1].emplace_back(a-1, c);} #define readdirwghG (G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1, c);} #define All(a) (a).begin(), (a).end() template inline void sortr(T& a){ sort((a).rbegin(), (a).rend()); } template inline vector argsort(T V, bool rev = false){vector res(V.size()); iota(res.begin(), res.end(), 0); sort(res.begin(), res.end(), [&](int x, int y){if(!rev){return V[x] < V[y];}else{return V[x] > V[y];}}); return res;} template inline void sort_by_idx(T& V, vector& I){assert(V.size() == I.size()); T tmpv = V; for(int loopi = 0; loopi < (int)I.size(); loopi++){V[loopi] = tmpv[I.at(loopi)];}} template inline void sortp(vector& v1, vector& v2, bool rev1 = false, int rev2 = false){assert(v1.size() == v2.size()); vector I(v1.size()); iota(I.begin(), I.end(), 0); sort(I.begin(), I.end(), [&](const int x, const int y){if(v1[x] != v1[y]){return (bool)(rev1 ^ (v1[x] < v1[y]));}else{if(v2[x]==v2[y]){return false;} return (bool)(rev2 ^ (v2[x] < v2[y]));}}); sort_by_idx(v1, I); sort_by_idx(v2, I);} template T POW(T x, ll n) {T ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;} ll powll(ll x, ll n){ll ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;} inline ll divceil(ll x, ll y) { if(x >= 0) {return(x / y + (ll)(x % y != 0)); } else { return -((-x) / y); } } inline ll divfloor(ll x, ll y) { if(x >= 0) { return x/y; } else { return -((-x)/y + (ll)((-x) % y != 0)); } } inline bool inLR(ll x, ll L, ll R){ return (L <= x && x < R); } inline bool inRect(ll pos_x, ll pos_y, ll rect_H, ll rect_W, ll rect_h = 0, ll rect_w = 0){ return (rect_h <= pos_x && pos_x < rect_H && rect_w <= pos_y && pos_y < rect_W); } template vector &operator++(vector &v) {for(auto &e : v){e++;} return v;} template vector operator++(vector &v, signed) {auto res=v; for(auto &e : v){e++;} return res;} template vector &operator--(vector &v) {for(auto &e : v){e--;} return v;} template vector operator--(vector &v, signed) {auto res=v; for(auto &e : v){e--;} return res;} template vector operator+(const vector &x, const vector &y) { assert(x.size() == y.size()); vector ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] + y[i];} return ret; } template vector operator-(const vector &x, const vector &y) { assert(x.size() == y.size()); vector ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] - y[i];} return ret; } template pair operator+(const pair &x, const pair &y) { return make_pair(x.first + y.first, x.second + y.second); } template pair operator-(const pair &x, const pair &y) { return make_pair(x.first - y.first, x.second - y.second); } template void operator+=(pair &x, pair &y) { x = x + y; } template void operator-=(pair &x, pair &y) { x = x - y; } template size_t HashCombine(const size_t seed,const T &v){ return seed^(std::hash()(v)+0x9e3779b9+(seed<<6)+(seed>>2)); } template struct std::hash>{ size_t operator()(const std::pair &keyval) const noexcept { return HashCombine(std::hash()(keyval.first), keyval.second); } }; template struct std::hash>{ size_t operator()(const std::vector &keyval) const noexcept { size_t s=0; for (auto&& v: keyval) s=HashCombine(s,v); return s; } }; template struct HashTupleCore{ template size_t operator()(const Tuple &keyval) const noexcept{ size_t s=HashTupleCore()(keyval); return HashCombine(s,std::get(keyval)); } }; template <> struct HashTupleCore<0>{ template size_t operator()(const Tuple &keyval) const noexcept{ return 0; } }; template struct std::hash>{ size_t operator()(const tuple &keyval) const noexcept { return HashTupleCore>::value>()(keyval); } }; int main() { std::cin.tie(nullptr), std::ios_base::sync_with_stdio(false); readll(N, M, K); readvll(A, K); vvll T(N, vll(N)); read(T); assert(1 <= N && N <= 16); assert(1 <= M && M <= min(6LL, N)); assert(1 <= K && K <= N); rep(i, K) { if(i > 0) assert(A[i - 1] < A[i]); assert(1 <= A[i] && A[i] <= N); } rep(i, N) rep(j, N) { if(i == j) assert(T[i][j] == 0); else assert(1 <= T[i][j] && T[i][j] <= 100000); } rep(i, N) rep(j, N) rep(k, N) assert(T[i][j] <= T[i][k] + T[k][j]); A--; vvvll dp(1LL << N, vvll(N, vll(M + 1, INF))); rep(i, N) dp[1LL << i][i][1] = 0; REP(S, 1, (1LL << N) - 1) rep(i, N) { if(!((S >> i) & 1)) continue; REP(j, 1, M) { rep(k, N) { ll nxt = j + !((S >> k) & 1); if(nxt <= M) chmin(dp[S | (1LL << k)][k][nxt], dp[S][i][j] + T[i][k]); } } } ll ans = INF; for(auto a : A) { rep(S, 1LL << N) chmin(ans, dp[S][a][M]); } out(ans); }