#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[51][5100], l[51];
long long inner[51], leftValue[51], rightValue[51], all[51];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &l[i]);
    for (int j = 0; j < l[i]; j++) scanf("%d", &a[i][j]);
  }
  for (int i = 0; i < n; i++) {
    long long sum = 0;
    inner[i] = leftValue[i] = rightValue[i] = -10000000000000000LL;
    for (int j = 0; j < l[i]; j++) {
      sum += a[i][j];
      leftValue[i] = max(leftValue[i], sum);
    }
    sum = 0;
    for (int j = l[i] - 1; j >= 0; --j) {
      sum += a[i][j];
      rightValue[i] = max(rightValue[i], sum);
    }
    long long mina = 0;
    sum = 0;
    for (int j = 0; j < l[i]; j++) {
      sum += a[i][j];
      inner[i] = max(inner[i], sum - mina);
      mina = min(mina, sum);
    }
    all[i] = sum;
  }
  long long ans = -10000000000000000LL;
  long long maxLeftValue = 0;
  for (int i = 0; i < m; i++) {
    int idx = -1;
    scanf("%d", &idx);
    --idx;
    ans = max(ans, inner[idx]);
    ans = max(ans, maxLeftValue + leftValue[idx]);
    maxLeftValue = max(maxLeftValue + all[idx], rightValue[idx]);
  }
  cout << ans << endl;
  return 0;
}
