using System; using System.Linq; using System.Collections.Generic; namespace yukicoder { class pg { static void Main(string[] args) { var line = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToList(); int N = line[0]; int M = line[1]; int K = line[2]; var nations = new List[N]; var takeGiftList = Enumerable.Repeat(1,N).ToArray(); int iniTotal = 0; for(int i = 0;i < N;i++) { var gifts = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).OrderBy(c => c).ToList(); nations[i] = gifts; iniTotal += gifts[0]; } if(iniTotal > K)Console.WriteLine(-1); int total = iniTotal; int oldTotal; while(true) { int minMoney = int.MaxValue; int minNation = -1; bool SerchFlag = false; for(int q = 0;q < N;q++) { if(takeGiftList[q] >= N)continue; int a = nations[q][takeGiftList[q]]; if(a < minMoney) { SerchFlag = true; minNation = q; minMoney = a; } } if(!SerchFlag) { Console.WriteLine(K - total); return; } oldTotal = total; int b = takeGiftList[minNation]; int c = nations[minNation][b] - nations[minNation][b - 1]; if(total + c > K) { Console.WriteLine(K - total); return; } total += c; } } } }