#include <bits/stdc++.h>
using namespace std;
long long adj[55][5005], pref[55][5005], suff[55][5005];
long long bestpref[55], bestsuff[55], soma[55], kadane[55], a[300005];
int main() {
  cout.sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    bestpref[i] = bestsuff[i] = -1e18;
    for (int j = 1; j <= k; j++) cin >> adj[i][j];
    for (int j = 1; j <= k; j++) pref[i][j] = pref[i][j - 1] + adj[i][j];
    for (int j = k; j >= 1; j--) suff[i][j] = suff[i][j + 1] + adj[i][j];
    for (int j = 1; j <= k; j++) {
      bestpref[i] = max(bestpref[i], pref[i][j]);
      bestsuff[i] = max(bestsuff[i], suff[i][j]);
    }
    soma[i] = pref[i][k];
    long long cursum = 0;
    long long curbest = -1e18;
    for (int j = 1; j <= k; j++) {
      cursum = max(cursum, 0LL);
      cursum += adj[i][j];
      curbest = max(curbest, cursum);
    }
    kadane[i] = curbest;
  }
  long long res = -1e18;
  long long cur = 0;
  for (int i = 1; i <= m; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    int idx = a[i];
    res = max(res, bestpref[idx]);
    res = max(res, bestsuff[idx]);
    res = max(res, kadane[idx]);
    res = max(res, cur + bestpref[idx]);
    cur = max(bestsuff[idx], cur + soma[idx]);
    res = max(res, cur);
  }
  cout << res << endl;
  return 0;
}
