using System;
using System.Linq;

namespace ABC080
{
    public class Problem_C
    {
        public static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var schedules = new int[n][];
            var profits = new int[n + 1][];
            for (var i = 0; i < n; i++)
            {
                schedules[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            }
            for (var i = 0; i < n; i++)
            {
                profits[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            }

            var headSum = 0;
            var minHead = int.MaxValue;
            var maxWithoutHead = int.MinValue;
            var p = 0;
            for (var i = 0; i < n; i++)
            {
                var open = schedules[i].Where(x => x == 1).Count();
                var maxProf = profits[i].Take(open + 1).Max();
                p += maxProf;

                headSum += profits[i][0];
                minHead = Math.Min(minHead, profits[i][0]);
                maxWithoutHead = Math.Max(maxWithoutHead, profits[i].Skip(1).Take(open).Max());
            }
            p = p == headSum ? p - minHead + maxWithoutHead : p;
            Console.WriteLine($"{p}");
        }
    }
}
