#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
int maxsubarray(vector<int> v) {
  int n = v.size();
  int mx = -1 * OO, sum = 0;
  for (int i = 0; i < n; ++i) {
    mx = max(mx, v[i]);
    sum += v[i];
    if (sum < 0)
      sum = 0;
    else
      mx = max(mx, sum);
  }
  return mx;
}
int maxleft(vector<int> v) {
  int mx = 0, sum = 0, n = v.size();
  for (int i = 0; i < n; ++i) {
    sum += v[i];
    mx = max(mx, sum);
  }
  return mx;
}
int maxright(vector<int> v) {
  int mx = 0, sum = 0, n = v.size();
  reverse(v.begin(), v.end());
  for (int i = 0; i < n; ++i) {
    sum += v[i];
    mx = max(mx, sum);
  }
  return mx;
}
int totsum(vector<int> v) {
  int sum = 0, n = v.size();
  for (int i = 0; i < n; ++i) {
    sum += v[i];
  }
  return sum;
}
int lft[55], rght[55], tot[55], mxi[55];
int main() {
  int n, m, x, val;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    vector<int> v;
    for (int j = 0; j < x; ++j) {
      scanf("%d", &val);
      v.push_back(val);
    }
    lft[i] = maxleft(v), rght[i] = maxright(v), tot[i] = totsum(v),
    mxi[i] = maxsubarray(v);
  }
  long long cur = 0;
  long long best = (long long)(-1e18);
  while (m--) {
    int i;
    scanf("%d", &i);
    i--;
    best = max(best, 1LL * mxi[i]);
    if (cur) best = max(best, cur + lft[i]);
    cur = max(0ll, max(1LL * rght[i], cur + tot[i]));
  }
  cout << best << endl;
  return 0;
}
