#include <bits/stdc++.h>
using namespace std;
const long long INF = 1ll << 56;
int n, m;
vector<long long> smallarray[50];
int bigarray[250000];
long long lsum[50], asum[50], rsum[50];
bool ok[50];
long long mss(vector<long long> ar) {
  static long long dp[5001];
  long long res = -INF;
  for (int i = 0; i < ar.size(); i++) {
    dp[i + 1] = max(dp[i] + ar[i], ar[i]);
    res = max(res, dp[i + 1]);
  }
  return res;
}
void solve() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < smallarray[i].size(); j++) {
      asum[i] += smallarray[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    long long sum = 0;
    long long ans = -INF;
    for (int j = 0; j < smallarray[i].size(); j++) {
      sum += smallarray[i][j];
      ans = max(ans, sum);
    }
    lsum[i] = ans;
  }
  for (int i = 0; i < n; i++) {
    long long sum = 0;
    long long ans = -INF;
    for (int j = smallarray[i].size() - 1; j >= 0; j--) {
      sum += smallarray[i][j];
      ans = max(ans, sum);
    }
    rsum[i] = ans;
  }
  static long long dp[250000][3];
  fill_n(*dp, 250000 * 3, -INF);
  dp[0][0] = lsum[bigarray[0]];
  dp[0][1] = asum[bigarray[0]];
  dp[0][2] = rsum[bigarray[0]];
  for (int i = 1; i < m; i++) {
    int id = bigarray[i];
    dp[i][0] = max(dp[i][0], lsum[id]);
    dp[i][0] = max(dp[i][0], dp[i - 1][1] + lsum[id]);
    dp[i][0] = max(dp[i][0], dp[i - 1][2] + lsum[id]);
    dp[i][1] = max(dp[i][1], asum[id]);
    dp[i][1] = max(dp[i][1], dp[i - 1][1] + asum[id]);
    dp[i][1] = max(dp[i][1], dp[i - 1][2] + asum[id]);
    dp[i][2] = max(dp[i][2], rsum[id]);
  }
  long long res = -INF;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 3; j++) {
      res = max(res, dp[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (ok[i]) res = max(res, mss(smallarray[i]));
  }
  printf("%I64d\n", res);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    int l;
    scanf("%d", &l);
    smallarray[i].resize(l);
    for (int j = 0; j < l; j++) {
      scanf("%I64d", &smallarray[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &bigarray[i]);
    bigarray[i]--;
    ok[bigarray[i]] = true;
  }
  solve();
  return 0;
}
