#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, k, t[5005], i, x, l;
  long long s, ans, s1, s2, a[100], b[100], c[100], sum[100];
  cin >> n >> m;
  for (k = 1; k <= n; k++) {
    cin >> l;
    for (i = 1; i <= l; i++) scanf("%I64d", &t[i]);
    a[k] = t[1];
    c[k] = t[1];
    s1 = t[1];
    s2 = t[1];
    if (s2 < 0) s2 = 0;
    for (i = 2; i <= l; i++) {
      s1 += t[i];
      s2 += t[i];
      if (s1 > a[k]) a[k] = s1;
      if (s2 > c[k]) c[k] = s2;
      if (s2 < 0) s2 = 0;
    }
    sum[k] = s1;
    s1 = t[l];
    b[k] = t[l];
    for (i = l - 1; i >= 1; i--) {
      s1 += t[i];
      if (s1 > b[k]) b[k] = s1;
    }
  }
  scanf("%I64d", &x);
  s = b[x];
  ans = c[x];
  if (s < 0) s = 0;
  for (i = 2; i <= m; i++) {
    scanf("%I64d", &x);
    if (c[x] > ans) ans = c[x];
    if (s + a[x] > ans) ans = s + a[x];
    if (s + sum[x] > b[x])
      s = s + sum[x];
    else
      s = b[x];
    if (s < 0) s = 0;
  }
  cout << ans << endl;
}
