#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
ll dis[25];
int inq[25],pre[25],prv[25];
set <pair <int,int> > st[25][25];
int c[100005][25],dir[25][100005],deg[25],n,k;
int spfa(){
for(int i = 0;i <= k;i ++){
dis[i] = LLINF; inq[i] = 0;
}
queue <int> q; q.push(0);
inq[0] = 1; dis[0] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for(int v = 0;v <= k;v ++){
if(st[u][v].size()){
auto [w,id] = *st[u][v].begin();
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
prv[v] = u; pre[v] = id;
if(!inq[v]){
inq[v] = 1;
q.push(v);
}
}
}
}
}
int id = -1;
for(int i = 1;i <= k;i ++){
if(deg[i] > 0 && (id == -1 || dis[i] < dis[id])) id = i;
}
return id;
}
void solve(){
cin >> n >> k;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= k;j ++){
cin >> c[i][j];
}
c[i][0] = INF;
}
for(int i = 1;i <= k;i ++) cin >> deg[i];
for(int i = 1;i <= n;i ++){
dir[0][i] = 1;
for(int j = 1;j <= k;j ++){
st[0][j].insert({c[i][j] - c[i][0],i});
}
}
ll ans = 0;
for(int ti = 1;ti <= n;ti ++){
// for(int i = 0;i <= k;i ++){
// for(int j = 0;j <= k;j ++){
// if(st[i][j].size()) cout << st[i][j].begin()->first << ',' << st[i][j].begin()->second << ' ';
// else cout << "0,0 ";
// }
// cout << endl;
// }
int v = spfa();
deg[v] --;
ans += dis[v];
for(int i = 1;i <= k;i ++) assert(deg[i] >= 0);
if(n >= 20 && ti >= 990){
int suma = 0;
suma += deg[i];
cout << ti << ' ' << suma << endl;
}
while(v){
int u = prv[v],w = pre[v];
// u -> w -> v
for(int i = 0;i <= k;i ++){
if(i != u){
if(!dir[i][w]) st[u][i].erase({c[w][i] - c[w][u],w});
else st[i][u].insert({c[w][u] - c[w][i],w});
}
}
for(int i = 0;i <= k;i ++){
if(i != v){
if(dir[i][w]) st[i][v].erase({c[w][v] - c[w][i],w});
else st[v][i].insert({c[w][i] - c[w][v],w});
}
}
dir[u][w] ^= 1; dir[v][w] ^= 1;
v = u;
}
}
cout << ans + 1ll * n * INF << '\n';
}
int main(){
ios::sync_with_stdio(false);
solve();
return 0;
}