#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;

LL next_combination(LL p)
{
	LL lsb = p & -p;
	LL rem = p + lsb;
	LL rit = rem & ~p;
	return rem|(((rit/lsb)>>1)-1);
}

class ByteLand
{
public:
	int buildCastles(vector<int> road, vector<int> distance, vector<int> castle, int k) 
	{
		int n = road.size();

		// Construct the Graph
		int d[50][50] = {};
		for(int v=0; v<n; ++v)
			for(int u=v+1; u<n; ++u)
				d[v][u] = d[u][v] = 0x3fffffff;
		for(int v=0; v<n; ++v)
			d[v][road[v]] = d[road[v]][v] = min( d[v][road[v]], distance[v] );

		// Warshall-Floyd
		for(int m=0; m<n; ++m)
			for(int i=0; i<n; ++i)
				for(int j=0; j<n; ++j)
					d[i][j] = min(d[i][j], d[i][m]+d[m][j]);

		// Binary Search: the answer is in (L,R]
		int L=-1, R=accumulate(distance.begin(), distance.end(), 0);
		while( R-L > 1 )
			(is_possible((L+R)/2, d, n, castle, k) ? R : L) = (L+R)/2;
		return R;
	}

	bool is_possible( int D, int d[50][50], int n, const vector<int>& c, int k )
	{
		// the set of cities not covered yet
		LL goal = (1LL << n) - 1;
		for(int v=0; v<n; ++v)
			for(int i=0; i<c.size(); ++i)
				if( d[v][c[i]] <= D )
					goal &= ~(1LL << v);

		// the set of cities each city can cover
		set<LL> mask;
		for(int v=0; v<n; ++v) {
			LL m = 0;
			for(int u=0; u<n; ++u)
				if( d[v][u] <= D )
					m |= (1LL << u);
			if( m&goal )
				mask.insert( m&goal );
		}

		// is it possible to cover the goal by at most k elements from mask?
		return canCover(goal, mask, k);
	}

	bool canCover( LL goal, set<LL>& mask, int k )
	{
		// optimizer
		for(bool update=true; update; ) {
			update = false;

			// if *it \subseteq *jt for some jt, then we NEVER use it
			// if *it is the only mask covering some city, we ALWAYS use it
			for(set<LL>::iterator it=mask.begin(); it!=mask.end(); ) {
				bool isSubset = false;
				LL   onlyByMe = *it & goal;
				for(set<LL>::iterator jt=mask.begin(); jt!=mask.end(); ++jt)
					if( it!=jt ) {
						onlyByMe &= ~*jt;
						isSubset |= (*it & *jt & goal) == (*it & goal);
					}

				update |= isSubset | !!onlyByMe;

				if( isSubset )
					mask.erase(it++);
				else if( onlyByMe ) {
					if( --k < 0 )
						return false;
					goal &= ~*it;
					mask.erase(it++);
				}
				else
					++it;
			}

			if( mask.size()<=k || goal==0 )
				return true;
		}

		// exhaustive search
		vector<LL> ms(mask.begin(), mask.end());
		for(LL i=(1LL<<k)-1; i<(1LL<<ms.size()); i=next_combination(i))
		{
			LL gg = goal;
			for(LL j=0; (1LL<<j)<=i; ++j)
				if( i & (1<<j) )
					gg &= ~ms[j];
			if( gg == 0 )
				return true;
		}
		return false;
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arr0[] = {1,2,3,4,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1,1,1,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector <int> Arg2; int Arg3 = 1; int Arg4 = 2; verify_case(0, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_1() { int Arr0[] = {1,2,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 1; verify_case(1, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_2() { int Arr0[] = {0,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 0; verify_case(2, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_3() { int Arr0[] = {0,2,0,0,2,2,8,3,8,7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10,9,1,8,1,3,7,2,8,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {3,4,6}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 3; int Arg4 = 3; verify_case(3, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_4() { int Arr0[] = {1, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5, 10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector <int> Arg2; int Arg3 = 1; int Arg4 = 5; verify_case(4, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() { ByteLand().run_test(-1); }
// END CUT HERE
