inFile = open('input.txt', 'r', encoding='utf8')
outFile = open('output.txt', 'w', encoding='utf8')
score = []
i = 0
nstr = 1
for line in inFile:
if nstr == 1:
k = int(line)
else:
line1 = line.split()
sc3 = int(line1[-1])
sc2 = int(line1[-2])
sc1 = int(line1[-3])
if sc1 >= 40 and sc2 >= 40 and sc3 >= 40:
score.append(sc1 + sc2 + sc3)
i += 1
nstr += 1
inFile.close()
if k >= i:
print(0, file=outFile)
score.sort(reverse=True)
if score[k-1] > score[k]:
print(score[k-1], file=outFile)
ans = ''
for ii in score[k-1::-1]:
if score[k-1] < ii and ans == '':
ans = ii
if ans == '':
ans = 1
print(ans, file=outFile)
outFile.close()
inFile = open('input.txt', 'r', encoding='utf8')
outFile = open('output.txt', 'w', encoding='utf8')
score = []
i = 0
nstr = 1
for line in inFile:
if nstr == 1:
k = int(line)
else:
line1 = line.split()
sc3 = int(line1[-1])
sc2 = int(line1[-2])
sc1 = int(line1[-3])
if sc1 >= 40 and sc2 >= 40 and sc3 >= 40:
score.append(sc1 + sc2 + sc3)
i += 1
nstr += 1
inFile.close()
if k >= i:
print(0, file=outFile)
else:
score.sort(reverse=True)
if score[k-1] > score[k]:
print(score[k-1], file=outFile)
else:
ans = ''
for ii in score[k-1::-1]:
if score[k-1] < ii and ans == '':
ans = ii
if ans == '':
ans = 1
print(ans, file=outFile)
outFile.close()