#include<bits/stdc++.h>
#define cs const
#define pb push_back
#define rep(i, j, k) for(int i = j; i <= k; i++)
using namespace std;
typedef long long ll;
cs int N = 55;
int n, m;
ll dp[N][N][N][4][4];
int pt[N];
ll a[N], b[N], c[N];
void chkmn(ll &a, ll b){ if(a > b) a = b; }
int F(int z, int p, int k){
	if(p == 0) return a[z] >> k & 1;
	if(p == 1) return a[z] >> k & 1 ^ 1;
	if(p == 2) return b[z] >> k & 1;
	if(p == 3) return b[z] >> k & 1 ^ 1; 
}
ll cost(int l, int r, int p, int q, int k){
	if(l == 0 || r == n + 1) return 0;
	int a = F(l, p, k), b = F(r, q, k);
	return (a ^ b) ? c[k] : 0;
}
int main(){
	#ifdef FSYo
	freopen("1.in", "r", stdin);
	#endif
	cin >> n >> m;
	memset(pt, -1, sizeof(pt));
	rep(i, 1, n){
		scanf("%lld%lld", &a[i], &b[i]); 
		for(int j = m - 1; ~j; j--)
		if((a[i] >> j & 1) ^ (b[i] >> j & 1)){
			pt[i] = j; break;
		}
	}
	rep(i, 0, m - 1) scanf("%lld", &c[i]);
	memset(dp, 0x3f, sizeof(dp));
	rep(i, 0, n)
	for(int j = 0; j < 3; j += 2)
	for(int k = 0; k < 3; k += 2)
	dp[m][i][i + 1][j][k] = 0; ll c;
	for(int k = m - 1; ~k; k--){
		rep(l, 0, n) rep(r, l + 1, n + 1)
		for(int p = 0; p < 3; p += 2)
		for(int q = 0; q < 3; q += 2)
		if((c = dp[k + 1][l][r][p][q]) < 1e18)
		for(int o = 0; o < 2; o++)
		if(!o || k < pt[l]){
			if(o && p == 0 && (a[l] >> k & 1)) continue;
			if(o && p == 2 && (b[l] >> k & 1 ^ 1)) continue;
			for(int e = 0; e < 2; e++)
			if(!e || k < pt[r]){
				if(e && q == 0 && (a[r] >> k & 1)) continue;
				if(e && q == 2 && (b[r] >> k & 1 ^ 1)) continue;
				chkmn(dp[k][l][r][p + o][q + e], c + cost(l, r, p + o, q + e, k));
			}
		}
		rep(len, 2, n + 1) rep(l, 0, n + 1 - len){
			int r = l + len;
			rep(t, l + 1, r - 1)
			rep(o, 0, 3) rep(e, 0, 3)
			for(int z = 1; z <= 3; z += 2)
			chkmn(dp[k][l][r][o][e], dp[k][l][t][o][z] + dp[k][t][r][z][e]);
		}
	}
	rep(len, 2, n + 1)
	rep(l, 0, n + 1 - len){
		int r = l + len;
		rep(t, l + 1, r - 1)
		rep(o, 0, 3) rep(e, 0, 3) rep(z, 0, 3)
		chkmn(dp[0][l][r][o][e], dp[0][l][t][o][z] + dp[0][t][r][z][e]);
	}
	ll ans = 1e18;
	rep(o, 0, 3) rep(e, 0, 3)
	chkmn(ans, dp[0][0][n + 1][o][e]);
	cout << ans; return 0;
}