#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> s;
  vector<vector<int>> p;
  int a, b;
  for (int i = 0; i < n; i++) {
    vector<pair<int, int>> v;
    for (int j = 0; j < 5; j++) {
      cin >> a >> b;
      v.push_back(make_pair(a, b));
    }
    s.push_back(v);
  }
  int pp;
  for (int j = 0; j < n; j++) {
    vector<int> v;
    for (int k = 0; k < 11; k++) {
      cin >> pp;
      v.push_back(pp);
    }
    p.push_back(v);
  }
  long long mx = 0LL;
  long long mnx = numeric_limits<long long>::min();
  bool c = false;
  for (int i = 0; i < n; i++) {
    int x = 0;
    for (int j = 0; j < 5; j++) {
      x += s[i][j].first;
      x += s[i][j].second;
    }
    long long mmx1 = numeric_limits<long long>::min();
    long long mmx2 = numeric_limits<long long>::min();
    int y = -1;
    for (int k = 0; k <= x; k++) {
      if (p[i][k] > mmx1) {
        mmx1 = p[i][k];
        y = k;
      }
      if (k && p[i][k] != mmx1 && p[i][k] > mmx2) {
        mmx2 = p[i][k];
      }
    }
    if (y != 0) c = true;
    if (mmx2 > mnx) mnx = mmx2;
    mx += (long long)mmx1;
  }
  if (!c) mx += mnx;
  cout << mx << endl;
  return 0;
}
