#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 * 2;   int n, m, ind;   long long L[55], M[55], R[55], A[55], ans = -inf, un;   bool all = 1;   vector<int> a[55];   void calc() {     for (int i = 0; i < n; i++) {       R[i] = M[i] = L[i] = -inf;       for (int j = 0; j < a[i].size(); j++) {         A[i] += (long long)a[i][j];         if (A[i] > L[i]) L[i] = A[i];       }       long long sum = 0;       for (int j = a[i].size() - 1; j >= 0; j--) {         sum += (long long)a[i][j];         if (sum > R[i]) R[i] = sum;       }       sum = 0;       for (int j = 0; j < a[i].size(); j++) {         sum = max(sum + (long long)a[i][j], (long long)a[i][j]);         M[i] = max(M[i], sum);       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       int l;       scanf( %d , &l);       for (int j = 0; j < l; j++) {         int x;         scanf( %d , &x);         a[i].push_back(x);       }     }     calc();     for (int i = 0; i < m; i++) {       scanf( %d , &ind), ind--;       ans = max(un + L[ind], ans);       un = max(un + A[ind], R[ind]);       ans = max(ans, M[ind]);     }     printf( %I64d n , ans);     return 0;   }   