#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include using namespace std; template struct cLtraits_identity{ using type = T; } ; template using cLtraits_try_make_signed = typename conditional< is_integral::value, make_signed, cLtraits_identity >::type; template struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed::type; using tT = typename cLtraits_try_make_signed::type; using type = typename common_type::type; } ; void*wmem; char memarr[96000000]; template inline auto min_L(S a, T b) -> typename cLtraits_common_type::type{ return (typename cLtraits_common_type::type) a <= (typename cLtraits_common_type::type) b ? a : b; } template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } template void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } template void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){ int i; pair >*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second.first = b[i]; arr[i].second.second = c[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second.first; c[i] = arr[i].second.second; } } struct Rand{ unsigned x; unsigned y; unsigned z; unsigned w; Rand(void){ x=123456789; y=362436069; z=521288629; w=(unsigned)time(NULL); } Rand(unsigned seed){ x=123456789; y=362436069; z=521288629; w=seed; } inline unsigned get(void){ unsigned t; t = (x^(x<<11)); x=y; y=z; z=w; w = (w^(w>>19))^(t^(t>>8)); return w; } inline double getUni(void){ return get()/4294967296.0; } inline int get(int a){ return (int)(a*getUni()); } inline int get(int a, int b){ return a+(int)((b-a+1)*getUni()); } inline long long get(long long a){ return(long long)(a*getUni()); } inline long long get(long long a, long long b){ return a+(long long)((b-a+1)*getUni()); } inline double get(double a, double b){ return a+(b-a)*getUni(); } inline int getExp(int a){ return(int)(exp(getUni()*log(a+1.0))-1.0); } inline int getExp(int a, int b){ return a+(int)(exp(getUni()*log((b-a+1)+1.0))-1.0); } } ; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void my_putchar_unlocked(const int k){ putchar_unlocked(k); } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned x){ int s=0; char f[10]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned long long x){ int s=0; char f[21]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int WRITER_DOUBLE_DIGIT = 15; inline int writerDigit_double(){ return WRITER_DOUBLE_DIGIT; } inline void writerDigit_double(int d){ WRITER_DOUBLE_DIGIT = d; } inline void wt_L(double x){ const int d = WRITER_DOUBLE_DIGIT; int k; int r; double v; if(x!=x || (x==x+1 && x==2*x)){ my_putchar_unlocked('E'); my_putchar_unlocked('r'); my_putchar_unlocked('r'); return; } if(x < 0){ my_putchar_unlocked('-'); x = -x; } x += 0.5 * pow(0.1, d); r = 0; v = 1; while(x >= 10*v){ v *= 10; r++; } while(r >= 0){ r--; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; v *= 0.1; my_putchar_unlocked(k + '0'); } if(d > 0){ my_putchar_unlocked('.'); v = 1; for(r=(0);r<(d);r++){ v *= 0.1; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; my_putchar_unlocked(k + '0'); } } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } inline void wt_L(string &x){ int i=0; for(i=0;x[i]!='\0';i++){ my_putchar_unlocked(x[i]); } } inline int BIT_popcount_L(const int x){ return __builtin_popcount(x); } inline int BIT_popcount_L(const long long x){ return __builtin_popcountll(x); } template inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } template inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } struct unionFind{ int*d; int N; int M; inline void malloc(const int n){ d = (int*)std::malloc(n*sizeof(int)); M = n; } inline void malloc(const int n, const int fg){ d = (int*)std::malloc(n*sizeof(int)); M = n; if(fg){ init(n); } } inline void free(void){ std::free(d); } inline void walloc(const int n, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; } inline void walloc(const int n, const int fg, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; if(fg){ init(n); } } inline void init(const int n){ int i; N = n; for(i=(0);i<(n);i++){ d[i] = -1; } } inline void init(void){ init(M); } inline int get(int a){ int t = a; int k; while(d[t]>=0){ t=d[t]; } while(d[a]>=0){ k=d[a]; d[a]=t; a=k; } return a; } inline int connect(int a, int b){ if(d[a]>=0){ a=get(a); } if(d[b]>=0){ b=get(b); } if(a==b){ return 0; } if(d[a] < d[b]){ d[a] += d[b]; d[b] = a; } else{ d[b] += d[a]; d[a] = b; } return 1; } inline int operator()(int a){ return get(a); } inline int operator()(int a, int b){ return connect(a,b); } inline int& operator[](const int a){ return d[a]; } inline int size(int a){ a = get(a); return -d[a]; } inline int sizeList(int res[]){ int i; int sz=0; for(i=(0);i<(N);i++){ if(d[i]<0){ res[sz++] = -d[i]; } } return sz; } inline int comp(int res[], void *mem = wmem){ int i; int sz=0; int*cnt; walloc1d(&cnt, N, &mem); for(i=(0);i<(N);i++){ cnt[i] = 0; } for(i=(0);i<(N);i++){ cnt[get(i)] = 1; } for(i=(0);i<(N);i++){ if(cnt[i]){ cnt[i] = sz++; } } for(i=(0);i<(N);i++){ res[i] = cnt[get(i)]; } return sz; } } ; long long cReader_ll(long long mn, long long mx, char nx){ int i; int fg = 0; int m = 1; int f = -1; long long res = 0; double tmp = 0; for(;;){ i = my_getchar_unlocked(); if(fg==0 && i=='-'){ fg++; m = -1; } else if('0' <= i && i <= '9'){ fg++; if(f == -1){ f = i - '0'; } res = 10 * res + i - '0'; tmp = 10 * tmp + i - '0'; assert(tmp < 1e20); } else{ break; } } assert(tmp / 2 <= res); assert((m==1 && fg >= 1) || (m==-1 && fg >= 2)); assert(mn <= m * res && m * res <= mx); assert(!(res == 0 && m == -1)); assert(!(res != 0 && f == 0)); assert(!(res == 0 && fg >= 2)); assert(i == nx); return m * res; } void cReader_eof(){ int i; i = my_getchar_unlocked(); assert(i == EOF); } int N; int M; int K; int A[20]; int X[200]; int Y[200]; int Z[200]; long long baka(){ int i; int j; int k; int ind[20]; long long res = 4611686016279904256LL; long long tmp; long long tmp2; long long mat[20][20]; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } int hCmBdyQB; for(hCmBdyQB=(0);hCmBdyQB<(N);hCmBdyQB++){ ind[hCmBdyQB] = hCmBdyQB; } do{ { tmp = 0; for(i=(0);i<(K);i++){ tmp += A[ind[i]]; } for(i=(1);i<(K);i++){ tmp2 = 4611686016279904256LL; for(j=(0);j<(i);j++){ chmin(tmp2, mat[ind[j]][ind[i]]); } tmp += tmp2; } chmin(res, tmp); } } while(next_permutation(ind,ind+N)); return res; } long long dist1[20][1200000]; long long dp1[1200000]; int bt[1200000]; int bc[1200000]; long long solve1(){ int i; int j; int k; long long mat[20][20]; long long res = 4611686016279904256LL; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } for(i=(0);i<(N);i++){ bt[1< mn[i]){ k = i; tmp2 = mn[i]; } } tmp += tmp2; use[k] = 1; cnt++; for(i=(0);i<(N);i++){ if(!use[i]){ chmin(mn[i], A[i] + mat[k][i]); } } } chmin(res, tmp); } return res; } int main(){ wmem = memarr; int i; int j; int k; int m; long long r0; long long r1; long long u1; N = cReader_ll(1, 18, ' '); M = cReader_ll(N-1, N*(N-1)/2, ' '); K = cReader_ll(1, N, '\n'); for(i=(0);i<(N);i++){ A[i] = cReader_ll(1, 1000000000, i==N-1?'\n':' '); } for(i=(0);i<(M);i++){ X[i] = cReader_ll(1, N, ' '); Y[i] = cReader_ll(X[i], N, ' '); Z[i] = cReader_ll(1, 1000000000, '\n'); X[i]--; Y[i]--; } cReader_eof(); for(i=(0);i<(M);i++){ for(j=(i+1);j<(M);j++){ assert(X[i] != X[j] || Y[i] != Y[j]); } } uf2.walloc(20,1); for(i=(0);i<(M);i++){ uf2(X[i],Y[i]); } for(i=(0);i<(N);i++){ assert(uf2(0)==uf2(i)); } wt_L(solve1()); wt_L('\n'); return 0; Rand rnd; unionFind uf; uf.walloc(20); puts("hoge"); for(;;){ int con = 0; N = rnd.get(1,8); K = rnd.get(1,N); M = 0; uf.init(N); while(con < N-1){ i = rnd.get(N); j = rnd.get(N); k = rnd.get(1, 1000000000); if(i==j){ continue; } for(m=(0);m<(M);m++){ if(X[m]==i && Y[m]==j){ goto ydh_YK0A; } } for(m=(0);m<(M);m++){ if(X[m]==j && Y[m]==i){ goto ydh_YK0A; } } con += uf(i,j); arrInsert(M,M,X,i,Y,j,Z,k); ydh_YK0A:; } for(i=(0);i<(N);i++){ A[i] = rnd.get(1, 1000000000); } r0 = baka(); r1 = solve1(); u1 = uso1(); wt_L(r0); wt_L(' '); wt_L(r1); wt_L(' '); wt_L(":"); wt_L(' '); wt_L(u1); wt_L('\n'); assert(r0==r1); } return 0; } // cLay version 20210819-1 [beta] // --- original code --- // int N, M, K, A[20], X[200], Y[200], Z[200]; // // ll baka(){ // int i, j, k, ind[20]; // ll res = ll_inf, tmp, tmp2; // ll mat[20][20]; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] mn[i]){ // k = i; // tmp2 = mn[i]; // } // tmp += tmp2; // use[k] = 1; // cnt++; // rep(i,N) if(!use[i]) mn[i]