#include <bits/stdc++.h>   using namespace std;   long long exp(long long x, long long y) {     x %= 1000000007;     long long res = 1;     while (y) {       if (y & 1) res = res * x % 1000000007;       x = x * x % 1000000007;       y >>= 1;     }     return res;   }   long long fact(long long n) {     long long res = 1;     for (long long i = 2; i <= n; ++i) (res *= i) %= 1000000007;     return res;   }   bool isprime(long long n) {     for (long long i = 2; i * i <= n; ++i)       if (n % i == 0) return 0;     return 1;   }   int main() {     int n, m;     cin >> n >> m;     const long long inf = 1e17;     vector<long long> sum(n), pre(n), s(n);     vector<long long> t(n);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       long long val = -inf;       long long mx = -inf;       vector<long long> v1, v2;       for (int j = 0; j < x; ++j) {         long long k;         cin >> k;         val = max(k, val + k);         mx = max(mx, val);         v1.push_back(k);         v2.push_back(k);       }       t[i] = mx;       for (int j = 1; j < x; ++j) v1[j] += v1[j - 1];       for (int j = x - 2; j >= 0; --j) v2[j] += v2[j + 1];       sum[i] = v1[x - 1];       pre[i] = *max_element(v1.begin(), v1.end());       s[i] = *max_element(v2.begin(), v2.end());     }     long long to = -inf;     vector<int> v(m + 1);     for (int i = 1; i <= m; ++i) {       cin >> v[i];       to = max(to, t[v[i] - 1]);     }     long long dp[m + 1][3];     memset(dp, 0, sizeof(dp));     for (int i = 1; i <= m; ++i) {       dp[i][0] = s[v[i] - 1];       dp[i][1] = max(dp[i - 1][0] + sum[v[i] - 1], dp[i - 1][1] + sum[v[i] - 1]);       dp[i][2] = max(dp[i - 1][0] + pre[v[i] - 1], dp[i - 1][1] + pre[v[i] - 1]);     }     long long ans = -inf;     for (int i = 1; i <= m; ++i) ans = max(ans, dp[i][2]);     cout << max(ans, to);     return 0;   }   