#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
long long n, m;
long long l[55];
long long a[55][5005];
long long b[250005];
long long mx[55], pre[55], suf[55], sum[55];
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    cin >> l[i];
    for (long long j = 1; j <= l[i]; ++j) cin >> a[i][j];
  }
  for (long long i = 1; i <= m; ++i) cin >> b[i];
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= l[i]; ++j) sum[i] += a[i][j];
    pre[i] = -1000000000007;
    long long s = 0;
    for (long long j = 1; j <= l[i]; ++j) {
      s += a[i][j];
      pre[i] = max(pre[i], s);
    }
    suf[i] = -1000000000007;
    s = 0;
    for (long long j = l[i]; j >= 1; --j) {
      s += a[i][j];
      suf[i] = max(suf[i], s);
    }
    s = 0;
    bool flag = false;
    long long iamidiot = -1000000000007;
    mx[i] = -1000000000007;
    for (long long j = 1; j <= l[i]; ++j) {
      iamidiot = max(iamidiot, a[i][j]);
      s += a[i][j];
      flag = true;
      if (s < 0) {
        s = 0;
        flag = false;
      }
      if (flag) mx[i] = max(mx[i], s);
    }
    mx[i] = max(mx[i], iamidiot);
  }
  long long ans = -1000000000007;
  long long cur = 0;
  for (long long i = 1; i <= m; ++i) {
    ans = max(ans, mx[b[i]]);
    ans = max(max(ans, cur + pre[b[i]]), max(cur + sum[b[i]], suf[b[i]]));
    cur = max(0LL, max(cur + sum[b[i]], suf[b[i]]));
  }
  cout << ans << endl;
  return 0;
}
