#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10, LEN = 5e3 + 10, S = 51, INF = 1e18;   long long f[N], l[N], r[N], sum[N];   long long lq[S], rq[S], sq[S], mq[S];   long long a[LEN], sx[LEN], mx[LEN];   int n, m;   int main() {     scanf( %d%d , &n, &m);     memset(mq, -0x3f, sizeof mq);     for (int i = 1, s; i <= n; i++) {       scanf( %d , &s);       for (int j = 1; j <= s; j++) scanf( %lld , &a[j]);       for (int j = 1; j <= s; j++) sx[j] = sx[j - 1] + a[j];       for (int j = 1; j <= s; j++) {         mx[j] = max(mx[j - 1] + a[j], a[j]);         mq[i] = max(mq[i], mx[j]);       }       lq[i] = a[1];       for (int j = 2; j <= s; j++) lq[i] = max(lq[i], sx[j]);       rq[i] = a[n];       for (int j = s - 2; j >= 0; j--) rq[i] = max(rq[i], sx[s] - sx[j]);       sq[i] = sx[s];     }     long long res = -INF;     for (int i = 1, x; i <= m; i++) {       scanf( %d , &x);       l[i] = lq[x];       r[i] = rq[x];       sum[i] = sum[i - 1] + sq[x];       res = max(res, mq[x]);     }     f[1] = l[1];     long long maxv = r[1] - sum[1], ans = f[1];     for (int i = 2; i <= m; i++) {       f[i] = sum[i - 1] + l[i] + maxv;       maxv = max(maxv, r[i] - sum[i]);       ans = max(ans, f[i]);     }     printf( %lld n , max(ans, res));     return 0;   }   