#include <bits/stdc++.h>
using namespace std;
const int maxi = 1e6 + 5;
const int big = 10000;
vector<int> v[maxi];
int n, m;
long long ans;
int a[maxi];
long long sum[maxi], sum1[maxi], sum2[maxi], sum3[maxi];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int sz;
    cin >> sz;
    for (int j = 1; j <= sz; j++) {
      int x;
      scanf("%d", &x);
      v[i].push_back(x);
      sum[i] += x;
    }
  }
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    sum1[i] = -big;
    int tot = 0;
    for (int j = v[i].size() - 1; j >= 0; j--) {
      tot += v[i][j];
      sum1[i] = max(sum1[i], 1ll * tot);
    }
  }
  for (int i = 1; i <= n; i++) {
    int ma = 0;
    sum2[i] = -big;
    int tot = 0;
    for (int j : v[i]) {
      tot += j;
      sum2[i] = max(sum2[i], 1ll * tot - ma);
      ma = min(ma, tot);
    }
  }
  for (int i = 1; i <= n; i++) {
    int tot = 0;
    sum3[i] = -big;
    for (int j : v[i]) {
      tot += j;
      sum3[i] = max(sum3[i], 1ll * tot);
    }
  }
  long long ans = -big;
  long long cur = 0;
  for (int i = 1; i <= m; i++) {
    ans = max(ans, sum2[a[i]]);
    ans = max(ans, cur + sum3[a[i]]);
    cur = max(sum1[a[i]], cur + sum[a[i]]);
    ans = max(ans, cur);
  }
  cout << ans;
  return 0;
}
