#include <bits/stdc++.h>
using namespace std;
class Reader {
  char buf[262144], b;
  int bi, bz;
  void read() {
    if (bi == bz) {
      bi = 0;
      bz = fread(buf, 1, 262144, stdin);
    }
    b = bz ? buf[bi++] : 0;
  }
  void skip() {
    while (b > 0 && b <= 32) read();
  }

 public:
  Reader() {
    bi = bz = 0;
    read();
  }
  unsigned int nextInt() {
    unsigned int v = 0;
    for (skip(); b > 32; read()) v = v * 10 + b - 48;
    return v;
  }
};
class SignedReader {
  char arr[262144];
  char *p, *e;
  int s, val;
  bool neg;
  void getTok() {
    p = arr;
    s = fread(arr, 1, 262144, stdin);
    e = arr + s;
  }
  void skip() {
    while (true) {
      if (p == e) getTok();
      if (*p == '-' || ((*p) >= '0' && (*p) <= '9')) return;
      p++;
    }
  }

 public:
  SignedReader() { getTok(); }
  int nextInt() {
    neg = false;
    skip();
    if (*p == '-') neg = true, p++;
    val = 0;
    while (true) {
      if (p == e) getTok();
      if (p == e) break;
      if (((*p) >= '0' && (*p) <= '9'))
        val = 10 * val + *p - 48, p++;
      else
        break;
    }
    if (neg) val = -val;
    return val;
  }
};
class StringIntegerParser {
  char s[500];
  char *p;

 public:
  void readLine() {
    p = s;
    if (!gets(p)) p = NULL;
  }
  int nextInt() {
    for (; !((*p) >= '0' && (*p) <= '9') && *p != '-'; p++)
      ;
    int n = 0;
    bool neg = *p == '-';
    if (neg) p++;
    for (; ((*p) >= '0' && (*p) <= '9'); p++) n = 10 * n + *p - 48;
    if (neg) return -n;
    return n;
  }
  bool empty() {
    if (!p || !*p) return true;
    for (; !((*p) >= '0' && (*p) <= '9') && *p != '-' && *p; p++)
      ;
    return !(*p);
  }
};
class CustomStringBuffer {
  char arr[2000000], *p;
  int len(int a) {
    int i = 0;
    for (; a; i++, a /= 10)
      ;
    return i;
  }

 public:
  CustomStringBuffer() { p = arr; }
  void addNumber(int n) {
    if (!n) {
      *p = 48;
      p++;
      return;
    }
    if (n < 0) {
      n = -n;
      *p = '-';
      p++;
    }
    int ln = len(n);
    char *end = p + ln;
    for (p = end - 1; n; p--, n /= 10) *p = (n % 10) | 0x30;
    p = end;
  }
  void addLetter(char c) {
    *p = c;
    p++;
  }
  char *getStr() {
    *p = 0;
    return arr;
  }
  void addNewLine() {
    *p = '\n';
    p++;
  }
  void addSpace() {
    *p = ' ';
    p++;
  }
};
void makeDelay(long long miliSecond) {
  long long lim = miliSecond * 400000;
  for (long long __w = 0; __w < lim; __w++)
    ;
}
long long lefT[55];
long long righT[55];
long long sum[55];
int n, m;
int temp[5005];
int a[250005];
long long bestIntermediate[55];
long long maxElm[55];
int allNeg(long long &maxEl) {
  maxEl = -1152921504606846976LL;
  for (int i = 1; i <= m; i++) maxEl = max(maxEl, maxElm[a[i]]);
  return maxEl < 0;
}
int main() {
  int i, j, sj;
  long long tempSum, runningSum, best = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &sj);
    lefT[i] = 0;
    tempSum = 0;
    bestIntermediate[i] = runningSum = 0;
    maxElm[i] = -1152921504606846976LL;
    for (j = 0; j < sj; j++) {
      scanf("%d", temp + j);
      tempSum += temp[j];
      lefT[i] = max(lefT[i], tempSum);
      maxElm[i] = max(maxElm[i], (long long)temp[j]);
      runningSum += temp[j];
      if (runningSum < 0) runningSum = 0;
      bestIntermediate[i] = max(bestIntermediate[i], runningSum);
    }
    sum[i] = tempSum;
    righT[i] = 0;
    tempSum = 0;
    for (j = sj - 1; j >= 0; j--) {
      tempSum += temp[j];
      righT[i] = max(righT[i], tempSum);
    }
  }
  for (i = 1; i <= m; i++) scanf("%d", a + i);
  long long maxSingleElement;
  if (allNeg(maxSingleElement))
    cout << maxSingleElement << endl;
  else {
    runningSum = 0;
    for (i = 1; i <= m; i++) {
      best = max(best, bestIntermediate[a[i]]);
      best = max(best, runningSum + lefT[a[i]]);
      runningSum = max(runningSum + sum[a[i]], righT[a[i]]);
    }
    cout << best << endl;
  }
  return 0;
}
