using System; using System.IO; using System.Linq; using System.Text; using System.Diagnostics; using System.Collections.Generic; using System.Text.RegularExpressions; using Enu = System.Linq.Enumerable; class Program { void Solve() { int N = reader.Int(); var A = reader.IntLine(); var divisors = new Dictionary>(); var multiples = new Dictionary>>(); var res = A.Select((a, i) => Tuple.Create(a, i)).ToArray(); for (int i = 0; i < N; i++) { var div = Divisors(A[i]); if (!divisors.ContainsKey(A[i])) divisors[A[i]] = div; foreach (int d in div) { if (!multiples.ContainsKey(d)) multiples[d] = new List>(); multiples[d].Add(new Tuple(A[i], i)); } } foreach (var list in multiples.Values) list.Sort(); for (int i = 0; i + 1 < A.Length; i++) { int bestLcm = int.MaxValue; Tuple best = new Tuple(int.MaxValue, -1); foreach (int d in divisors[res[i].Item1]) { multiples[d].Remove(res[i]); if (multiples[d].Count == 0) continue; var cand = multiples[d][0]; int lcm = LCM(res[i].Item1, cand.Item1); if (lcm < bestLcm || lcm == bestLcm && cand.Item1 < best.Item1) { bestLcm = lcm; best = cand; } } res[i + 1] = best; } Console.WriteLine(string.Join(" ", res.Select(t => t.Item1))); } static int GCD(int a, int b) { while (b != 0) { int r = a % b; a = b; b = r; } return a; } static int LCM(int a, int b) { return a / GCD(a, b) * b; } static void Swap(ref T a, ref T b) { T t = a; a = b; b = t; } static List Divisors(int n) { List divisors = new List(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { divisors.Add(i); if (i * i != n) { divisors.Add(n / i); } } } divisors.Sort(); return divisors; } Reader reader = new Reader(Console.In); static void Main() { new Program().Solve(); } } class Reader { private readonly TextReader reader; private readonly char[] separator = { ' ' }; private readonly StringSplitOptions removeOp = StringSplitOptions.RemoveEmptyEntries; private string[] A = new string[0]; private int i; public Reader(TextReader r) { reader = r; } public bool HasNext() { return Enqueue(); } public string String() { return Dequeue(); } public int Int() { return int.Parse(Dequeue()); } public long Long() { return long.Parse(Dequeue()); } public double Double() { return double.Parse(Dequeue()); } public int[] IntLine() { var s = Line(); return s == "" ? new int[0] : Array.ConvertAll(Split(s), int.Parse); } public int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); } public int[][] IntGrid(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); } public string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Line()).ToArray(); } public string Line() { return reader.ReadLine().Trim(); } private string[] Split(string s) { return s.Split(separator, removeOp); } private bool Enqueue() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return Enqueue(); A = Split(line); i = 0; return true; } private string Dequeue() { Enqueue(); return A[i++]; } }