#include <bits/stdc++.h>
using namespace std;
int N;
int tArr[50];
int arr[5000];
long long maxL[50], maxR[50], tot[50], W[50];
int M;
int main() {
  long long res = -1000LL;
  int i, j, k;
  long long sum;
  scanf("%d%d", &N, &M);
  for (k = 0; k < N; k++) {
    scanf("%d", &tArr[k]);
    sum = 0LL;
    maxL[k] = -1000LL;
    maxR[k] = -1000LL;
    W[k] = -1000LL;
    for (i = 0; i < tArr[k]; i++) {
      scanf("%d", &arr[i]);
      tot[k] += arr[i];
      maxR[k] = max(maxR[k], tot[k]);
      W[k] = max(W[k], sum + arr[i]);
      sum = max(0LL, sum + arr[i]);
    }
    sum = 0LL;
    for (i = tArr[k] - 1; i >= 0; i--) {
      sum += arr[i];
      maxL[k] = max(maxL[k], sum);
    }
  }
  sum = 0LL;
  for (i = 0; i < M; i++) {
    scanf("%d", &k);
    k--;
    res = max(res, maxR[k] + sum);
    res = max(res, W[k]);
    sum = max(sum + tot[k], maxL[k]);
  }
  cout << res << endl;
  return 0;
}
