#include <bits/stdc++.h>
using namespace std;
long long mx;
long long suff[55];
long long pref[55];
long long lmx[55];
long long sum[55];
long long ara[55][5005];
long long seq[250005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> ara[i][0];
    for (int j = 1; j <= ara[i][0]; j++) scanf("%I64d", &ara[i][j]);
    long long s = 0;
    long long smx = -6000000;
    for (int j = 1; j <= ara[i][0]; j++) {
      s += ara[i][j];
      if (s > smx) {
        smx = s;
      }
      sum[i] += ara[i][j];
    }
    pref[i] = smx;
    s = 0;
    smx = -6000000;
    for (int j = ara[i][0]; j >= 1; j--) {
      s += ara[i][j];
      if (s > smx) {
        smx = s;
      }
    }
    suff[i] = smx;
    smx = -6000000;
    s = 0;
    for (int j = 1; j <= ara[i][0]; j++) {
      s += ara[i][j];
      smx = max(smx, s);
      if (s < 0) {
        s = 0;
      }
    }
    lmx[i] = smx;
  }
  mx = -1000000000000000;
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%I64d", &x);
    mx = max(mx, lmx[x]);
    seq[i] = x;
  }
  int l = 1;
  int r = 1;
  long long cursm = 0;
  while (r <= m) {
    if (l == r) {
      if (suff[seq[l]] < 0) {
        l++;
        r++;
      } else {
        cursm = suff[seq[l]];
        mx = max(mx, cursm);
        r++;
      }
    } else {
      cursm += pref[seq[r]];
      mx = max(mx, cursm);
      if (cursm < 0) {
        l = r;
      } else {
        cursm -= pref[seq[r]];
        cursm += sum[seq[r]];
        mx = max(mx, cursm);
        if (cursm < 0 || cursm <= suff[seq[r]])
          l = r;
        else
          r++;
      }
    }
  }
  cout << mx << endl;
  return 0;
}
