#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; } 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 rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } 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(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(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } 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; } } ; 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; rd(N); rd(M); rd(K); { int d37S9NUa; for(d37S9NUa=(0);d37S9NUa<(N);d37S9NUa++){ rd(A[d37S9NUa]); } } { int lXxUrvh5; for(lXxUrvh5=(0);lXxUrvh5<(M);lXxUrvh5++){ rd(X[lXxUrvh5]);X[lXxUrvh5] += (-1); rd(Y[lXxUrvh5]);Y[lXxUrvh5] += (-1); rd(Z[lXxUrvh5]); } } int res = solve1(); wt_L(res); 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 o0VCpjj8; } } for(m=(0);m<(M);m++){ if(X[m]==j && Y[m]==i){ goto o0VCpjj8; } } con += uf(i,j); arrInsert(M,M,X,i,Y,j,Z,k); o0VCpjj8:; } 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]