#include <bits/stdc++.h>
using namespace std;
const int N = 25e4 + 2;
struct small {
  int n;
  vector<long long> arr;
  long long total, pref, suf, best;
};
vector<int> indices;
int n, m, l;
vector<small> a;
vector<long long> prefix;
long long dp[N][2];
long long overall = LONG_LONG_MIN;
long long solve(int i, bool start) {
  if (i >= m) return 0;
  if (dp[i][start] != -1) return dp[i][start];
  long long ans = 0;
  int ind = indices[i] - 1;
  overall = max(overall, a[ind].best);
  if (start) {
    long long stop = a[ind].pref;
    long long next = a[ind].total + solve(i + 1, 1);
    ans = max(0LL, max(stop, next));
  } else {
    long long next = solve(i + 1, 0);
    long long start = max(a[ind].total, a[ind].suf);
    ans = max(next, start + solve(i + 1, 1));
  }
  return dp[i][start] = ans;
}
int main() {
  scanf("%d %d", &n, &m);
  indices.resize(m);
  a.resize(n);
  long long num = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &l);
    a[i].n = l;
    a[i].arr.resize(l);
    for (int j = 0; j < l; j++) {
      scanf("%I64d", &num);
      a[i].arr[j] = num;
    }
  }
  for (int i = 0; i < m; i++) scanf("%d", &indices[i]);
  for (int i = 0; i < n; i++) {
    prefix.clear();
    prefix.resize(a[i].n + 1);
    prefix[0] = 0;
    long long sum = 0, best = a[i].arr[0];
    a[i].pref = a[i].arr[0];
    for (int j = 0; j < a[i].n; j++) {
      prefix[j + 1] = prefix[j] + a[i].arr[j];
      a[i].pref = max(a[i].pref, prefix[j + 1]);
      best = max(best, a[i].arr[j]);
      sum += a[i].arr[j];
      if (sum < 0)
        sum = 0;
      else
        best = max(best, sum);
    }
    a[i].total = prefix[a[i].n];
    a[i].best = best;
    a[i].suf = a[i].arr[a[i].n - 1];
    for (int j = a[i].n - 1; j >= 0; j--)
      a[i].suf = max(a[i].suf, a[i].total - prefix[j]);
  }
  memset(dp, -1, sizeof(dp));
  long long ans = solve(0, 0);
  if (ans == 0)
    cout << overall;
  else
    cout << max(overall, ans);
  return 0;
}
