#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
long long N, M;
vector<long long> A[50];
long long B[250000];
long long lef[50], rig[50], val[50], al[50];
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  for (long long i = 0; i < (N); i++) {
    long long n;
    cin >> n;
    A[i].resize(n);
    for (long long j = 0; j < (n); j++) cin >> A[i][j];
  }
  for (long long i = 0; i < (M); i++) cin >> B[i], B[i]--;
  for (long long i = 0; i < (N); i++) {
    lef[i] = LLONG_MIN;
    long long sum = 0;
    for (long long j = 0; j < A[i].size(); j++) {
      sum += A[i][j];
      chmax(lef[i], sum);
    }
    rig[i] = LLONG_MIN;
    sum = 0;
    for (long long j = A[i].size() - 1; j >= 0; j--) {
      sum += A[i][j];
      chmax(rig[i], sum);
    }
    val[i] = LLONG_MIN;
    long long mi = 0;
    sum = 0;
    for (long long j = 0; j < (A[i].size()); j++) {
      sum += A[i][j];
      chmax(val[i], sum - mi);
      chmin(mi, sum);
    }
    al[i] = sum;
  }
  long long ma = 0;
  long long sum = 0;
  long long ans = LLONG_MIN;
  for (long long i = 0; i < (M); i++) {
    chmax(ans, lef[B[i]] + sum + ma);
    sum += al[B[i]];
    chmax(ma, -sum + max(0ll, rig[B[i]]));
  }
  chmax(ans, rig[B[M - 1]]);
  for (long long i = 0; i < (M); i++) chmax(ans, val[B[i]]);
  cout << ans << endl;
  return 0;
}
