Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.

The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.

Suppose Ciel and Jiro play optimally, what is the score of the game?


-----Input-----

The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is s_{i} (1 ≤ s_{i} ≤ 100) — the number of cards in the i-th pile; then follow s_{i} positive integers c_1, c_2, ..., c_{k}, ..., c_{s}_{i} (1 ≤ c_{k} ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.


-----Output-----

Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.


-----Examples-----
Input
2
1 100
2 1 10

Output
101 10

Input
1
9 2 8 6 5 9 4 7 1 3

Output
30 15

Input
3
3 1 3 2
3 5 4 6
2 8 7

Output
18 18

Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000

Output
7000 7000



-----Note-----

In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.

In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.

Difficulty: HARD

Solutions:
from functools import reduce
n = int(input())
cards = [list(map(int, input().split()[1:])) for i in range(n)]
mid = sorted((c[len(c) >> 1] for c in cards if len(c) & 1 == 1), reverse=True)
add = lambda x=0, y=0: x + y
(a, b) = (reduce(add, mid[::2] or [0]), reduce(add, mid[1::2] or [0]))
for c in cards:
	m = len(c) >> 1
	a += reduce(add, c[:m] or [0])
	b += reduce(add, c[m + (len(c) & 1):] or [0])
print(a, b)


n = int(input())
(s1, s2) = (0, 0)
tab = []
for i in range(n):
	c = list(map(int, input().split()))
	for j in range(1, c[0] + 1):
		if j * 2 <= c[0]:
			s1 += c[j]
		else:
			s2 += c[j]
	if c[0] & 1:
		s2 -= c[(c[0] + 1) // 2]
		tab.append(c[(c[0] + 1) // 2])
if len(tab):
	tab.sort()
	tab.reverse()
	for i in range(len(tab)):
		if i & 1:
			s2 += tab[i]
		else:
			s1 += tab[i]
print(s1, s2)


n = int(input())
(a, b) = (0, 0)
l = []
for _ in range(n):
	inpt = list(map(int, input().split()))[1:]
	li = len(inpt)
	if li % 2:
		l.append(inpt[li // 2])
	a += sum(inpt[:li // 2])
	b += sum(inpt[(li + 1) // 2:])
l.sort(reverse=True)
a += sum(l[::2])
b += sum(l[1::2])
print(a, b)


n = int(input())
lista = []
aux = []
somaA = 0
somaB = 0
for i in range(n):
	a = [int(i) for i in input().split()][1:]
	if len(a) > 1:
		somaA += sum(a[0:len(a) // 2])
		somaB += sum(a[-(len(a) // 2):])
	if len(a) % 2 == 1:
		aux.append(a[len(a) // 2])
aux.sort(reverse=True)
for i in range(0, len(aux), 2):
	somaA += aux[i]
for i in range(1, len(aux), 2):
	somaB += aux[i]
print(somaA, somaB)


n = int(input())
a = b = 0
s = []
for _ in range(n):
	l = [*map(int, input().split())][1:]
	m = len(l)
	if m & 1:
		s.append(l[m // 2])
	a += sum(l[:m // 2])
	b += sum(l[(m + 1) // 2:])
s.sort(reverse=True)
a += sum(s[::2])
b += sum(s[1::2])
print(a, b)


import sys
from functools import reduce
for n in sys.stdin:
	n = int(n)
	cards = [list(map(int, input().split()[1:])) for i in range(n)]
	mid = []
	(a, b) = (0, 0)
	add = lambda x=0, y=0: x + y
	for c in cards:
		s = len(c)
		m = s >> 1
		a += reduce(add, c[:m] or [0])
		b += reduce(add, c[m + (s & 1):] or [0])
		if s & 1 == 1:
			mid.append(c[m])
	mid.sort(reverse=True)
	j = True
	for c in mid:
		if j:
			a += c
		else:
			b += c
		j = not j
	print(a, b)


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]

def sol(n):
	cm = []
	res1 = 0
	res2 = 0
	for i in range(n):
		x = rrm()
		if x[0] % 2 == 1:
			cm.append(x[x[0] // 2 + 1])
		res1 += sum(x[1:x[0] // 2 + 1])
		res2 += sum(x[(x[0] + 1) // 2 + 1:])
	cm.sort(reverse=True)
	for (i, v) in enumerate(cm):
		if i % 2 == 0:
			res1 += v
		else:
			res2 += v
	return str(res1) + ' ' + str(res2)
T = 1
for _ in range(T):
	n = rri()
	ans = sol(n)
	print(ans)


(odd, even) = ([], [])
player1_turn = True
player1 = player2 = 0
pile_number = int(input())
for _ in range(pile_number):
	(n, *pile) = tuple(map(int, input().split()))
	if n % 2 == 0:
		even.append(pile)
	else:
		odd.append(pile)
for pile in even:
	n = len(pile)
	player1 += sum(pile[:n // 2])
	player2 += sum(pile[n // 2:])
for pile in sorted(odd, reverse=True, key=lambda x: x[len(x) // 2]):
	n = len(pile)
	(top, middle, bottom) = (pile[:n // 2], pile[n // 2], pile[n // 2 + 1:])
	player1 += sum(top)
	player2 += sum(bottom)
	if player1_turn:
		player1 += middle
		player1_turn = not player1_turn
	else:
		player2 += middle
		player1_turn = not player1_turn
print(player1, player2)


chef_sum = 0
ramsay_sum = 0
odds_mids = []
for n in range(int(input())):
	(c, *c_list) = map(int, input().split())
	if c % 2 == 1:
		odds_mids.append(c_list.pop(c // 2))
	for (index, element) in enumerate(c_list):
		if index < len(c_list) / 2:
			chef_sum += element
		else:
			ramsay_sum += element
odds_mids.sort(reverse=True)
for (index, element) in enumerate(odds_mids):
	if index % 2 == 0:
		chef_sum += element
	else:
		ramsay_sum += element
print(chef_sum, ramsay_sum)


n = int(input())
c = [list(map(int, input().split())) for _ in range(n)]
(a, b) = (0, 0)
d = []
for i in range(n):
	if len(c[i]) % 2:
		a += sum(c[i][1:c[i][0] // 2 + 1])
		b += sum(c[i][c[i][0] // 2 + 1:])
	else:
		a += sum(c[i][1:c[i][0] // 2 + 1])
		b += sum(c[i][c[i][0] // 2 + 2:])
		d.append(c[i][c[i][0] // 2 + 1])
d.sort(reverse=True)
print(a + sum(d[0::2]), b + sum(d[1::2]))


n = int(input())
S = [0] * n
(ciel, giro) = (0, 0)
odd = []
for i in range(n):
	L = list(map(int, input().split()))
	k = L[0]
	L = L[1:]
	S[i] = sum(L)
	if k % 2:
		odd.append(L[k // 2])
	ciel += sum(L[:k // 2])
	giro += sum(L[(k + 1) // 2:])
odd.sort(reverse=True)
for (i, x) in enumerate(odd):
	if i % 2:
		giro += x
	else:
		ciel += x
print(ciel, giro)


from sys import stdin
from collections import deque
import heapq
n = int(stdin.readline())
piles = []
for x in range(n):
	a = [int(x) for x in stdin.readline().split()][1:]
	piles.append(a)
cielTotal = 0
jiroTotal = 0
mids = []
for x in piles:
	cielTotal += sum(x[:len(x) // 2])
	jiroTotal += sum(x[len(x) // 2 + len(x) % 2:])
	if len(x) % 2 == 1:
		mids.append(x[len(x) // 2])
mids.sort(reverse=True)
turn = True
for x in mids:
	if turn:
		cielTotal += x
	else:
		jiroTotal += x
	turn = not turn
print(cielTotal, jiroTotal)


N = int(input())
one = two = 0
middles = []
for i in range(N):
	array = list(map(int, input().split()))[1:]
	size = len(array) - 1
	middle = size // 2
	for i in range(middle):
		one += array[i]
	for i in range(middle + 1, len(array)):
		two += array[i]
	if len(array) % 2 == 1:
		middles.append(array[middle])
	else:
		one += array[middle]
middles = sorted(middles)
ONE = True
for i in range(len(middles) - 1, -1, -1):
	if ONE:
		one += middles[i]
		ONE = False
	else:
		two += middles[i]
		ONE = True
print(one, two)