#include <bits/stdc++.h>
using namespace std;
template <class T>
int len(const T &c) {
  return (int)c.size();
}
template <class T>
void cmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
void cmax(T &a, T b) {
  if (b > a) a = b;
}
int cnt = 0;
class segment {
 public:
  long long lsum, rsum, sum, ans;
};
segment tree[699999 + 2];
int lsum(vector<int> v) {
  int m = v[0], msf = v[0];
  for (int i(1), _b(len(v)); i < _b; ++i) {
    msf += v[i];
    cmax(m, msf);
  }
  return m;
}
int rsum(vector<int> v) {
  reverse(v.begin(), v.end());
  int m = v[0], msf = v[0];
  for (int i(1), _b(len(v)); i < _b; ++i) {
    msf += v[i];
    cmax(m, msf);
  }
  return m;
}
int sum(vector<int> v) { return accumulate(v.begin(), v.end(), 0); }
int ans(vector<int> v) {
  int m = v[0], msf = v[0];
  for (int i(1), _b(len(v)); i < _b; ++i) {
    if (msf < 0 && v[i] >= 0)
      msf = v[i];
    else {
      msf += v[i];
      if (msf < 0) msf = v[i];
    }
    cmax(m, msf);
  }
  return m;
}
vector<segment> v;
vector<int> mm;
void init(int node, int l, int r) {
  cmax(cnt, node);
  if (l == r) {
    tree[node].lsum = v[mm[l]].lsum;
    tree[node].rsum = v[mm[l]].rsum;
    tree[node].sum = v[mm[l]].sum;
    tree[node].ans = v[mm[l]].ans;
    return;
  }
  int mid = (l + r) / 2;
  init(node * 2, l, mid);
  init(node * 2 + 1, mid + 1, r);
  tree[node].ans =
      max(tree[node * 2].sum + tree[node * 2 + 1].lsum,
          max(tree[node * 2 + 1].sum + tree[node * 2].rsum,
              max(tree[node * 2].sum + tree[node * 2 + 1].sum,
                  max(tree[node * 2].ans, tree[node * 2 + 1].ans))));
  tree[node].ans =
      max(tree[node].ans, max(tree[node * 2].sum, tree[node * 2 + 1].sum));
  tree[node].ans =
      max(tree[node].ans, tree[node * 2].rsum + tree[node * 2 + 1].lsum);
  tree[node].lsum = max(tree[node * 2].sum + tree[node * 2 + 1].lsum,
                        max(tree[node * 2].lsum, tree[node * 2].sum));
  tree[node].rsum = max(tree[node * 2].rsum + tree[node * 2 + 1].sum,
                        max(tree[node * 2 + 1].rsum, tree[node * 2 + 1].sum));
  tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
int main() {
  int n, m, t, l;
  vector<int> tem;
  cin >> n >> m;
  v = vector<segment>(n);
  for (int i(0), _n(n); i < _n; ++i) {
    cin >> l;
    tem.clear();
    for (int j(0), _n(l); j < _n; ++j) {
      cin >> t;
      tem.push_back(t);
    }
    v[i].lsum = lsum(tem);
    v[i].rsum = rsum(tem);
    v[i].sum = sum(tem);
    v[i].ans = ans(tem);
  }
  for (int i(0), _n(m); i < _n; ++i) {
    cin >> t;
    mm.push_back(--t);
  }
  init(1, 0, m - 1);
  cout << tree[1].ans << endl;
}
