using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int itemCount = int.Parse(Reader.ReadLine()); char[] items = Reader.ReadLine().Split(' ').Select(a => a[0]).ToArray(); this.PrimeList = this.GetPrimeList(); int fromIdx = -1; int toIdx = -1; int ans = -1; Dictionary cnt = new Dictionary(); items.ToList().ForEach(a=>cnt.Add(a, false)); for (int i = 0; i < PrimeList.Length; i++) { string num = PrimeList[i].ToString(); if(num.All(a=>cnt.ContainsKey(a))) { if (fromIdx < 0) { fromIdx = i; } toIdx = i; num.ToList().ForEach(a => cnt[a] = true); } else { if (fromIdx >= 0 && cnt.All(a=>a.Value)) { int fromNum = 1; if (fromIdx > 0) { fromNum = PrimeList[fromIdx-1]+1; } int toNum = 5000000; if(toIdx < PrimeList.Length - 1) { toNum = PrimeList[toIdx + 1] - 1; } ans = Math.Max(ans, toNum - fromNum); } fromIdx = -1; toIdx = -1; cnt.Keys.ToList().ForEach(a => cnt[a] = false); } } if(toIdx >= 0) { int fromNum = 1; if (fromIdx>0) { fromNum = PrimeList[fromIdx - 1] + 1; } ans = Math.Max(ans, 5000000 - fromNum); } Console.WriteLine(ans); } private int[] GetPrimeList() { int num = 5000000; bool[] flags = new bool[num + 1]; List ret = new List(); for (int i = 2; i <= num; i++) { if (flags[i]) { continue; } ret.Add(i); int max = num / i; for (int j = 0; j <= max; j++) { flags[i*j] = true; } } return ret.ToArray(); } private int[] PrimeList; 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 = @" 5 1 2 3 4 5 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }