using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; this.Requests = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).OrderByDescending(a => a).ToArray(); this.SheetCount = int.Parse(Reader.ReadLine()); this.Sheets = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).OrderBy(a => a).ToArray(); long ans = this.GetAns(0, false, false, false); Console.WriteLine(ans); } private Dictionary> dic = new Dictionary>(); private long GetAns(int idx, bool fixA, bool fixB, bool fixC) { int fixKey = 0; if (fixA) { fixKey += 1; } if (fixB) { fixKey += 2; } if (fixC) { fixKey += 4; } if (!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary()); } if (dic[idx].ContainsKey(fixKey)) { return dic[idx][fixKey]; } if (idx == SheetCount) { if (fixA && fixB && fixC) { return 1; } else { return 0; } } long ans = 0; if (fixA && fixB && fixC) { ans += this.GetAns(idx + 1, fixA, fixB, fixC) * 2; } else { ans += this.GetAns(idx + 1, fixA, fixB, fixC); bool newFixA = fixA; bool newFixB = fixB; bool newFixC = fixC; bool isfixed = false; if (!newFixC) { if (Sheets[idx] >= Requests[2]) { newFixC = true; isfixed = true; } } if ((!newFixB) && (!isfixed)) { if (Sheets[idx] >= Requests[1]) { newFixB = true; isfixed = true; } } if ((!newFixA) && (!isfixed)) { if (Sheets[idx] >= Requests[0]) { newFixA = true; isfixed = true; } } ans += this.GetAns(idx + 1, newFixA, newFixB, newFixC); } dic[idx].Add(fixKey, ans); return ans; } private int[] Requests; private int SheetCount; private int[] Sheets; public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 15 25 35 10 10 20 30 40 50 60 70 80 90 100 "; 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(); } }