#include <bits/stdc++.h>
using namespace std;
void readSequence(vector<long long> &ans);
struct package {
  long long bestpref, bestsuf, best, total;
  long long largest;
  package() {}
  package(vector<long long> &v) {
    int i;
    long long pref, suf, cur;
    total = 0;
    largest = -1000000;
    for (i = 0; i < v.size(); i++) {
      total += v[i];
      largest = (((largest) > (v[i])) ? (largest) : (v[i]));
    }
    bestpref = pref = 0;
    for (i = 0; i < v.size(); i++) {
      pref += v[i];
      bestpref = (((pref) > (bestpref)) ? (pref) : (bestpref));
    }
    suf = bestsuf = 0;
    for (i = v.size() - 1; i >= 0; i--) {
      suf += v[i];
      bestsuf = (((suf) > (bestsuf)) ? (suf) : (bestsuf));
    }
    cur = 0;
    best = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i] + cur >= 0) {
        cur += v[i];
        best = (((best) > (cur)) ? (best) : (cur));
      } else {
        cur = 0;
      }
    }
  }
};
vector<long long> big;
vector<long long> small[64];
struct package details[64];
int N;
void input() {
  int m;
  cin >> N >> m;
  for (int i = 1; i <= N; i++) {
    readSequence(small[i]);
  }
  big.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> big[i];
  }
}
struct package combine(struct package &a, struct package &b) {
  struct package c;
  c.bestpref =
      (((a.bestpref) > (a.total + b.bestpref)) ? (a.bestpref)
                                               : (a.total + b.bestpref));
  c.bestsuf = (((b.bestsuf) > (b.total + a.bestsuf)) ? (b.bestsuf)
                                                     : (b.total + a.bestsuf));
  c.total = a.total + b.total;
  c.best = (((a.best) > (b.best)) ? (a.best) : (b.best));
  c.best = (((c.best) > (a.bestsuf + b.bestpref)) ? (c.best)
                                                  : (a.bestsuf + b.bestpref));
  c.largest = (((a.largest) > (b.largest)) ? (a.largest) : (b.largest));
  return c;
}
void solve() {
  vector<struct package> L[2];
  int i;
  for (i = 1; i <= N; i++) details[i] = package(small[i]);
  for (i = 0; i < big.size(); i++) {
    L[0].push_back(details[big[i]]);
  }
  vector<struct package> &old = L[0];
  vector<struct package> &cur = L[1];
  vector<struct package> &tmp = L[0];
  while (1) {
    if (old.size() == 1) break;
    cur.clear();
    for (i = 0; i < old.size() - 1; i += 2) {
      cur.push_back(combine(old[i], old[i + 1]));
    }
    if (i < old.size()) {
      cur.push_back(old[i]);
    }
    tmp = cur;
    cur = old;
    old = tmp;
  }
  if (old[0].best == 0 && old[0].largest < 0) {
    cout << old[0].largest << "\n";
  } else
    cout << old[0].best << "\n";
}
int main() {
  input();
  solve();
  return 0;
}
void readSequence(vector<long long> &ans) {
  int n;
  cin >> n;
  ans.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> ans[i];
  }
}
