#include <bits/stdc++.h>
using namespace std;
struct node {
  long long int res;
  long long int m_pre;
  long long int m_suf;
  long long int tot;
};
node v[51];
void calc(vector<long long int> &a, long long int i) {
  long long int mn = 0, pre = a[1], suf = a[a.size() - 1], to = 0, re = a[1];
  for (long long int i = 1; i < a.size(); i++) {
    to += a[i];
    pre = max(pre, to);
    re = max(re, to - mn);
    mn = min(mn, to);
  }
  long long int sd = 0;
  for (long long int i = a.size() - 1; i >= 1; i--) {
    sd += a[i];
    suf = max(suf, sd);
  }
  v[i].tot = to;
  v[i].res = re;
  v[i].m_suf = suf;
  v[i].m_pre = pre;
}
long long int ind[250001];
node tree[4 * 250010];
void build(long long int l, long long int r, long long int i) {
  if (l == r) {
    tree[i] = v[ind[l]];
  } else {
    long long int mid = (l + r) / 2;
    build(l, mid, 2 * i);
    build(mid + 1, r, 2 * i + 1);
    tree[i].tot = tree[2 * i].tot + tree[2 * i + 1].tot;
    tree[i].res = max(tree[2 * i].res, tree[2 * i + 1].res);
    tree[i].res = max(tree[i].res, tree[2 * i].m_suf + tree[2 * i + 1].m_pre);
    tree[i].m_suf =
        max(tree[2 * i + 1].m_suf, tree[2 * i + 1].tot + tree[2 * i].m_suf);
    tree[i].m_pre =
        max(tree[2 * i].m_pre, tree[2 * i + 1].m_pre + tree[2 * i].tot);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    long long int l;
    cin >> l;
    vector<long long int> a(l + 1, 0);
    for (long long int j = 1; j <= l; j++) {
      cin >> a[j];
    }
    calc(a, i);
  }
  for (long long int i = 1; i <= m; i++) {
    cin >> ind[i];
  }
  build(1, m, 1);
  cout << tree[1].res;
  return 0;
}
