eolymp
bolt
Try our new interface for solving problems
Problems

Primes’ Problem

Primes’ Problem

\includegraphics{https://static.e-olymp.com/content/cb/cb3babb7b7055bbb8a503da1a00230597604dd65.jpg} One of the most difficulties of an instructor is question design for the final-term exam. Dr. Ghavamnia teaches “Fundamentals of Algorithms” at University of Isfahan (UI) and has designed a good practical algorithmic problem for his exam. He wants that students use all of their algorithmic skills to code this problem as best and efficient as they can. The problem is as follows: a ring is composed of N circles numbered from \textbf{1} to \textbf{N} as shown in diagram. You are given N integer numbers. Put these numbers into each circle separately with the condition that sum of numbers in three adjacent circles must be a prime. Each test case may have several solutions and your task is to find the one with minimum weighted sum. Weighted sum is defined as the sum of products of each number in circle by the sequence number of that circle. For instance, the weighted sum in above diagram is 36 (and also it is the minimum). \InputFile The first line of input contains a single integer, the number of test cases. Following, there is one line for each test case. Each test case begins with one integer, \textbf{N} (\textbf{3} ≤ \textbf{N} ≤ \textbf{15}), which is the number of circles. Next \textbf{N} integers are the given numbers to put in circles (all between \textbf{1} and \textbf{100}, inclusively). \OutputFile There should be one line for each test case in output. If there’s no way for building the ring, the word "\textbf{impossible}" should be outputted, or otherwise, a single integer which is the minimum weighted sum.
Time limit 4 seconds
Memory limit 64 MiB
Input example #1
1
15 11 13 15 17 19 21 22 23 25 27 29 31 33 35 37
Output example #1
impossible