#include <cstdio>
#include <iostream>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
    freopen("run.in", "r", stdin);
    freopen("run.out", "w", stdout);

    int n, k;
    cin >> n >> k;
    vector<int> a(k);
    for (int i = 0; i < k; i++) cin >> a[i];
    vector<vector<int> > b(n + 1, vector<int>(n + 1));
    for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) cin >> b[i][j];

    ll ans = 0;
    vector<int> was(n + 1);
    int last = 0, c = 0;
    for (int it = 0; it < n; it++) {
        int v = -1;
        for (int i = 1; i <= n; i++) {
            if (!was[i] && (v == -1 || b[last][i] < b[last][v])) {
                v = i;
            }
        }
        was[v] = 1;
        ans += b[last][v];
        last = v;
        if (--a[c] == 0) {
            c++;
            ans += b[last][0];
            last = 0;
        }
    }
    cout << ans << endl;


    return 0;
}