#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, k, p[101][11], m = 0, ma = 0, ans = 0;
  bool f[105][6][3];
  cin >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= 5; j++) cin >> f[i][j][1] >> f[i][j][2];
  for (i = 1; i <= n; i++)
    for (j = 0; j <= 10; j++) cin >> p[i][j];
  for (i = 1; i <= 5; i++)
    for (j = 1; j <= 2; j++) {
      m = 0;
      for (k = 1; k <= n; k++) {
        if (f[k][i][j] == 1 && p[k][1] > m) {
          m = max(m, p[k][1]);
          ma = k;
        }
      }
      ans += m;
      p[ma][1] = p[ma][2] - p[ma][1];
      for (k = 3; k <= n; k++) p[ma][k - 1] = p[ma][k];
    }
  if (ans == 0) {
    m = 0;
    for (i = 1; i <= n; i++) m = max(p[i][1], m);
    cout << m;
    return 0;
  }
  cout << ans;
  return 0;
}
