#include <bits/stdc++.h>
using namespace std;
const long long N = 5e3 + 5;
long long n, m, len, now, x, ans;
long long sum[N], a[N], l[N], r[N], maxn[N];
signed main() {
  scanf("%lld%lld", &n, &m);
  for (register long long i = 1; i <= n; ++i) {
    scanf("%lld", &len);
    for (register long long j = 1; j <= len; ++j)
      scanf("%lld", &a[j]), sum[i] += a[j];
    now = a[1];
    l[i] = now;
    for (register long long j = 2; j <= len; ++j) {
      now += a[j];
      l[i] = max(l[i], now);
    }
    now = a[len];
    r[i] = now;
    for (register long long j = len - 1; j >= 1; --j) {
      now += a[j];
      r[i] = max(r[i], now);
    }
    now = a[1];
    maxn[i] = now;
    if (now < 0) now = 0;
    for (register long long j = 2; j <= len; ++j) {
      now += a[j];
      maxn[i] = max(maxn[i], now);
      if (now < 0) now = 0;
    }
  }
  scanf("%lld", &x);
  now = r[x];
  ans = maxn[x];
  for (register long long i = 2; i <= m; ++i) {
    scanf("%lld", &x);
    ans = max(ans, maxn[x]);
    ans = max(ans, now + l[x]);
    now += sum[x];
    now = max(now, r[x]);
  }
  printf("%lld\n", ans);
  return 0;
}
