#include <bits/stdc++.h>
using namespace std;
int main() {
  int smalls;
  cin >> smalls;
  int bigSize;
  cin >> bigSize;
  long long totalSum[50] = {0};
  long long leftSum[50] = {0};
  long long rightSum[50] = {0};
  long long maxSum[50] = {0};
  for (int i = 0; i < smalls; i++) {
    int smallSize;
    cin >> smallSize;
    long long leftSum2 = 0;
    long long rightSum2 = 0;
    long long totalSum2 = 0;
    long long localMax = 0;
    long long globalMax = LLONG_MIN;
    long long* tempArray = new long long[smallSize];
    for (int j = 0; j < smallSize; j++) {
      long long val;
      cin >> val;
      tempArray[j] = val;
      totalSum2 += val;
      if (j == 0) {
        leftSum2 = val;
        localMax = val;
        globalMax = max(localMax, globalMax);
      } else {
        localMax = max(val, localMax + val);
        globalMax = max(globalMax, localMax);
        leftSum2 = max(leftSum2, totalSum2);
      }
    }
    maxSum[i] = globalMax;
    totalSum[i] = totalSum2;
    leftSum[i] = leftSum2;
    long long totalSum3 = 0;
    for (int k = smallSize - 1; k >= 0; k--) {
      totalSum3 += tempArray[k];
      if (k == smallSize - 1) {
        rightSum2 = tempArray[k];
      } else {
        rightSum2 = max(rightSum2, totalSum3);
      }
    }
    rightSum[i] = rightSum2;
  }
  long long cur = 0;
  long long best = (long long)(-1e18);
  while (bigSize--) {
    int i;
    scanf("%d", &i);
    i--;
    best = max(best, maxSum[i]);
    if (cur) best = max(best, cur + leftSum[i]);
    cur = max(0ll, max(rightSum[i], cur + totalSum[i]));
  }
  cout << best << endl;
  return 0;
}
