p0 = []
maxp = []
for i in range(n):
    p0.append(p[i][0])
    sumf = sum(f[i])
    maxp.append(max(p[i][1:(sumf+1)]))

cnt = 0
sum_max = 0
for i in range(n):
    if p0[i] > maxp[i]:
        cnt += 1
    sum_max += max(p0[i],maxp[i])

if cnt == n:
    a = []
    for i in range(n):
        a.append(p0[i] + sum(maxp[:i]) + sum(maxp[i+1:]))
    print(max(a))
else:
    print(sum_max)