#include <bits/stdc++.h>
using namespace std;
typedef struct cell {
  long long mx, pref, suf, total;
} cell;
int tam[55], vet[300000];
long long sum[55], preffix[55], suffix[55], best[55], v[300000];
long long getMax(int n) {
  long long ret = -1000000000000000000LL, aux = 0LL;
  for (int i = 0; i < n; i++) {
    aux += v[i];
    ret = max(ret, aux);
    if (aux < 0) aux = 0LL;
  }
  return ret;
}
cell solve(int b, int e) {
  cell r;
  if (b == e) {
    r.mx = best[vet[b]];
    r.total = sum[vet[b]];
    r.pref = preffix[vet[b]];
    r.suf = suffix[vet[b]];
    return r;
  }
  int m = (b + e) / 2;
  cell p1, p2;
  p1 = solve(b, m);
  p2 = solve(m + 1, e);
  r.mx = max(max(p1.mx, p2.mx), p1.suf + p2.pref);
  r.total = p1.total + p2.total;
  r.pref = max(p1.pref, p1.total + p2.pref);
  r.suf = max(p2.suf, p1.suf + p2.total);
  return r;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> tam[i];
    for (int j = 0; j < tam[i]; j++) {
      cin >> v[j];
    }
    best[i] = getMax(tam[i]);
    sum[i] = 0LL;
    preffix[i] = suffix[i] = -1000000000000000000LL;
    for (int j = 0; j < tam[i]; j++) {
      sum[i] += v[j];
      preffix[i] = max(preffix[i], sum[i]);
    }
    sum[i] = 0LL;
    for (int j = tam[i] - 1; j >= 0; j--) {
      sum[i] += v[j];
      suffix[i] = max(suffix[i], sum[i]);
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> vet[i];
    vet[i]--;
  }
  cell ans = solve(0, m - 1);
  cout << ans.mx << endl;
  return 0;
}
