#include <bits/stdc++.h>
using namespace std;
int N, M;
int idxs[250005];
vector<int> arrs[55];
int prefix[55];
int suffix[55];
int total[55];
long long maxi[55];
long long singular[55];
long long memo[250005][2];
long long roll(int idx, int state) {
  if (idx == M) {
    return 0LL;
  }
  if (~memo[idx][state]) {
    return memo[idx][state];
  }
  long long &ans = memo[idx][state];
  ans = -(1LL << 62);
  if (state == 0) {
    ans = max({ans, roll(idx + 1, 0), roll(idx + 1, 1) + suffix[idxs[idx]],
               (long long)prefix[idxs[idx]], (long long)suffix[idxs[idx]]});
  } else if (state == 1) {
    ans = max({ans, roll(idx + 1, 1) + total[idxs[idx]],
               (long long)prefix[idxs[idx]]});
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int l;
    cin >> l;
    maxi[i] = -(1LL << 60);
    for (int j = 0; j < l; j++) {
      int cur;
      cin >> cur;
      maxi[i] = max(maxi[i], (long long)cur);
      arrs[i].push_back(cur);
    }
  }
  unordered_set<int> s;
  for (int i = 0; i < M; i++) {
    cin >> idxs[i];
    idxs[i]--;
    s.insert(idxs[i]);
  }
  long long all_maxi = -(1LL << 60);
  for (auto each : s) {
    all_maxi = max(maxi[each], all_maxi);
    long long sum = 0;
    for (int j = 0; j < arrs[each].size(); j++) {
      sum = max(sum + arrs[each][j], (long long)arrs[each][j]);
      singular[each] = max(sum, singular[each]);
    }
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < arrs[i].size(); j++) {
      sum += arrs[i][j];
      prefix[i] = max(prefix[i], sum);
    }
    total[i] = sum;
    sum = 0;
    for (int j = arrs[i].size() - 1; j >= 0; j--) {
      sum += arrs[i][j];
      suffix[i] = max(suffix[i], sum);
    }
  }
  long long ans = 0;
  memset(memo, -1, sizeof memo);
  ans = max(ans, roll(0, 0));
  for (int i = 0; i < N; i++) {
    ans = max(ans, singular[i]);
  }
  cout << ((0 > all_maxi) ? all_maxi : ans) << '\n';
  return 0;
}
