#include <bits/stdc++.h>
using namespace std;
long long _set(long long N, long long pos) { return N = N | (1 << pos); }
long long _reset(long long N, long long pos) { return N = N & ~(1 << pos); }
bool _check(long long N, long long pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }
long long dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
long long dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
int pn[110], sp[110][7][3], pr[110][13], max0[110], max1[110];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 5; j++)
      cin >> sp[i][j][1] >> sp[i][j][2], pn[i] += sp[i][j][1] + sp[i][j][2];
  }
  int ans = 0, cnt = 0, f = 0;
  for (int i = 1; i <= n; i++) {
    int x = INT_MIN, y = x;
    for (int j = 0; j <= 10; j++) {
      cin >> pr[i][j];
      if (j <= pn[i]) {
        if (pr[i][j] >= x) max0[i] = x = pr[i][j], f = j;
      }
      if (j <= pn[i] && j) {
        if (pr[i][j] >= y) max1[i] = y = pr[i][j];
      }
    }
    cnt += f;
    ans += x;
  }
  if (!cnt) {
    int id = INT_MIN;
    for (int i = 1; i <= n; i++) id = max(id, max1[i] - max0[i]);
    ans += id;
  }
  cout << ans << '\n';
  return 0;
}
