#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int f[n][5][2];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 2; k++) cin >> f[i][j][k];
  int p[n][11];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 11; j++) cin >> p[i][j];
  int ans = 0, res = 0;
  bool check = false, alt = false;
  int least[n];
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 2; k++) cnt += f[i][j][k];
    alt = false;
    least[i] = res = -10000001;
    for (int j = 0; j < cnt + 1; j++) {
      if (j != 0 && res < p[i][j]) alt = true;
      res = max(res, p[i][j]);
      if (j != 0) least[i] = max(least[i], p[i][j]);
    }
    ans += res;
    if (alt) check = true;
  }
  if (check)
    cout << ans << endl;
  else {
    ans = -10000001;
    for (int i = 0; i < n; i++) ans = max(ans, least[i]);
    cout << ans << endl;
  }
  return 0;
}
