#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long n;
  long long ans = 0;
  cin >> n;
  long long a[n][5][2], p[n][11];
  long long co[n];
  for (int i = 0; i < n; i++) {
    co[i] = 0;
    for (int j = 0; j < 5; j++) {
      cin >> a[i][j][0] >> a[i][j][1];
      co[i] += (a[i][j][0] == 1) + (a[i][j][1] == 1);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 11; j++) cin >> p[i][j];
  }
  bool f = false;
  for (int i = 0; i < n; i++) {
    int maxdex = 0;
    for (int j = 1; j <= co[i]; j++) {
      if (p[i][j] > p[i][maxdex]) maxdex = j;
    }
    if (maxdex != 0) f = true;
    ans += p[i][maxdex];
  }
  if (f)
    cout << ans << endl;
  else {
    long long mn = INT_MAX;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= co[i]; j++) mn = min(mn, p[i][0] - p[i][j]);
    }
    cout << ans - mn << endl;
  }
  return 0;
}
