#include <bits/stdc++.h>
using namespace std;
long long int prefix[55][5005], suffix[55][5005];
int bigN, n, smallArray[55][5005], length[55];
long long int maxPrefix[55], maxSuffix[55], minPrefix[55], minSuffix[55],
    maxSubseq[55];
int main() {
  scanf("%d%d", &n, &bigN);
  int i, m, k, l, x, y;
  long long res = -5000000;
  for (i = 1; i <= n; ++i) {
    scanf("%d", &length[i]);
    for (m = 1; m <= length[i]; ++m) scanf("%d", &smallArray[i][m]);
    maxPrefix[i] = smallArray[i][1];
    maxSubseq[i] = maxPrefix[i];
    for (m = 1; m <= length[i]; ++m) {
      prefix[i][m] = prefix[i][m - 1] + smallArray[i][m];
      maxPrefix[i] = max(maxPrefix[i], prefix[i][m]);
      maxSubseq[i] = max(maxSubseq[i], prefix[i][m] - minPrefix[i]);
      minPrefix[i] = min(minPrefix[i], prefix[i][m]);
    }
    maxSuffix[i] = smallArray[i][length[i]];
    for (m = length[i]; m >= 1; --m) {
      suffix[i][m] = suffix[i][m + 1] + smallArray[i][m];
      maxSuffix[i] = max(maxSuffix[i], suffix[i][m]);
      minSuffix[i] = min(minSuffix[i], suffix[i][m]);
    }
  }
  long long cur = 0;
  for (i = 1; i <= bigN; ++i) {
    scanf("%d", &x);
    res = max(res, cur + maxPrefix[x]);
    res = max(res, maxSubseq[x]);
    cur = max(cur + prefix[x][length[x]], maxSuffix[x]);
    cur = max(cur, 0ll);
  }
  cout << res << endl;
  return 0;
}
