#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
int numSmall, numIndex;
vector<long long> input[55];
long long minPrefix[55];
long long maxPrefix[55];
long long totalSum[55];
bool present[55];
long long ans = -(1LL << 62);
void processArrray(int index, bool updateAns = false) {
  long long mn = (1LL << 60), mx = -(1LL << 60), sm = 0;
  for (int i = 0; i < input[index].size(); i++) {
    sm += input[index][i];
    if (updateAns == true) {
      ans = max(ans, sm - mn);
    }
    mx = max(mx, sm);
    mn = min(mn, sm);
  }
  minPrefix[index] = mn;
  maxPrefix[index] = mx;
  totalSum[index] = sm;
}
int main() {
  scanf("%d %d", &numSmall, &numIndex);
  int len, tp;
  for (int i = 1; i <= numSmall; i++) {
    scanf("%d", &len);
    input[i].resize(len);
    for (int j = 0; j < len; j++) {
      scanf("%d", &tp);
      input[i][j] = tp;
    }
    processArrray(i);
  }
  long long mn = (1LL << 62), sm = 0;
  memset(present, 0, sizeof(present));
  ;
  for (int j = 0; j < numIndex; j++) {
    scanf("%d", &tp);
    present[tp] = true;
    ans = max(ans, sm + maxPrefix[tp]);
    ans = max(ans, sm + maxPrefix[tp] - mn);
    ans = max(ans, sm + totalSum[tp] - mn);
    mn = min(mn, sm + minPrefix[tp]);
    sm += totalSum[tp];
    mn = min(mn, sm);
  }
  for (int i = 1; i <= numSmall; i++) {
    if (present[i]) {
      processArrray(i, true);
    }
  }
  cout << ans << endl;
  return 0;
}
