#include <bits/stdc++.h>
using namespace std;
struct mas {
  long long sum, maxSum, prefMax, sufMax;
  mas() {}
  mas(mas a, mas b) {
    sum = a.sum + b.sum;
    prefMax = max(a.prefMax, a.sum + b.prefMax);
    sufMax = max(b.sufMax, b.sum + a.sufMax);
    maxSum = max(a.sufMax + b.prefMax,
                 max(max(prefMax, sufMax), max(a.maxSum, b.maxSum)));
  }
  mas(int n, int *t) {
    maxSum = prefMax = sufMax = -1e18;
    long long s = 0, cur = sum = 0;
    for (int i = 1; i <= n; i++) {
      prefMax = max(prefMax, sum += t[i]);
      sufMax = max(sufMax, s += t[n - i + 1]);
      maxSum = max(maxSum, cur += t[i]);
      cur = max(cur, 0ll);
    }
  }
} a[250010], b[64];
mas solve(int l, int r) {
  if (l == r) {
    return a[l];
  }
  mas L = solve(l, (l + r) / 2);
  mas R = solve((l + r) / 2 + 1, r);
  mas res(L, R);
  return res;
}
int main() {
  int m, n;
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    static int l, t[5002];
    cin >> l;
    for (int j = 1; j <= l; j++) {
      cin >> t[j];
    }
    b[i] = mas(l, t);
  }
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    a[i] = b[p];
  }
  cout << solve(1, n).maxSum;
  return 0;
}
