using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int toyCount = int.Parse(Reader.ReadLine()); this.ToyList = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); int boxCount = int.Parse(Reader.ReadLine()); this.BoxList = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); BoxCountResult ans = this.CountBox(0, 0); Console.WriteLine(ans.Count); } private int[] ToyList; private int[] BoxList; public struct BoxCountResult { public int target; public int Count; } public struct ToyCountResult { public int target; public int Remain; } private Dictionary> BoxDic = new Dictionary>(); private BoxCountResult CountBox(int idx, int target){ if(!this.BoxDic.ContainsKey(idx)) { this.BoxDic.Add(idx, new Dictionary()); } if(this.BoxDic[idx].ContainsKey(target)) { return this.BoxDic[idx][target]; } BoxCountResult ans; ans.target = target; ans.Count = -1; int allUsed = (1< tr = this.CountOmocha(0, target, this.BoxList[idx]); for(int i=0; i= 0) { tmp.Count+=1; if(ret1.Count < 0 || ret1.Count > tmp.Count) { ret1 = tmp; } } } else if(a.target == allUsed) { ret1.Count = 1; ret1.target = a.target; break; } } } BoxCountResult ret2 = this.CountBox(idx+1, target); if(ret1.Count >= 0 && ret2.Count >= 0) { if(ret1.Count < ret2.Count) { ans = ret1; } else { ans = ret2; } } else if(ret1.Count >= 0) { ans = ret1; } else if(ret2.Count >=0 ) { ans = ret2; } this.BoxDic[idx].Add(target, ans); return ans; } Dictionary> LargeistDic = new Dictionary>(); private Dictionary>>> ToyDic = new Dictionary>>>(); private List CountOmocha(int idx, int target, int remain) { if(!this.ToyDic.ContainsKey(idx)) { this.ToyDic.Add(idx, new Dictionary>>()); } if(!this.ToyDic[idx].ContainsKey(target)) { this.ToyDic[idx].Add(target, new Dictionary>()); } if(this.ToyDic[idx][target].ContainsKey(remain)) { return this.ToyDic[idx][target][remain]; } List ans = new List(); ToyCountResult tmp; tmp.target = target; tmp.Remain = remain; ans.Add(tmp); int allUsed = (1< ret1 = new List(); if(this.ToyList[idx] <= remain && (target & (1< ret2 = this.CountOmocha(idx+1, target, remain); ret1.Union(ret2).Distinct().ToList().ForEach(a=>{ if(!ans.Contains(a)) { ans.Add(a); } }); this.ToyDic[idx][target].Add(remain, ans); return ans; } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 2 2 3 1 5 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }