func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, m, l;   long long dp[250005];   short big_arr[250005], small_arr[5001];   long long pre[51], sum[51], suf[51], mx[51], maxx = -1e5;   long long solve(int indx, long long curSum) {     if (indx == m + 1) return -2000;     short &i = big_arr[indx];     long long &res = dp[indx];     if (~res) return res;     res = pre[i];     if (curSum + sum[i] >= suf[i] && curSum + sum[i] > 0)       res = max(res, solve(indx + 1, curSum + sum[i]) + sum[i]);     else if (suf[i] > 0)       res = max(res, solve(indx + 1, suf[i]) + suf[i] - curSum);     else       res = max(res, solve(indx + 1, 0) - curSum);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL), cout.tie(NULL);     cin >> n >> m;     fill(pre, pre + 51, -1e5);     fill(suf, suf + 51, -1e5);     fill(mx, mx + 51, -1e5);     fill(dp, dp + 250005, -1);     for (int i = 0; i < n; i++) {       cin >> l;       long long tmp = 0;       for (int j = 0; j < l; j++) {         cin >> small_arr[j];         mx[i] = max((long long)small_arr[j], mx[i]);         if (tmp < 0) tmp = 0;         tmp += small_arr[j];         mx[i] = max(mx[i], tmp);         sum[i] += small_arr[j];         pre[i] = max(pre[i], sum[i]);       }       tmp = 0;       for (int j = l - 1; j >= 0; j--) {         tmp += small_arr[j];         suf[i] = max(suf[i], tmp);       }     }     for (int i = 1; i <= m; i++) {       cin >> big_arr[i];       big_arr[i]--;       maxx = max(mx[big_arr[i]], maxx);     }     cout << max(solve(1, 0), maxx) << endl;   }   