#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:16000000")
#pragma warning(disable : 4996)
const int inf = 1 << 25;
const double eps = 1e-9;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > b(n);
  for (int i = (0); i < (n); ++i) {
    int sz;
    cin >> sz;
    b[i].resize(sz);
    for (int j = (0); j < (sz); ++j) cin >> b[i][j];
  }
  vector<int> a(m);
  for (int i = (0); i < (m); ++i) cin >> a[i], --a[i];
  vector<long long> finish(n);
  vector<long long> allSum(n);
  vector<long long> begin(n);
  vector<int> h(n);
  vector<long long> f(m);
  for (int i = (0); i < (n); ++i) {
    int m = b[i].size();
    vector<int> ps(m + 1);
    ps[0] = 0;
    for (int j = (1); j < (m + 1); ++j) ps[j] = ps[j - 1] + b[i][j - 1];
    allSum[i] = ps[m];
    begin[i] = *max_element(ps.begin(), ps.end());
    finish[i] = ps[m] - *min_element(ps.begin(), ps.begin() + m);
    int mn = 0;
    h[i] = -inf;
    for (int j = (1); j < (m + 1); ++j)
      h[i] = max<long long>(h[i], ps[j] - mn), mn = min(mn, ps[j]);
  }
  f[0] = finish[a[0]];
  long long ans = h[a[0]];
  for (int n = (1); n < ((int)f.size()); ++n) {
    f[n] = max(finish[a[n]], f[n - 1] + allSum[a[n]]);
    ans = max(ans, f[n - 1] + begin[a[n]]);
    ans = max<long long>(ans, h[a[n]]);
  }
  for (int i = (0); i < (m); ++i) ans = max(ans, f[i]);
  cout << ans << endl;
  return 0;
}
