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?
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*<sub class="lower-index">*i*</sub> (1 ≤ *s*<sub class="lower-index">*i*</sub> ≤ 100) — the number of cards in the *i*-th pile; then follow *s*<sub class="lower-index">*i*</sub> positive integers *c*<sub class="lower-index">1</sub>, *c*<sub class="lower-index">2</sub>, ..., *c*<sub class="lower-index">*k*</sub>, ..., *c*<sub class="lower-index">*s*<sub class="lower-index">*i*</sub></sub> (1 ≤ *c*<sub class="lower-index">*k*</sub> ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.


def main(args):

	nPiles = int(input())
	piles = []
	for i in range (nPiles):
		line = list(map(int, input().split(' ')))
		piles.append(line[1:])

	ciel = 0
	jiro = 0
	
	turn = 1

	while piles:
		if turn%2 != 0:
			ciel += piles[0][-1]
			del piles[0][-1]
			turn += 1
			
			if not piles[0]:
				del piles[0]
		
		elif turn%2 == 0:
			jiro += piles[0][0]
			del piles[0][0]
			turn += 1
			
			if not piles[0]:
				del piles[0]

	print(ciel, jiro)

	return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))



		 			  		    	  	   	  	 	 	