using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; this.ItemCount = int.Parse(Reader.ReadLine()); this.Items = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); decimal ans = GetAns(1, this.Items[0]); if (this.Items[0] > 0) { ans = Math.Max(ans, GetAns(1, this.Items[0] * -1)); } Console.WriteLine(ans); } private int[] Items; private int ItemCount = 0; private Dictionary> dic = new Dictionary>(); private decimal GetAns(int idx, decimal subTotal) { if (idx >= ItemCount) { return subTotal; } if (!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary()); } if (dic[idx].ContainsKey(subTotal)) { return dic[idx][subTotal]; } decimal ans = 0; int num = Items[idx]; ans = GetAns(idx + 1, subTotal + num); ans = Math.Max(ans, GetAns(idx + 1, subTotal - num)); ans = Math.Max(ans, GetAns(idx + 1, subTotal * num)); ans = Math.Max(ans, GetAns(idx + 1, subTotal / num)); dic[idx][subTotal] = ans; return ans; } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 16 6 9 1 2 1 2 1 2 1 4 1 4 1 8 1 2 "; 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(); } }