SHOW:
|
|
- or go back to the newest paste.
| 1 | #ifndef OLD | |
| 2 | #include <map> | |
| 3 | #include <set> | |
| 4 | #include <new> | |
| 5 | #include <stack> | |
| 6 | #include <deque> | |
| 7 | #include <queue> | |
| 8 | #include <cmath> | |
| 9 | #include <ctime> | |
| 10 | #include <locale> | |
| 11 | #include <memory> | |
| 12 | #include <vector> | |
| 13 | #include <cstdio> | |
| 14 | #include <limits> | |
| 15 | #include <cassert> | |
| 16 | #include <ctype.h> | |
| 17 | #include <climits> | |
| 18 | #include <cstdarg> | |
| 19 | #include <utility> | |
| 20 | #include <cstring> | |
| 21 | #include <iomanip> | |
| 22 | #include <numeric> | |
| 23 | #include <cassert> | |
| 24 | #include <sstream> | |
| 25 | #include <fstream> | |
| 26 | #include <complex> | |
| 27 | #include <iterator> | |
| 28 | #include <iostream> | |
| 29 | #include <typeinfo> | |
| 30 | #include <valarray> | |
| 31 | #include <algorithm> | |
| 32 | #include <functional> | |
| 33 | #else | |
| 34 | #include <bits/stdc++.h> | |
| 35 | #endif | |
| 36 | ||
| 37 | #define f first | |
| 38 | #define s second | |
| 39 | #define re return | |
| 40 | #define mp make_pair | |
| 41 | #define pb push_back | |
| 42 | #define sz(s) (int) s.size () | |
| 43 | #define all(s) s.begin (), s.end () | |
| 44 | ||
| 45 | #define name "" | |
| 46 | ||
| 47 | using namespace std; | |
| 48 | ||
| 49 | typedef long long ll; | |
| 50 | typedef long double ld; | |
| 51 | typedef vector<int> vi; | |
| 52 | typedef vector<vi> vvi; | |
| 53 | typedef pair<int, int> pi; | |
| 54 | ||
| 55 | - | const int N = 100009; |
| 55 | + | const int N = 2009; |
| 56 | const int inf = (int) 2e9; | |
| 57 | const int mod = (int) 1e9 + 7; | |
| 58 | const double eps = 1e-10; | |
| 59 | ||
| 60 | int a[N]; | |
| 61 | int p[N]; | |
| 62 | int pr[N]; | |
| 63 | int dp[N][N]; | |
| 64 | ||
| 65 | int main () {
| |
| 66 | int n; | |
| 67 | scanf ("%d", &n);
| |
| 68 | for (int i = 0; i < n; i++) {
| |
| 69 | scanf ("%d", a + i);
| |
| 70 | } | |
| 71 | int m = 0; | |
| 72 | for (int i = 2; i <= 2000; i++) {
| |
| 73 | if (!pr[i]) {
| |
| 74 | p[m++] = i; | |
| 75 | for (int j = i * i; j <= 2000; j += i) {
| |
| 76 | pr[j] = 1; | |
| 77 | } | |
| 78 | } | |
| 79 | } | |
| 80 | for (int i = 0; i < n; i++) {
| |
| 81 | for (int j = 0; j < m; j++) {
| |
| 82 | dp[i][j] = inf; | |
| 83 | } | |
| 84 | } | |
| 85 | sort (a, a + n); | |
| 86 | int ans = inf; | |
| 87 | for (int i = 0; i < m; i++) {
| |
| 88 | dp[0][i] = abs (a[0] - p[i]); | |
| 89 | if (n == 1) {
| |
| 90 | ans = min (ans, dp[0][i]); | |
| 91 | } | |
| 92 | } | |
| 93 | for (int i = 1; i < n; i++) {
| |
| 94 | for (int j = i - 1; j < m; j++) {
| |
| 95 | for (int k = j + 1; k < m; k++) {
| |
| 96 | dp[i][k] = min (dp[i][k], dp[i - 1][j] + abs (a[i] - p[k])); | |
| 97 | if (i == n - 1) {
| |
| 98 | ans = min (ans, dp[i][k]); | |
| 99 | } | |
| 100 | } | |
| 101 | } | |
| 102 | } | |
| 103 | printf ("%d\n", ans);
| |
| 104 | return 0; | |
| 105 | } |