using System; using System.Collections.Generic; using System.Linq; class Solution { static void Main() { var n = int.Parse(Console.ReadLine()); var v = new int[n]; var w = new int[n]; for (int i = 0; i < n; i++) { var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); v[i] = vals[0]; w[i] = vals[1]; } var maxV = int.Parse(Console.ReadLine()); // 重さ -> 価値 var napsack = new SortedDictionary() { { 0, 0 } }; for (int i = 0; i < n; i++) { var adds = new List>(); foreach (var nap in napsack) { var weight = nap.Key + w[i]; var value = nap.Value + v[i]; if (napsack.ContainsKey(weight)) { if (napsack[weight] < value) { adds.Add(new KeyValuePair(weight, value)); } } else { adds.Add(new KeyValuePair(weight, value)); } } foreach (var add in adds) { napsack[add.Key] = add.Value; } } // 容量0を排除 if (!napsack.ContainsKey(1)) { napsack[1] = napsack[0]; } napsack.Remove(0); var min = napsack.First(nap => nap.Value == maxV).Key; var maxKV = napsack.FirstOrDefault(nap => nap.Value > maxV && nap.Key > min); var max = (maxKV.Key == 0) ? -1 : maxKV.Key - 1; Console.WriteLine($"{min} {((max == -1) ? "inf" : max.ToString())}"); } }