#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int MAXW = 1e5 + 1;
bool ispal(string s) {
  int start = 0, end = s.size() - 1;
  while (start < end) {
    if (s[start] == s[end]) {
      end--;
      start++;
    } else {
      return false;
    }
  }
  return true;
}
int n, m;
bool isok(int i, int j) { return i < n && i >= 0 && j < m && j >= 0; }
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<long long> > arrays(n);
  long long ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    vector<long long> v(k);
    for (int j = 0; j < k; j++) {
      cin >> v[j];
    }
    long long sum = 0, left = 0, right = 0, gen = INT_MIN, tot = 0,
              tempright = 0, templeft = 0;
    for (int j = 0; j < k; j++) {
      templeft += v[j];
      left = max(templeft, left);
      tempright += v[k - j - 1];
      right = max(tempright, right);
      tot += v[j];
      gen = max(gen, v[j]);
      sum += v[j];
      if (sum < 0)
        sum = 0;
      else {
        gen = max(sum, gen);
      }
    }
    arrays[i].push_back(right);
    arrays[i].push_back(left);
    arrays[i].push_back(tot);
    arrays[i].push_back(gen);
  }
  vector<int> finals(m);
  for (int i = 0; i < m; i++) {
    cin >> finals[i];
    finals[i]--;
  }
  long long sum = 0, left = 0, gen, right, tot;
  for (int i = 0; i < m; i++) {
    sum = max(sum, left);
    gen = arrays[finals[i]][3];
    left = arrays[finals[i]][0];
    right = arrays[finals[i]][1];
    tot = arrays[finals[i]][2];
    if (sum + right > sum + tot && sum + right > 0) {
      ans = max(ans, sum + right);
      sum += tot;
    } else if (sum + tot > 0) {
      ans = max(ans, sum + tot);
      sum += tot;
    } else if (sum < 0) {
      sum = 0;
    }
    ans = max(ans, gen);
  }
  cout << ans;
  cout << endl;
  return 0;
}
