/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<int> ians(n);
	long long int ans=-100000000;
	For(i,0,n)
	{
		int mans,mi;
		mans=lr[i][0];mi=0;
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,lr[i][j]);
		}
		ians[i]=mans;
	}
	For(i,0,m)
	ans=max(ans,(long long int)ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	cout<<ans<<endl;
}


/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<int> ians(n);
	For(i,0,n)
	{
		int mans,mi;
		mans=lr[i][0];mi=0;
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,lr[i][j]);
		}
		ians[i]=mans;
	}
	long long int ans=ians[br[0]];
	For(i,1,m)
	ans=max(ans,(long long int)ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	ans=max(ans,cm);
	cout<<ans<<endl;
}


//HACK ME, PLEASE! ^_^
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <utility>
#include <math.h>
#include <cstdlib>
#include <memory.h>
#include <queue>

#define pb push_back
#define i64 long long
#define mp make_pair
#define pii pair <int,int>
#define vi vector <int>
#define vii vector <pii>
#define f first
#define s second
#define foran(i,a,b) for (int i=a;i<(int)b;i++)
#define forn(i,n) for (int i=0;i<(int)n;i++)
#define ford(i,n) for (int i=(int)n-1;i>=0;i--)
#define sqr(x) (x) * (x)

const double eps = 1e-9;
const int int_inf = 1 << 31 - 1;
const i64 i64_inf = 1ll << 63 - 1;
const double pi = acos(-1.0);

using namespace std;

int n, m;

int big[350000];

int a[60][6000];
int sum[60];
int max_pref[60];
int min_pref[60];
int l[60];

int answer[60];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
  cin >> n >> m;
  forn(i, n)
  {
      int l; scanf("%d", &l);
      sum[i] = 0;
      forn(j, l)
       scanf("%d", &a[i][j]), sum[i] += a[i][j];
      int s = 0;
      min_pref[i] = max_pref[i] = 0;
      forn(j, l)
       s += a[i][j], max_pref[i] = max(max_pref[i], s), min_pref[i] = min(min_pref[i], s);

      s = a[i][0];
      int mins = 0;
      answer[i] = 0;
      forn(j, l)
      {
           answer[i] = max(answer[i], s - mins);
           s += a[i][j + 1];
           mins = min(mins, s);
      }
  }

  forn(i, m)
   scanf("%d",&big[i]), big[i]--;

  i64 S = sum[big[0]];
  i64 MinS = min(0, min_pref[big[0]]);
  i64 ans = a[big[0]][0];

  forn(i, m)
  {
      ans = max(ans, (i64)answer[big[i]]);
//      cout << i << " " << S << " " << MinS << endl;
      i64 res = S - MinS;
      if  (i != m - 1) res += max_pref[big[i + 1]];
      ans = max(ans, res);
      MinS = min(MinS, S + min_pref[big[i + 1]]);
      S += sum[big[i + 1]];
  }

  cout << ans << endl;
  return 0;
}


//HACK ME, PLEASE! ^_^
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <utility>
#include <math.h>
#include <cstdlib>
#include <memory.h>
#include <queue>

#define pb push_back
#define i64 long long
#define mp make_pair
#define pii pair <int,int>
#define vi vector <int>
#define vii vector <pii>
#define f first
#define s second
#define foran(i,a,b) for (int i=a;i<(int)b;i++)
#define forn(i,n) for (int i=0;i<(int)n;i++)
#define ford(i,n) for (int i=(int)n-1;i>=0;i--)
#define sqr(x) (x) * (x)

const double eps = 1e-9;
const int int_inf = 1 << 31 - 1;
const i64 i64_inf = 1ll << 63 - 1;
const double pi = acos(-1.0);

using namespace std;

int n, m;

int big[350000];

int a[60][6000];
int sum[60];
int max_pref[60];
int min_pref[60];
int Max[60];
int l[60];

i64 answer[60];

bool was = 0;

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
  cin >> n >> m;
  forn(i, n)
  {
      int l; scanf("%d", &l);
      sum[i] = 0;
      forn(j, l)
       scanf("%d", &a[i][j]), sum[i] += a[i][j], was = a[i][j] > 0;
      int s = 0;
      min_pref[i] = max_pref[i] = 0;
      forn(j, l)
       s += a[i][j], max_pref[i] = max(max_pref[i], s), min_pref[i] = min(min_pref[i], s);
      Max[i] = a[i][0];
      forn(j, l)
       Max[i] = max(Max[i], a[i][j]);
      i64 S = a[i][0];
      i64 MinS = 0;
      answer[i] = S;
      forn(j, l)
      {
           answer[i] = max(answer[i], S - MinS);
           MinS = min(MinS, S);
           S += a[i][j + 1];
      }
  }

  forn(i, m)
   scanf("%d",&big[i]), big[i]--;

  i64 S = sum[big[0]];
  i64 MinS = min(0, min_pref[big[0]]);
  i64 ans = a[big[0]][0];

  forn(i, m)
  {
      ans = max(ans, answer[big[i]]);
      i64 res = S - MinS;
      if  (i != m - 1) res += max_pref[big[i + 1]];
      ans = max(ans, res);
      MinS = min(MinS, S + min_pref[big[i + 1]]);
      S += sum[big[i + 1]];
  }

  if  (!was)
  {
      ans = a[big[0]][0];
      forn(i, m)
      {
       i64 ma = Max[big[i]];
       ans = max(ans, ma);
      }
  }

  cout << ans << endl;
  return 0;
}


//HACK ME, PLEASE! ^_^
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <utility>
#include <math.h>
#include <cstdlib>
#include <memory.h>
#include <queue>

#define pb push_back
#define i64 long long
#define mp make_pair
#define pii pair <int,int>
#define vi vector <int>
#define vii vector <pii>
#define f first
#define s second
#define foran(i,a,b) for (int i=a;i<(int)b;i++)
#define forn(i,n) for (int i=0;i<(int)n;i++)
#define ford(i,n) for (int i=(int)n-1;i>=0;i--)
#define sqr(x) (x) * (x)

const double eps = 1e-9;
const int int_inf = 1 << 31 - 1;
const i64 i64_inf = 1ll << 63 - 1;
const double pi = acos(-1.0);

using namespace std;

int n, m;

int big[350000];

int a[60][6000];
int sum[60];
int max_pref[60];
int min_pref[60];
int Max[60];
int l[60];

i64 answer[60];

bool was = 0;

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
  cin >> n >> m;
  forn(i, n)
  {
      int l; scanf("%d", &l);
      sum[i] = 0;
      forn(j, l)
       scanf("%d", &a[i][j]), sum[i] += a[i][j], was |= a[i][j] > 0;
      int s = 0;
      min_pref[i] = max_pref[i] = 0;
      forn(j, l)
       s += a[i][j], max_pref[i] = max(max_pref[i], s), min_pref[i] = min(min_pref[i], s);
      Max[i] = a[i][0];
      forn(j, l)
       Max[i] = max(Max[i], a[i][j]);
      i64 S = a[i][0];
      i64 MinS = 0;
      answer[i] = S;
      forn(j, l)
      {
           answer[i] = max(answer[i], S - MinS);
           MinS = min(MinS, S);
           S += a[i][j + 1];
      }
  }

  forn(i, m)
   scanf("%d",&big[i]), big[i]--;

  i64 S = sum[big[0]];
  i64 MinS = min(0, min_pref[big[0]]);
  i64 ans = a[big[0]][0];

  forn(i, m)
  {
      ans = max(ans, answer[big[i]]);
      i64 res = S - MinS;
      if  (i != m - 1) res += max_pref[big[i + 1]];
      ans = max(ans, res);
      MinS = min(MinS, S + min_pref[big[i + 1]]);
      S += sum[big[i + 1]];
  }

  if  (!was)
  {
      ans = a[big[0]][0];
      forn(i, m)
      {
       i64 ma = Max[big[i]];
       ans = max(ans, ma);
      }
  }

  cout << ans << endl;
  return 0;
}


//HACK ME, PLEASE! ^_^
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <utility>
#include <math.h>
#include <cstdlib>
#include <memory.h>
#include <queue>

#define pb push_back
#define i64 long long
#define mp make_pair
#define pii pair <int,int>
#define vi vector <int>
#define vii vector <pii>
#define f first
#define s second
#define foran(i,a,b) for (int i=a;i<(int)b;i++)
#define forn(i,n) for (int i=0;i<(int)n;i++)
#define ford(i,n) for (int i=(int)n-1;i>=0;i--)
#define sqr(x) (x) * (x)

const double eps = 1e-9;
const int int_inf = 1 << 31 - 1;
const i64 i64_inf = 1ll << 63 - 1;
const double pi = acos(-1.0);

using namespace std;

int n, m;

int big[350000];

int a[60][6000];
int sum[60];
int max_pref[60];
int min_pref[60];
int Max[60];
int l[60];

i64 answer[60];

bool was[60];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
  cin >> n >> m;
  memset(was, 0, sizeof was);
  forn(i, n)
  {
      int l; scanf("%d", &l);
      sum[i] = 0;
      forn(j, l)
       scanf("%d", &a[i][j]), sum[i] += a[i][j], was[i] |= a[i][j] > 0;
      int s = 0;
      min_pref[i] = max_pref[i] = 0;
      forn(j, l)
       s += a[i][j], max_pref[i] = max(max_pref[i], s), min_pref[i] = min(min_pref[i], s);
      Max[i] = a[i][0];
      forn(j, l)
       Max[i] = max(Max[i], a[i][j]);
      i64 S = a[i][0];
      i64 MinS = 0;
      answer[i] = S;
      forn(j, l)
      {
           answer[i] = max(answer[i], S - MinS);
           MinS = min(MinS, S);
           S += a[i][j + 1];
      }
  }

  forn(i, m)
   scanf("%d",&big[i]), big[i]--;

  i64 S = sum[big[0]];
  i64 MinS = min(0, min_pref[big[0]]);
  i64 ans = a[big[0]][0];

  bool Was = 0;
  forn(i, m)
  {
      Was |= was[big[i]];
      ans = max(ans, answer[big[i]]);
      i64 res = S - MinS;
      if  (i != m - 1) res += max_pref[big[i + 1]];
      ans = max(ans, res);
      MinS = min(MinS, S + min_pref[big[i + 1]]);
      S += sum[big[i + 1]];
  }

  if  (!Was)
  {
      ans = a[big[0]][0];
      forn(i, m)
      {
       i64 ma = Max[big[i]];
       ans = max(ans, ma);
      }
  }

  cout << ans << endl;
  return 0;
}


#include <stdio.h>
#include <stack>
#include <vector>
#include<string>
#include<iostream>
#include <algorithm>

using namespace std;

typedef long long lg;
typedef vector<lg> vi;
vector<vi> ma;
int n, m;

int main(){
	int u, t;
	lg v;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++){
			scanf("%I64d", &v);
			ma[i][j]=v;
		}
	}
	lg maxi=-1001, act=0;
	int pos;
	while(m--){
		scanf("%d", &pos);
		pos--;
		for(int i=0; i<ma[pos].size(); i++){
		    if(ma[pos][i]+act>ma[pos][i]){
                act=ma[pos][i]+act;
            }
            else{
                act=ma[pos][i];
            }
			//act = max(ma[pos][i], act + ma[pos][i]);
            //maxi = max(maxi, act);
            if(act>maxi)    maxi=act;
		}
	}
	//cout<<"maxi = "<<maxi<<endl;
	printf("%I64d\n", maxi);
	return 0;
}


#include <stdio.h>
#include <stack>
#include <vector>
#include<string>
#include<iostream>

using namespace std;

typedef long long lg;
typedef vector<int> vi;
vector<vi> ma;
int n, m;

int main(){
	int u, t;
	lg v;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++){
			scanf("%I64d", &v);
			ma[i][j]=v;
		}
	}
	lg maxi=0, act=0;
	int pos;
	while(m--){
		scanf("%d", &pos);
		pos--;
		for(int i=0; i<ma[pos].size(); i++){
			act+=ma[pos][i];
			if(act<0){
				act=0;
			}
			if(act>maxi){
				maxi=act;
			}
		}
	}
	cout<<maxi<<endl;
	return 0;
}


#include <stdio.h>
#include <stack>
#include <vector>
#include<string>
#include<iostream>
#include <algorithm>

using namespace std;

typedef long long lg;
typedef vector<lg> vi;
vector<vi> ma;
int n, m;

int main(){
	int u, t;
	lg v;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++){
			scanf("%I64d", &v);
			ma[i][j]=v;
		}
	}
	lg maxi=-1001, act=0;
	int pos;
	while(m--){
		scanf("%d", &pos);
		pos--;
		for(int i=0; i<ma[pos].size(); i++){
		    if(ma[pos][i]+act>ma[pos][i]){
                act=ma[pos][i]+act;
            }
            else{
                act=ma[pos][i];
            }
			//act = max(ma[pos][i], act + ma[pos][i]);
            //maxi = max(maxi, act);
            if(act>maxi)    maxi=act;
		}
	}
	cout<<maxi<<endl;
	return 0;
}


#include <stdio.h>
#include <stack>
#include <vector>
#include<string>
#include<iostream>

using namespace std;

typedef long long lg;
typedef vector<int> vi;
vector<vi> ma;
int n, m;

int main(){
	int u, t;
	lg v;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++){
			scanf("%I64d", &v);
			ma[i][j]=v;
		}
	}
	lg maxi=-1001, act=0;
	int pos;
	while(m--){
		scanf("%d", &pos);
		pos--;
		for(int i=0; i<ma[pos].size(); i++){
			act+=ma[pos][i];

			if(act>maxi){
				maxi=act;
			}
		}
	}
	cout<<maxi<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lg;

struct nodo{
	lg sum, maxi, pref, suf;
	nodo(){}
	nodo(lg s, lg m, lg p, lg su){
		sum=s;
		maxi=m;
		pref=p;
		suf=su;
	}
};

typedef vector<lg> vi;
vector<nodo> tree;
vector<vi> ma;
vector<nodo> a;
map<int, nodo> mapa;
map<int, nodo>::iterator it;
int n, m;

nodo join(nodo a, nodo b){
	lg sum=a.sum+b.sum;
	lg maxi=max(a.maxi, max(b.maxi, a.suf+b.pref));
	lg pref=max(a.pref, a.sum+b.pref);
	lg suf=max(b.suf, b.sum+a.suf);
	return nodo(sum, maxi, pref, suf);
}

void init2(int i, int j, int begin, int end, vector<nodo>& tree2){
	if(begin==end){
		tree2[j]=nodo(ma[i][begin], ma[i][begin], ma[i][begin], ma[i][begin]);
		/*printf("ma[%d][%d] = %lld\n", i, begin, ma[i][begin]);
		printf("tree2[%d]-> begin = %d, end =  %d , maxi = %lld, sum = %lld, pref = %lld, suf %lld\n", j, begin, end, tree2[j].maxi, tree2[j].sum, tree2[j].pref, tree2[j].suf);
		printf("\n");*/
	}
	else{
		int mid=(begin+end)/2;
		int le=2*j+1;
		int ri=2*j+2;
		init2(i, le, begin, mid, tree2);
		init2(i, ri, mid+1, end, tree2);
		tree2[j]=join(tree2[le], tree2[ri]);
		//printf("tree2[%d]-> begin = %d, end =  %d , maxi = %lld, sum = %lld, pref = %lld, suf %lld\n", j, begin, end, tree2[j].maxi, tree2[j].sum, tree2[j].pref, tree2[j].suf);
	}
}

void init(int i, int begin, int end){
	if(begin==end){
		tree[i]=a[begin];
	}
	else{
		int mid=(begin+end)/2;
		int le=2*i+1;
		int ri=2*i+2;
		init(le, begin, mid);
		init(ri, mid+1, end);
		tree[i]=join(tree[le], tree[ri]);
	}
}

void print(){
	cout<<"---------------pRINT------------"<<endl;
	nodo res;
	for(it=mapa.begin(); it!=mapa.end(); it++){
		res=it->second;
		printf("mapa[%d] = %lld\n", it->first, res.maxi);
	}
	cout<<"------------------"<<endl;
}

void print2(){
	cout<<"----------------mATRIZ-----"<<endl;
	for(int i=0; i<ma.size(); i++){
		for(int j=0; j<ma[i].size(); j++){
			printf("%lld ", ma[i][j]);
		}
		printf("\n");
	}
	cout<<"_-------------------------"<<endl;
}

int main(){
	int u, v, t;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++)
			//scanf("%I64d", &ma[i][j]);
			cin>>ma[i][j];
	}
	//print2();
	a.assign(m, nodo());
	vi b;
	vector<nodo> treeAux;
	for(int i=0; i<m; i++){
		scanf("%d", &u);
		u--;
		it=mapa.find(u);
		if(it==mapa.end()){
			b=ma[u];
			t=b.size();
			treeAux.assign(4*t, nodo());
			//cout<<"-----------u = "<<u<<endl;
			init2(u, 0, 0, t-1, treeAux);
			//printf("final = %lld\n", treeAux[0].maxi);
			//cout<<"----------termino init-------"<<endl;
			mapa[u]=treeAux[0];
			treeAux.clear();
		}
		a[i]=mapa[u];
	}
	//print();
	tree.assign(4*m, nodo());
	init(0, 0, m-1);
	printf("%I64d\n", tree[0].maxi);
	return 0;
}


#include <stdio.h>
#include <stack>
#include <vector>
#include<string>
#include<iostream>
#include <algorithm>

using namespace std;

typedef long long lg;
typedef vector<lg> vi;
vector<vi> ma;
int n, m;

int main(){
	int u, t;
	lg v;
	scanf("%d %d", &n, &m);
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		scanf("%d", &t);
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++){
			scanf("%I64d", &v);
			ma[i][j]=v;
		}
	}
	lg maxi=-1001, act=0;
	int pos;
	while(m--){
		scanf("%d", &pos);
		pos--;
		for(int i=0; i<ma[pos].size(); i++){
			act = max(ma[pos][i], act + ma[pos][i]);
            maxi = max(maxi, act);
		}
	}
	cout<<maxi<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lg;

struct nodo{
	lg sum, maxi, pref, suf;
	nodo(){}
	nodo(lg s, lg m, lg p, lg su){
		sum=s;
		maxi=m;
		pref=p;
		suf=su;
	}
};

typedef vector<lg> vi;
vector<nodo> tree;
vector<vi> ma;
vector<nodo> a;
map<int, nodo> mapa;
map<int, nodo>::iterator it;
int n, m;

nodo join(nodo a, nodo b){
	lg sum=a.sum+b.sum;
	lg maxi=max(a.maxi, max(b.maxi, a.suf+b.pref));
	lg pref=max(a.pref, a.sum+b.pref);
	lg suf=max(b.suf, b.sum+a.suf);
	return nodo(sum, maxi, pref, suf);
}

void init2(int i, int j, int begin, int end, vector<nodo>& tree2){
	if(begin==end){
		tree2[j]=nodo(ma[i][begin], ma[i][begin], ma[i][begin], ma[i][begin]);
		/*printf("ma[%d][%d] = %lld\n", i, begin, ma[i][begin]);
		printf("tree2[%d]-> begin = %d, end =  %d , maxi = %lld, sum = %lld, pref = %lld, suf %lld\n", j, begin, end, tree2[j].maxi, tree2[j].sum, tree2[j].pref, tree2[j].suf);
		printf("\n");*/
	}
	else{
		int mid=(begin+end)/2;
		int le=2*j+1;
		int ri=2*j+2;
		init2(i, le, begin, mid, tree2);
		init2(i, ri, mid+1, end, tree2);
		tree2[j]=join(tree2[le], tree2[ri]);
		//printf("tree2[%d]-> begin = %d, end =  %d , maxi = %lld, sum = %lld, pref = %lld, suf %lld\n", j, begin, end, tree2[j].maxi, tree2[j].sum, tree2[j].pref, tree2[j].suf);
	}
}

void init(int i, int begin, int end){
	if(begin==end){
		tree[i]=a[begin];
	}
	else{
		int mid=(begin+end)/2;
		int le=2*i+1;
		int ri=2*i+2;
		init(le, begin, mid);
		init(ri, mid+1, end);
		tree[i]=join(tree[le], tree[ri]);
	}
}

void print(){
	cout<<"---------------pRINT------------"<<endl;
	nodo res;
	for(it=mapa.begin(); it!=mapa.end(); it++){
		res=it->second;
		printf("mapa[%d] = %lld\n", it->first, res.maxi);
	}
	cout<<"------------------"<<endl;
}

void print2(){
	cout<<"----------------mATRIZ-----"<<endl;
	for(int i=0; i<ma.size(); i++){
		for(int j=0; j<ma[i].size(); j++){
			printf("%lld ", ma[i][j]);
		}
		printf("\n");
	}
	cout<<"_-------------------------"<<endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	int u, v, t;
	cin>>n>>m;
	ma.assign(n, vi());
	for(int i=0; i<n; i++){
		cin>>t;
		ma[i].assign(t, 0);
		for(int j=0; j<t; j++)
			cin>>ma[i][j];
	}
	//print2();
	a.assign(m, nodo());
	vi b;
	vector<nodo> treeAux;
	for(int i=0; i<m; i++){
		cin>>u;
		u--;
		it=mapa.find(u);
		if(it==mapa.end()){
			b=ma[u];
			t=b.size();
			treeAux.assign(4*t, nodo());
			//cout<<"-----------u = "<<u<<endl;
			init2(u, 0, 0, t-1, treeAux);
			//printf("final = %lld\n", treeAux[0].maxi);
			mapa[u]=treeAux[0];
			treeAux.clear();
		}
		a[i]=mapa[u];
	}
	//print();
	tree.assign(4*m, nodo());
	init(0, 0, m-1);
	printf("%I64d\n", tree[0].maxi);
	return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <limits>

using namespace std;

int q[5000];
long long f[51],s[51],t[51],opt[51];

int main()
{
    long n,i,j,l,ip1;
    long m;
    long long ans = - LONG_MAX;
    long sum, max_sum,max_seq;
    scanf("%d%ld",&n,&m);
    for ( i = 0; i < n; ++i)
    {
        scanf("%d",&l);
        ip1 = i + 1;
        sum = s[ip1] = 0;
        max_sum = max_seq = -LONG_MAX;
        for ( j = 0; j < l; ++j)
        {
            scanf("%d",&q[j]);

            s[ip1] += q[j];
            if (s[ip1] > max_sum)
                max_sum = s[ip1];

            sum += q[j];
            if (sum > max_seq)
                max_seq = sum;
            if (sum < 0)
                sum = 0;

        }
        opt[ip1] = max_seq;
        f[ip1] = max_sum;

        max_sum = -LONG_MAX;
        sum = 0;
        for ( j = l; j-- > 0; )
        {
            sum += q[j];
            if (sum > max_sum)
                max_sum = sum;
        }
        t[ip1] = max_sum;
    }

    long long dp = 0;

    for (i = 0; i < m; ++i)
    {
        scanf("%d", &j);
        if ( dp + f[j] > ans)
            ans = dp + f[j];
        dp += s[j];
        if (t[j] > dp)
            dp = t[j];
        if (dp > ans)
            ans = dp;
    }
    cout << ans;
}


#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int q[5000];
long long f[51],s[51],t[51],ms[51];

int main()
{
    int n,i,j,l;
    long m;
    long long ans = 0;
    long long sum, max_sum,max_seq;
    scanf("%d%ld",&n,&m);
    for ( i = 0; i < n; ++i)
    {
        scanf("%d",&l);
        scanf("%d",&q[0]);
        ms[i+1] = max_sum = max_seq = sum = q[0];
        for ( j = 1; j < l; ++j)
        {
            scanf("%d",&q[j]);
            sum += q[j];
            max_seq += q[j];
            if (max_seq > ms[i+1])
                ms[i+1] = max_seq;
            if (max_seq < 0)
                max_seq = 0;
            if (sum > max_sum)
                max_sum = sum;
        }
        f[i + 1] = max_sum;
        s[i + 1] = sum;
        sum = 0;
        max_sum = q[l - 1];
        for ( j = l; j--; )
        {
            sum += q[j];
            if (sum > max_sum)
                max_sum = sum;
        }
        t[i + 1] = max_sum;
    }
    long long toEnd = 0;
    for ( i = 0; i < m; ++i)
    {
        scanf("%d",&j);
        if (ms[j] > ans) ans = ms[j];
        if (toEnd + f[j] > ans)
            ans = toEnd + f[j];
        toEnd += s[j];
        if ( t[j] > toEnd)
            toEnd = t[j];
        if (toEnd > ans)
            ans = toEnd;
    }
    cout << ans;

}


#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int q[5000],f[51],s[51],t[51],ms[51];

int main()
{
    int n,i,j,m,l;
    long long ans = 0;
    int sum, max_sum,max_seq;
    scanf("%d%d",&n,&m);
    for ( i = 0; i < n; ++i)
    {
        scanf("%d",&l);
        scanf("%d",&q[0]);
        ms[i+1] = max_sum = max_seq = sum = q[0];
        for ( j = 1; j < l; ++j)
        {
            scanf("%d",&q[j]);
            sum += q[j];
            max_seq += q[j];
            if (max_seq > ms[i+1])
                ms[i+1] = max_seq;
            if (max_seq < 0)
                max_seq = 0;
            if (sum > max_sum)
                max_sum = sum;
        }
        f[i + 1] = max_sum;
        s[i + 1] = sum;
        sum = 0;
        max_sum = q[l - 1];
        for ( j = l; j--; )
        {
            sum += q[j];
            if (sum > max_sum)
                max_sum = sum;
        }
        t[i + 1] = max_sum;
    }
    long long toEnd = 0;
    for ( i = 0; i < m; ++i)
    {
        scanf("%d",&j);
        if (ms[j] > ans) ans = ms[j];
        if (toEnd + f[j] > ans)
            ans = toEnd + f[j];
        toEnd += s[j];
        if ( t[j] > toEnd)
            toEnd = t[j];
        if (toEnd > ans)
            ans = toEnd;
    }
    cout << ans;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <climits>

using namespace std;

int q[5000];
long long f[51],s[51],t[51],opt[51];

int main()
{
	long n,i,j,l,ip1;
	long m;
	long long ans = - LONG_MAX;
	long sum, max_sum,max_seq;
	scanf("%ld%ld",&n,&m);

	for ( i = 0; i < n; ++i)
	{
		//����f[],s[],opt[],
		scanf("%ld",&l);
		ip1 = i + 1;
		sum = s[ip1] = 0;
		max_sum = max_seq = -LONG_MAX;
		for ( j = 0; j < l; ++j)
		{
			scanf("%d",&q[j]);

			s[ip1] += q[j];
			if (s[ip1] > max_sum)
				max_sum = s[ip1];

			sum += q[j];
			if (sum > max_seq)
				max_seq = sum;
			if (sum < 0)
				sum = 0;

		}
		opt[ip1] = max_seq;
		f[ip1] = max_sum;

		max_sum = -LONG_MAX;
		sum = 0;
		for ( j = l; j-- > 0; )
		{
			sum += q[j];
			if (sum > max_sum)
				max_sum = sum;
		}
		t[ip1] = max_sum;
	}

	long long dp = 0;

	for (i = 0; i < m; ++i)
	{
		scanf("%ld", &j);
		if (opt[j] > ans)
			ans = opt[j];
		if ( dp + f[j] > ans)
			ans = dp + f[j];
		dp += s[j];
		if (t[j] > dp)
			dp = t[j];
		if (dp > ans)
			ans = dp;
	}
	cout << ans;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <climits>

using namespace std;

int q[5000];
long long f[51],s[51],t[51],opt[51];

int main()
{
    long n,i,j,l,ip1;
    long m;
    long long ans = - LONG_MAX;
    long sum, max_sum,max_seq;
    scanf("%ld%ld",&n,&m);
    for ( i = 0; i < n; ++i)
    {
        scanf("%ld",&l);
        ip1 = i + 1;
        sum = s[ip1] = 0;
        max_sum = max_seq = -LONG_MAX;
        for ( j = 0; j < l; ++j)
        {
            scanf("%d",&q[j]);

            s[ip1] += q[j];
            if (s[ip1] > max_sum)
                max_sum = s[ip1];

            sum += q[j];
            if (sum > max_seq)
                max_seq = sum;
            if (sum < 0)
                sum = 0;

        }
        opt[ip1] = max_seq;
        f[ip1] = max_sum;

        max_sum = -LONG_MAX;
        sum = 0;
        for ( j = l; j-- > 0; )
        {
            sum += q[j];
            if (sum > max_sum)
                max_sum = sum;
        }
        t[ip1] = max_sum;
    }

    long long dp = 0;

    for (i = 0; i < m; ++i)
    {
        scanf("%ld", &j);
        if ( dp + f[j] > ans)
            ans = dp + f[j];
        dp += s[j];
        if (t[j] > dp)
            dp = t[j];
        if (dp > ans)
            ans = dp;
    }
    cout << ans;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector < vector <int> > s;
        vector <int> a;
        int left,sum,right,mm;
        int f (int index, int flag)
        {
            if (index == n) {
                if (flag == 0) {
                    return -MAX;
                }
                return 0;
            }
           // int result;
            int &result = s[index][flag];
            if (result != -1) {
                return result;
            }
            result = -MAX;
            if (flag == 0) {
                result = max (result, f (index+1, 0));
            }

            result = max (result, a[index] + f (index+1, 1));

            return result;
        }
        void ff()
        {
            s.resize (n);
            int i;
            F (i, 0, n) {
                s[i].resize (2);
                s[i][0] = s[i][1] = -1;
            }

            mm = f (0, 0);
        }
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
            ff ();
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
        result = max (result, s[ii].right + f (index+1, 1));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));

    return result;
}

int main()
{
   // freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);
    int xx;
    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
      //  cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
        if (ii == 0) {
            xx = s[x].mm;
        } else {
            xx = max (xx, s[x].mm);
        }
    }
    memset (dp, -1, sizeof(dp));
    xx = max (xx, f (0, 0));
    printf ("%d\n", xx);
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
ill m;
class aman
{
    private:

    public:
        ill n;
        vector < vector <ill> > s;
        vector <ill> a;
        ill left,sum,right,mm;
        ill f (ill index, ill flag)
        {
            if (index == n) {
                if (flag == 0) {
                    return -MAX;
                }
                return 0;
            }
           // ill result;
            ill &result = s[index][flag];
            if (result != -1) {
                return result;
            }
            result = -MAX;
            if (flag == 1) result = 0;
            if (flag == 0) {
                result = max (result, f (index+1, 0));
            }

            result = max (result, a[index] + f (index+1, 1));

            return result;
        }
        void ff()
        {
            s.resize (n);
            ill i;
            F (i, 0, n) {
                s[i].resize (2);
                s[i][0] = s[i][1] = -1;
            }

            mm = f (0, 0);
        }
        void process()
        {
            cin >> n;
            ill i;
            sum = 0;
            F (i, 0, n) {
                ill x;
                S1 (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
            ff ();
        }

};
vector <aman> s;
vector <ill> ss;
ill dp[250009][2];

ill f (ill index, ill flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    ill ii = ss[index];
    ill &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 1) result = 0;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
        result = max (result, s[ii].right + f (index+1, 1));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));

    return result;
}

int main()
{
//    freopen ("input.txt", "r", stdin);
    ill n;
    S1 (n);  S1 (m);
    s.resize (n);
    ill xx;
    for (ill ii = 0; ii < n; ii++) {
        s[ii].process();
      //  cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (ill ii = 0; ii < m; ii++) {
        ill x;
        S1 (x);
        x--;
        ss.pb (x);
        if (ii == 0) {
            xx = s[x].mm;
        } else {
            xx = max (xx, s[x].mm);
        }
    }
    memset (dp, -1, sizeof(dp));
    xx = max (xx, f (0, 0));
    printf ("%lld\n", xx);
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector < vector <int> > s;
        vector <int> a;
        int left,sum,right,mm;
        int f (int index, int flag)
        {
            if (index == n) {
                if (flag == 0) {
                    return -MAX;
                }
                return 0;
            }
           // int result;
            int &result = s[index][flag];
            if (result != -1) {
                return result;
            }
            result = -MAX;
            if (flag == 1) result = 0;
            if (flag == 0) {
                result = max (result, f (index+1, 0));
            }

            result = max (result, a[index] + f (index+1, 1));

            return result;
        }
        void ff()
        {
            s.resize (n);
            int i;
            F (i, 0, n) {
                s[i].resize (2);
                s[i][0] = s[i][1] = -1;
            }

            mm = f (0, 0);
        }
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
            ff ();
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 1) result = 0;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
        result = max (result, s[ii].right + f (index+1, 1));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));

    return result;
}

int main()
{
//    freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);
    int xx;
    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
      //  cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
        if (ii == 0) {
            xx = s[x].mm;
        } else {
            xx = max (xx, s[x].mm);
        }
    }
    memset (dp, -1, sizeof(dp));
    xx = max (xx, f (0, 0));
    printf ("%d\n", xx);
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector <int> a;
        int left,sum;
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));
    return result;
}

int main()
{
    //freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);

    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
        //111111111111111cout << s[ii].left << " " << s[ii].sum << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
    }
    memset (dp, -1, sizeof(dp));
    printf ("%d\n", f (0, 0));
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector < vector <int> > s;
        vector <int> a;
        int left,sum,right,mm;
        int f (int index, int flag)
        {
            if (index == n) {
                if (flag == 0) {
                    return -MAX;
                }
                return 0;
            }
            int result;
           // int &result = s[index][flag];
            if (result != -1) {
                return result;
            }
            result = -MAX;
            if (flag == 0) {
                result = max (result, f (index+1, 0));
            }

            result = max (result, a[index] + f (index+1, 1));

            return result;
        }
        void ff()
        {
            s.resize (n);
            int i;
            F (i, 0, n) {
                s[i].resize (2);
                s[i][0] = s[i][1] = -1;
            }

            mm = f (0, 0);
        }
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
            ff ();
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
        result = max (result, s[ii].right + f (index+1, 1));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));

    return result;
}

int main()
{
  //  freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);
    int xx;
    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
        if (ii == 0) {
            xx = s[ii].mm;
        } else {
            xx = max (xx, s[ii].mm);
        }
      //  cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
    }
    memset (dp, -1, sizeof(dp));
    xx = max (xx, f (0, 0));
    printf ("%d\n", xx);
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector <int> a;
        int left,sum,right;
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
        result = max (result, s[ii].right + f (index+1, 1));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));

    return result;
}

int main()
{
    //freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);

    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
      //  cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
    }
    memset (dp, -1, sizeof(dp));
    printf ("%d\n", f (0, 0));
    return  0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define MAX 10000001
#define NIL 0
#define INF (1<<28)
using namespace std;
int m;
class aman
{
    private:

    public:
        int n;
        vector <int> a;
        int left,sum,right;
        void process()
        {
            cin >> n;
            int i;
            sum = 0;
            F (i, 0, n) {
                int x;
                S (x);
                a.pb (x);
                if (i == 0) {
                    left = a[i];
                } else {
                    left = max (left, sum + a[i]);
                }
                sum = sum + a[i];
            }
            right;
            sum = 0;
            for (i = n-1; i >= 0; i--) {
                if (i == n-1) {
                    right = a[i];
                    sum = right;
                    continue;
                }
                right = max (right, sum + a[i]);
                sum = sum + a[i];
            }
        }

};
vector <aman> s;
vector <int> ss;
int dp[250009][2];

int f (int index, int flag)
{
    if (index == m) {
        if (flag == 0) {
            return -MAX;
        }
        return 0;
    }
    int ii = ss[index];
    int &result = dp[index][flag];
    if (result != -1) {
        return result;
    }
    result = -MAX;
    if (flag == 0) {
        result = max (result, f (index+1, 0));
    }
    result = max (result, s[ii].left);
    result = max (result, s[ii].sum + f (index+1, 1));
    result = max (result, s[ii].right + f (index+1, 1));
    return result;
}

int main()
{
    //freopen ("input.txt", "r", stdin);
    int n;
    S (n);  S (m);
    s.resize (n);

    for (int ii = 0; ii < n; ii++) {
        s[ii].process();
   //     cout << s[ii].left << " " << s[ii].sum << " " << s[ii].right << endl;
    }
    for (int ii = 0; ii < m; ii++) {
        int x;
        S (x);
        x--;
        ss.pb (x);
    }
    memset (dp, -1, sizeof(dp));
    printf ("%d\n", f (0, 0));
    return  0;
}


#include<iostream>
using namespace std;
#define INT_MAX 999999999
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=0; i<t; ++i){
        sum+=num[i];
        sum1+=num[t-i-1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;
        if(dp1[i-1]+num[i]>0){
            dp1[i]=dp1[i-1]+num[i];
            if(dp1[i]>max1) max1=dp1[i];
        }else dp1[i]=0;
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
//    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=0; j<t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
#define INT_MAX 999999999
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=0; i<t; ++i){
        sum+=num[i];
        sum1+=num[t-i-1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;
        if(i!=0) dp1[i]=dp1[i-1]+num[i];
        if(dp1[i]>max1) max1=dp1[i];
        if(dp1[i]<0 ) dp1[i]=0;
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
//    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=0; j<t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=0; i<t; ++i){
        sum+=num[i];
        sum1+=num[t-i-1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;
        if(dp1[i-1]+num[i]>0){
            dp1[i]=dp1[i-1]+num[i];
            if(dp1[i]>max1) max1=dp1[i];
        }else dp1[i]=0;
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
//    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=0; j<t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
#define INT_MAX 999999999
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=1; i<=t; ++i){
        sum+=num[i];
        sum1+=num[t-i+1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;

        dp1[i]=dp1[i-1]+num[i];
        if(dp1[i]>max1) max1=dp1[i];
        if(dp1[i]<0 ) dp1[i]=0;
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
//    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=1; j<=t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
#define INT_MAX 999999999
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=1; i<=t; ++i){
        sum+=num[i];
        sum1+=num[t-i+1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;

        dp1[i]=dp1[i-1]+num[i];
        if(dp1[i]>max1) max1=dp1[i];
        if(dp1[i]<0 ) dp1[i]=0;
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=1; j<=t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
#define INT_MAX 999999999
int dp[50001][4];

void chuli(int x, int num[50001],int t){
    int sum=0,sum1=0;
    int max1=-INT_MAX,max2=-INT_MAX,max3=-INT_MAX;
    int dp1[50001]={0};
    for(int i=0; i<t; ++i){
        sum+=num[i];
        sum1+=num[t-i-1];
        if(sum > max2 ) max2=sum;
        if(sum1> max3 ) max3=sum1;

        if(i!=0 && dp1[i-1]+num[i]>=0){
            dp1[i]=dp1[i-1]+num[i];
            if(dp1[i]>max1) max1=dp1[i];
        }else{
            if(num[i] > max1 ) max1=num[i];
            dp1[i]=0;
        }
    }
    dp[x][0]=max1; // 此数组最大值
    dp[x][1]=max2; // 开头往后最大能获得的值
    dp[x][2]=max3; //末尾往前最大能获得的值
    dp[x][3]=sum;  //整个数组的和
//    cout << max1 <<" "<< max2 <<" "<< max3 <<" "<< sum << endl;
}

int main(){
    int n,m;
    while( cin >> n >> m){
        for(int i=1; i<=n; ++i){
            int t;
            cin >> t;
            int num[5001];
            for(int j=0; j<t; ++j)
                cin >> num[j];
            chuli(i,num,t);
        }
        long long ans=-INT_MAX,wei=0;
        for(int i=0; i<m; ++i){
            int num1;
            cin >> num1;
            if(dp[num1][0]>ans) ans=dp[num1][0];
             if(wei+dp[num1][1] > ans ) ans = wei+dp[num1][1];
            if( wei+dp[num1][3] > dp[num1][2]){
                wei+=dp[num1][3];
            }else wei=dp[num1][2];
            if(wei > ans ) ans =wei;

        }
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=a[0];
    s=0;
    for(int j=1;j<len;j++){
      s=max(s,s+a[i]);
      mx[i]=max(mx[i],s);
    }
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,2)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50];
int b[250000];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;
  }
  rep(i,m)scanf("%d",b+i), b[i]--;

  ll ans=max(lmax[b[0]],rmax[b[0]]), s=rmax[b[0]];
  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,2)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[5000];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=mx[i]=-inf;
    ll s=0,t=0,u=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
      u=max((ll)a[j],u+a[j]);
      mx[i]=max(mx[i],u);
    }
    sum[i]=s;
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,3)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=a[0];
    s=0;
    for(int j=1;j<len;j++){
      s=max((ll)a[i],s+a[i]);
      mx[i]=max(mx[i],s);
    }
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,2)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=-inf;
    rep(r,len)rep(l,r+1)
    {
      ll s=0;
      for(int j=l;j<=r;j++)s+=a[j];
      mx[i]=max(mx[i],s);
    }

  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,2)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=a[0];
    s=0;
    for(int j=1;j<len;j++){
      s=max(a[i],s+a[i]);
      mx[i]=max(mx[i],s);
    }
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,2)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=a[0];
    s=0;
    for(int j=1;j<len;j++){
      s=max((ll)a[j],s+a[j]);
      mx[i]=max(mx[i],s);
    }
  }

  ll ans=-inf, s=rmax[b[0]];
  rep(i,m)scanf("%d",b+i), ans=max(ans,mx[--b[i]]);

  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,3)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=mx[i]=-inf;
    ll s=0,t=0,u=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
      u=max((ll)a[j],u+a[j]);
      mx[i]=max(mx[i],u);
    }
    sum[i]=s;
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,3)
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;
const ll inf=(1ll<<60);
int n,m;
ll lmax[50], rmax[50], sum[50], mx[50];
int b[250000];

bool use[50];

void run(){
  scanf("%d%d",&n,&m);

  rep(i,n){
    int len,a[50];
    scanf("%d",&len);
    rep(j,len)scanf("%d",a+j);

    lmax[i]=rmax[i]=-inf;
    ll s=0,t=0;
    rep(j,len){
      s+=a[j]; t+=a[len-1-j];
      lmax[i]=max(lmax[i],s);
      rmax[i]=max(rmax[i],t);
    }
    sum[i]=s;

    mx[i]=a[0];
    s=0;
    for(int j=1;j<len;j++){
      s=max((ll)a[j],s+a[j]);
      mx[i]=max(mx[i],s);
    }
  }
  rep(i,n)use[i]=0;
  rep(i,m)scanf("%d",b+i), use[--b[i]]=1;

  ll ans=-inf, s=rmax[b[0]];

  rep(i,n)if(use[i])ans=max(ans,mx[i]);
  ans=max(ans, max(lmax[b[0]],rmax[b[0]]));

  for(int i=1;i<m;i++){

    ans=max(ans,max(lmax[b[i]],rmax[b[i]]));
    ans=max(ans,s+lmax[b[i]]);
    s=max(rmax[b[i]],s+sum[b[i]]);
  }
  cout<<ans<<endl;
}
int main(){
  //rep(i,3)
    run();
  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= -(1LL<<62);
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll least= vv[0];ar[i].maxi=ar[i].e= least;ar[i].maxi= least;
		for(int j=1;j<vv.size();++j){
			ar[i].maxi = max(ar[i].maxi,vv[j]-least);
			if(j==vv.size()-1) ar[i].e = sum-least;
			least= min(vv[j],least);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *min_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum;
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= -(1LL<<62);
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll least= vv[0];ar[i].maxi=ar[i].e= least;ar[i].maxi= sum;
		for(int j=1;j<vv.size();++j){
			ar[i].maxi = max(ar[i].maxi,max(vv[j]-least,vv[j]));
			if(j==vv.size()-1) ar[i].e = max(sum-least,sum);
			least= min(vv[j],least);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *min_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum;
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *max_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum,ar[i].e = max(sum-mini,sum),ar[i].maxi = max(maxi,maxi-mini);
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll least= vv[0];ar[i].maxi= least;
		for(int j=1;j<vv.size();++j){
			ar[i].maxi = max(ar[i].maxi,vv[j]-least);
			least= min(vv[j],least);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *min_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum,ar[i].e = max(sum-mini,sum);
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll least= vv[0];ar[i].maxi=ar[i].e= least;
		for(int j=1;j<vv.size();++j){
			ar[i].maxi = max(ar[i].maxi,vv[j]-least);
			if(j==vv.size()-1) ar[i].e = sum-least;
			least= min(vv[j],least);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *min_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum;
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long int
struct t{
	ll p,w,e,maxi;
}ar[1000];
int main(){
	int n,m,c,k;
	ll ret= -(1LL<<62);
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>c;
		vector<ll> vv;ll sum = 0;
		for(int j=0;j<c;++j){
			cin>>k; sum+= k;vv.push_back(sum);
		}
		ll least= vv[0];ar[i].maxi=ar[i].e= least;ar[i].maxi= least;
		for(int j=1;j<vv.size();++j){
			ar[i].maxi = max(ar[i].maxi,vv[j]-least);
			if(j==vv.size()-1) ar[i].e = max(sum-least,sum);
			least= min(vv[j],least);
		}
		ll maxi = *max_element(vv.begin(),vv.end()), mini = *min_element(vv.begin(),vv.end());
		ar[i].p = maxi,ar[i].w= sum;
	}
	ll con = 0;
	for(int i=0;i<m;++i){
		cin>>c;--c;
		ret= max(ret,max(con+ar[c].p,ar[c].maxi));
		con = max(con+ar[c].w , ar[c].e);
	}
	ret= max(con,ret);
	cout<<ret<<endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b*a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            a.push_back(b);}
            else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            a.push_back(b);}
            else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            {
                if(j>1)
                {
                     if(b<0&&a[j-1]<0&&a[j-2]<0)
                {
                    if(j>a[j-2])
                    {
                        a[j-1]+=a[j-2];
                        a[j-2]=b;
                    }
                    else
                    a[j-1]+=b;
                }
                else a.push_back(b);
                }
                else a.push_back(b);

                   }

            }
                        else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            a.push_back(b);
        }
        t.resize(4*c);
        init1(0,c-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <int> > t;
vector<int> a;
vector<vector<int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            a.push_back(b);
        }
        t.resize(4*c);
        init1(0,c-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <int> > t;
vector<int> a;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <int> > vec;
    vector<int> lista;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            lista.push_back(b);
        }
        vec.push_back(lista);
        lista.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        for(int j=0;j<vec[b-1].size();j++)
        {
            a.push_back(vec[b-1][j]);
        }
    }
    n=a.size();
    t.resize(4*n);
    init(0,n-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            {
                if(j>1)
                {
                     if(b<0&&a[j-1]<0&&a[j-2]<0)
                {
                    if(j>a[j-2])
                    {
                        a[j-1]+=a[j-2];
                        a[j-2]=b; j--; c--;
                    }
                    else
                    {a[j-1]+=b; j--; c--;}
                }
                else a.push_back(b);
                }
                else a.push_back(b);

                   }

            }
                        else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            {
                if(j>1)
                {
                     if(b<0&&a[j-1]<0&&a[j-2]<0)
                {
                    if(b>a[j-2])
                    {
                        a[j-1]+=a[j-2];
                        a[j-2]=b; c--; j--;
                    }
                    else
                    {a[j-1]+=b; c--; j--;}
                }
                else a.push_back(b);
                }
                else a.push_back(b);

                   }

            }
                        else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            {
                if(j>1)
                {
                     if(b<0&&a[j-1]<0&&a[j-2]<0)
                {
                    if(b>a[j-2]&&b>a[j-1])
                     {
                        a[j-1]+=a[j-2];
                        a[j-2]=b; j--; c--;
                     }
                     else
                     {
                        if(a[j-2]>b&&a[j-2]>a[j-1])
                        {
                            a[j-1]+=b; c--; j--;
                        }
                        else
                        if(a[j-1]>b&&a[j-1]>a[j-2])
                        {
                            b+=a[j-2];
                            a[j-2]=a[j-1];
                            a[j-1]+=b; c--;j--;
                        }
                        else
                        {
                            a[j-1]+=b; c--; j--;
                        }
                        }

                }
                else a.push_back(b);
                }
                else a.push_back(b);

                   }

            }
                        else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector <long long int> > t;
vector<long long int> a;
vector<vector<long long int> >vect;

void init(int b, int e, int nodo)
{
    if(e==b)
    {
        t[nodo]=vect[b];
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init(b,mid,l); init(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}
void init1(int b, int e, int nodo)
{

    if(e==b)
    {
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
        t[nodo].push_back(a[b]);
    }
    else
    {
        int mid=(b+e)/2, l=nodo*2+1, r=l+1;
        init1(b,mid,l); init1(mid+1,e,r);
        t[nodo].push_back(max(t[r][0],max(t[r][2]+t[l][0],t[r][2])));
        t[nodo].push_back(max(t[l][1],max(t[l][2]+t[r][1],t[l][2])));
        t[nodo].push_back(t[r][2]+t[l][2]);
        t[nodo].push_back(max(max(t[nodo][0],t[nodo][1]),max(max(t[l][0],max(t[l][3],t[r][3])),max(t[r][1],t[r][1]+t[l][0]))));
    }
}

int main() {
    int n,N,M,b,c;
    vector<vector <long long int> > vec;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
        cin>>c;
        for(int j=0;j<c;j++)
        {
            cin>>b;
            if(j>0){
            if(b>=0&&a[j-1]>=0)
            {a[j-1]=b+a[j-1]; j--; c--;}
            else
            {
                if(j>1)
                {
                     if(b<0&&a[j-1]<0&&a[j-2]<0)
                {
                    if(b>=a[j-2]&&b>=a[j-1])
                     {
                        a[j-1]+=a[j-2];
                        a[j-2]=b; j--; c--;
                     }
                     else
                     {
                        if(a[j-2]>=b&&a[j-2]>=a[j-1])
                        {
                            a[j-1]+=b; c--; j--;
                        }
                        else
                        {
                            b+=a[j-2];
                            a[j-2]=a[j-1];
                            a[j-1]+=b; c--;j--;
                        }}

                }
                else a.push_back(b);
                }
                else a.push_back(b);

                   }

            }
                        else
            a.push_back(b);
        }
        t.resize(4*a.size());
        init1(0,a.size()-1,0);
        vec.push_back(t[0]);
        t.clear();
        a.clear();
    }
    for(int i=0;i<M;i++)
    {
        cin>>b;
        vect.push_back(vec[b-1]);
    }
    t.resize(4*M);
    init(0,M-1,0);
    cout<<t[0][3]<<endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

const int MAXN = 25111;
const int INF = 0x3f3f3f3f;

LL LEFT[MAXN], RIGHT[MAXN], maxsum[MAXN], sum[MAXN];
LL idx[MAXN], f[MAXN], dp[MAXN];

void gao(const LL n, const LL k) {
    LL i, j;
    LL ThisSum(0), MaxSum(0);
    LL maxneg(-INF);

    LEFT[k] = maxsum[k] = RIGHT[k] = -INF;

    for (i=0; i<n; ++i) {
        maxneg = max(maxneg, f[i]);

        ThisSum += f[i];

        if (ThisSum > MaxSum) MaxSum = ThisSum;
        else if (ThisSum < 0) ThisSum = 0;
    }
    maxsum[k] = MaxSum;
    if (maxneg < 0) maxsum[k] = maxneg;

    sum[k] = 0;
    for (i=0; i<n; ++i) sum[k] += f[i];

    LL ls(0), rs(0);
    for (i=0; i<n; ++i) {
        ls += f[i];
        LEFT[k] = max(LEFT[k], ls);
    }

    for (i=n-1; i>=0; --i) {
        rs += f[i];
        RIGHT[k] = max(RIGHT[k], rs);
    }
}

int main() {
    LL i, j, k;
    LL m, n;
    LL r;
    LL res, temp, tot(0);

    cin >> n >> m;

    for (i=0; i<n; ++i) {
        cin >> r;
        for (j=0; j<r; ++j) cin >> f[j];
        gao(r, i);
    }

    for (i=0; i<m; ++i) {
        cin >> idx[i];
        --idx[i];
    }

    res = 0;
    dp[0] = RIGHT[idx[0]];

    tot = -INF;
    for (i=0; i<m; ++i) {
        tot = max(tot, maxsum[idx[i]]);
    }

    for (i=1; i<m; ++i) {
        dp[i] = max(RIGHT[idx[i]], dp[i-1]+sum[idx[i]]);
    }

    for (i=0; i<m-1; ++i) {
        tot = max(tot, dp[i]);
        tot = max(tot, dp[i]+LEFT[idx[i+1]]);
    }

    //for (i=0; i<n; ++i) printf("----%lld %lld %lld\n", maxsum[i], LEFT[i], RIGHT[i]);
    //for (i=0; i<m; ++i) printf("dp---%lld ", dp[i]); puts("");

    cout << tot << endl;

    return 0;
}