using System; using System.IO; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { int itemCount = int.Parse(Reader.ReadLine()); for (int i = 0; i < itemCount; i++) { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); this.NimList.Add(new Nimotsu(inpt[0], inpt[1])); } this.NimList = this.NimList.OrderBy(a => a.Size).ToList(); this.Target = long.Parse(Reader.ReadLine()); long tmp = this.NimList.Sum(a => a.Size); long min = GetMin(tmp, 1); long max = GetMax(tmp, 1); Console.WriteLine(min); Console.WriteLine(max==long.MaxValue?"inf":max.ToString()); } private long Target; private long GetMax(long max, long min) { if (max - min <= 1) { long tmpMax = GetAns(0, max); long tmpMin = GetAns(0, min); if (Target >= tmpMax) { return long.MaxValue; } if (Target < tmpMin) { return -1; } if (tmpMax == Target) { return max; } return min; } long mid = (max + min) / 2; long midVal = GetAns(0, mid); if (midVal <= Target) { return GetMax(max, mid); } else { return GetMax(mid, min); } } private long GetMin(long max, long min) { if(max-min<=1) { long tmpMax = GetAns(0, max); long tmpMin = GetAns(0, min); if(Target > tmpMax) { return long.MaxValue; } if(Target < tmpMin) { return -1; } if(tmpMin == Target) { return min; } return max; } long mid = (max + min) / 2; long midVal = GetAns(0, mid); if(midVal < Target) { return GetMin(max, mid); } else { return GetMin(mid, min); } } private Dictionary> dic = new Dictionary>(); private long GetAns(int idx, long remainSize) { if (remainSize == 0) { return 0; } if (remainSize < 0) { return -1; } if(idx >= this.NimList.Count) { return 0; } if(!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary()); } if(dic[idx].ContainsKey(remainSize)) { return dic[idx][remainSize]; } long ans = 0; if(this.NimList[idx].Size <= remainSize) { long tmp = GetAns(idx + 1, remainSize - this.NimList[idx].Size); if(tmp>=0) { ans = tmp + this.NimList[idx].Value; } tmp = GetAns(idx + 1, remainSize); if(tmp>=0) { ans = Math.Max(ans, tmp); } } dic[idx][remainSize] = ans; return ans; } private List NimList = new List(); private class Nimotsu { public long Size; public long Value; public Nimotsu(long v, long s) { this.Size = s; this.Value = v; } } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 3 5 3 9 8 3 2 8 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }