#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
int Pre[102];
int Suf[102];
int Opt[102];
int a[5004];
int Sm[102];
int b[300007];
int P[102];
long long int dp[300007];
int mX;
void Dekhi(int id) {
  int n;
  n = in<int>();
  mX = -1000000;
  for (int i = 1; i < n + 1; i++) {
    a[i] = in<int>();
    mX = max(mX, a[i]);
  }
  P[id] = mX;
  int sum = 0;
  int tot = 0;
  int mx = 0;
  int pr = 0;
  for (int i = 1; i <= n; i++) {
    tot += a[i];
    pr = max(pr, tot);
    sum += a[i];
    if (sum < 0) sum = 0;
    mx = max(mx, sum);
  }
  Pre[id] = pr;
  Opt[id] = mx;
  tot = 0;
  pr = 0;
  for (int i = n; i >= 1; i--) {
    tot += a[i];
    pr = max(pr, tot);
  }
  Suf[id] = pr;
  Sm[id] = tot;
  return;
}
int main() {
  int n, m;
  n = in<int>(), m = in<int>();
  for (int i = 1; i < n + 1; i++) {
    Dekhi(i);
  }
  mX = -1000000;
  for (int i = 1; i <= m; i++) {
    b[i] = in<int>();
    mX = max(mX, P[b[i]]);
  }
  if (mX < 0) {
    printf("%d\n", mX);
    return 0;
  }
  long long int ans = 0;
  for (int i = m; i >= 1; i--) {
    ans = ((ans > Opt[b[i]]) ? ans : Opt[b[i]]);
    ans = ((ans > dp[i + 1] + Suf[b[i]]) ? ans : dp[i + 1] + Suf[b[i]]);
    dp[i] =
        ((Pre[b[i]] > Sm[b[i]] + dp[i + 1]) ? Pre[b[i]] : Sm[b[i]] + dp[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
