#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
long long M = 250002;
vector<long long> v(M);
long long INF = 1e14;
long long ans = -INF;
vector<long long> MAX(55);
vector<long long> MIN(55);
vector<long long> SUM(55);
vector<long long> KAD(55);
vector<long long> A;
long long sz;
void fun(long long num) {
  long long curr_sum = 0;
  long long mx = -INF;
  MIN[num] = A[1];
  MAX[num] = A[1];
  long long s = 0;
  for (long long i = 1; i <= sz; ++i) {
    curr_sum += A[i];
    mx = max(mx, curr_sum);
    curr_sum = max(curr_sum, (long long)0);
    s += A[i];
    MAX[num] = max(MAX[num], s);
    MIN[num] = min(MIN[num], s);
  }
  SUM[num] = s;
  KAD[num] = mx;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    long long curr_size;
    cin >> curr_size;
    sz = curr_size;
    A.clear();
    A.resize(curr_size + 1);
    for (long long j = 1; j <= curr_size; ++j) {
      cin >> A[j];
    }
    fun(i);
  }
  for (long long i = 1; i <= m; ++i) {
    cin >> v[i];
  }
  long long curr_pre = SUM[v[1]];
  long long curr_min_pre = min((long long)0, MIN[v[1]]);
  ans = max(ans, KAD[v[1]]);
  for (long long i = 2; i <= m; ++i) {
    long long z = curr_pre + MAX[v[i]];
    long long p = z - curr_min_pre;
    ;
    ;
    ;
    ans = max(ans, z - curr_min_pre);
    curr_min_pre = min(curr_min_pre, curr_pre + MIN[v[i]]);
    curr_pre += SUM[v[i]];
    ans = max(ans, KAD[v[i]]);
  };
  cout << ans << endl;
}
int main() {
  init();
  long long t = 1;
  while (t--) {
    solve();
  }
}
