#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long l[51];
long long sum[51], psum[51], ssum[51], a[51][5000], best[51];
long long b[250010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cerr.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> l[i];
    for (int j = 0; j < l[i]; j++) {
      cin >> a[i][j];
      sum[i] += a[i][j];
    }
    long long cur;
    cur = psum[i] = a[i][0];
    for (int j = 1; j < l[i]; j++) {
      cur += a[i][j];
      psum[i] = max(psum[i], cur);
    }
    cur = ssum[i] = a[i][l[i] - 1];
    for (int j = l[i] - 2; j >= 0; j--) {
      cur += a[i][j];
      ssum[i] = max(ssum[i], cur);
    }
    cur = a[i][0];
    long long tmp = cur;
    if (cur < 0) cur = 0;
    for (int j = 1; j < l[i]; j++) {
      cur += a[i][j];
      tmp = max(tmp, cur);
      if (cur < 0) cur = 0;
    }
    best[i] = tmp;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  long long res = max(max(ssum[b[1]], psum[b[1]]), sum[b[1]]);
  res = max(res, best[b[1]]);
  long long cur = max(sum[b[1]], ssum[b[1]]);
  if (cur < 0) cur = 0;
  for (int i = 2; i <= m; i++) {
    res = max(res, max(cur + psum[b[i]], cur + sum[b[i]]));
    cur += sum[b[i]];
    res = max(res, best[b[i]]);
    cur = max(max(ssum[b[i]], sum[b[i]]), cur);
    if (cur < 0) cur = 0;
  }
  cout << res << "\n";
  return 0;
}
