using System; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { Reader.IsDebug = false; int itemCount = int.Parse(Reader.ReadLine()); this.items = Reader.ReadLine().Split(' ').Select(a=>long.Parse(a)).OrderByDescending(a=>a).ToArray(); this.range = Reader.ReadLine().Split(' ').Select(a=>Math.Abs(long.Parse(a))).Max(); Dictionary dic = new Dictionary(); long pos = range; dic.Add(pos, 0); Queue que = new Queue(); que.Enqueue(pos); while(que.Count > 0) { pos = que.Dequeue(); if(pos == 0) { break; } if(pos % items[0] == 0) { if((!dic.ContainsKey(0)) || dic[0] > pos / items[0]) { dic[0] = pos / items[0]; continue; } } long step = dic[pos]; items.ToList().ForEach(a=>{ long[] tmp = new long[]{a, a*-1}; foreach(long item in tmp) { long nextPos = pos + item; if(pos >= 0 && nextPos >= 0 && nextPos > pos) { continue; } if(pos <= 0 && nextPos <= 0 && nextPos < pos) { continue; } if(dic.ContainsKey(nextPos) && dic[nextPos] <= step + 1) { continue; } dic[nextPos] = step + 1; que.Enqueue(nextPos); } }); } if(dic.ContainsKey(0)) { Console.WriteLine(dic[0]); } else { Console.WriteLine(-1); } } private long[] items; private long range; public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 1 1 45 14 "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }