#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define _USE_MATH_DEFINES #include #include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair pic; #define bit(x,v) ((ll)x << v) const ll INF = 1000000007; const int MAX = 210000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int pr[100010]; void uini(int n) { for (size_t i = 0; i <= n; i++) { pr[i] = i; } } int parent(int x) { if (x == pr[x]) return x; return pr[x] = parent(pr[x]); } int same(int x, int y) { return parent(x) == parent(y); } bool unit(int x, int y) { int px = parent(x); int py = parent(y); if (px == py) return false; if (px < py) { pr[py] = px; } else { pr[px] = py; } return true; } ll bit[200010]; int max_n = 200000; int pm = 0; void add(int x) { while (max_n >= x) { bit[x]++; x += x & -x; } } void sub(int x) { while (max_n >= x) { bit[x]--; x += x & -x; } } ll merge(ll* a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector L(n1 + 1); vector R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = INF; R[n2] = INF; ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll merge2(pair* a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector> L(n1 + 1); vector> R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = make_pair(INF, ' '); R[n2] = make_pair(INF, ' '); ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i].first <= R[j].first) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll mergeSort2(pair* a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort2(a, left, mid); res += mergeSort2(a, mid, right); res += merge2(a, left, mid, right); } return res; } ll mergeSort(ll* a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort(a, left, mid); res += mergeSort(a, mid, right); res += merge(a, left, mid, right); } return res; } int partition(pair* a, int p, int r) { pair x = a[r]; int i = p - 1; for (size_t j = p; j < r; j++) { if (a[j].first <= x.first) { i++; swap(a[i], a[j]); } } swap(a[i + 1], a[r]); return i + 1; } void quick(pair* a, int p, int r) { if (p < r) { int q = partition(a, p, r); quick(a, p, q - 1); quick(a, q + 1, r); } } ll n; vector es[100010]; int ci = 0; ll P[1000010]; struct Node { int key; int priority; Node* parent, *left, *right; Node(int key, int priority); Node() {} }; Node NIL; Node::Node(int key, int priority) : key(key), priority(priority) { left = &NIL; right = &NIL; } Node* root = new Node(); void cenrec(Node* k) { if (k->key == NIL.key) return; cenrec(k->left); cout << " " << k->key; cenrec(k->right); } void fastrec(Node* k) { if (k->key == NIL.key) return; cout << " " << k->key; fastrec(k->left); fastrec(k->right); } void insert(Node* v) { Node* y = &NIL; Node* x = root; while (x->key != NIL.key) { y = x; if (v->key < x->key) { x = x->left; } else { x = x->right; } } v->parent = y; if (y->key == NIL.key) { root = v; } else if (v->key < y->key) { y->left = v; } else { y->right = v; } } Node* find(Node* k, ll v) { if (k->key == NIL.key) return &NIL; if (k->key == v) return k; if (v < k->key) return find(k->left, v); return find(k->right, v); } void delp12(Node* x) { if (x->key == NIL.key) return; Node* l = x->left; Node* r = x->right; Node* pr = x->parent; if (l->key == NIL.key && r->key == NIL.key) { if (pr->left == x) { pr->left = &NIL; } else pr->right = &NIL; } else if (l->key != NIL.key) { if (pr->left == x) { pr->left = l; } else pr->right = l; l->parent = pr; } else if (r->key != NIL.key) { if (pr->left == x) { pr->left = r; } else pr->right = r; r->parent = pr; } } Node* get_next(Node* k) { if (k->key == NIL.key) return &NIL; Node* res = get_next(k->left); if (res->key != NIL.key) return res; return k; } void del(Node* x) { if (x->key == NIL.key) return; Node* l = x->left; Node* r = x->right; Node* pr = x->parent; if (l->key != NIL.key && r->key != NIL.key) { Node* nex = get_next(r); x->key = nex->key; delp12(nex); } else { delp12(x); } } Node* rightRotate(Node* t) { Node* s = t->left; t->left = s->right; s->right = t; return s; } Node* leftRotate(Node* t) { Node* s = t->right; t->right = s->left; s->left = t; return s; } Node* _insert(Node* t, int key, int priority) { if (t->key == NIL.key) { return new Node(key, priority); } if (key == t->key) { return t; } if (key < t->key) { t->left = _insert(t->left, key, priority); if (t->priority < t->left->priority) { t = rightRotate(t); } } else { t->right = _insert(t->right, key, priority); if (t->priority < t->right->priority) { t = leftRotate(t); } } return t; } Node* delete1(Node* t, int key); Node* _delete(Node* t, int key) { if (t->left->key == NIL.key && t->right->key == NIL.key) { return &NIL; } else if (t->left->key == NIL.key) { t = leftRotate(t); } else if (t->right->key == NIL.key) { t = rightRotate(t); } else { if (t->left->priority > t->right->priority) { t = rightRotate(t); } else t = leftRotate(t); } return delete1(t, key); } Node* delete1(Node* t, int key) { if (t->key == NIL.key) { return &NIL; } if (key < t->key) { t->left = delete1(t->left, key); } else if (key > t->key) { t->right = delete1(t->right, key); } else return _delete(t, key); return t; } int H; int left(int i) { return i * 2 + 1; } int right(int i) { return i * 2 + 2; } ll heap[2000010]; void maxHeapify(int i) { int l = left(i); int r = right(i); int largest = 0; if (l < H && heap[l] > heap[i]) largest = l; else largest = i; if (r < H && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(largest); } } int pare(int i) { return (i - 1) / 2; } void raise(int i) { int l = pare(i); if (l < 0) return; if (heap[l] < heap[i]) { swap(heap[i], heap[l]); raise(l); } } void minHeapify(int i) { int l = left(i); int r = right(i); int minimam = 0; if (l < H && heap[l] < heap[i]) minimam = l; else minimam = i; if (r < H && heap[r] < heap[minimam]) minimam = r; if (minimam != i) { swap(heap[i], heap[minimam]); minHeapify(minimam); } } void buildMaxHeap() { for (int i = H / 2; i >= 0; i--) { maxHeapify(i); } } ll memo[1 << 17]; ll tb[20][20]; ll rec(int x) { if (~memo[x]) return memo[x]; ll p = 1; bool one = false; int dig = 0; int dc = 0; vector vi; while (p <= x) { if (p & x) { dc++; vi.push_back(dig); }p <<= 1; dig++; } if (one) return memo[x] = 0; memo[x] = 0; ll v = 0; for (size_t i = 0; i < vi.size(); i++) { for (size_t j = i + 1; j < vi.size(); j++) { memo[x] += tb[vi[i]][vi[j]]; } } for (int i = (x - 1) & x; i > 0; i = (i - 1) & x) { int t = i & x; if (t == x) continue; int rev = x ^ t; ll val = rec(t) + rec(rev); memo[x] = max(memo[x], val); } return memo[x]; } void solv() { int n; cin >> n; ll a[100010]; for (size_t i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); if (a[0] == a[n - 1]) { cout << 1 << endl; return; } if (n == 2) { cout << a[1] - a[0] << endl; return; } ll dp[100010]; fill(dp, dp + n, INF*INF); dp[1] = a[1] - a[0]; dp[2] = a[2] - a[0]; for (int i = 3; i < n; i++) { for (int j = i - 3; j < i; j++) { ll v = 0; int m = (i - j) / 2 + j; for (int k = j + 1; k <= i; k++) { v += abs(a[m] - a[k]); } dp[i] = min(dp[i], dp[j] + v); } } cout << dp[n-1] << endl; } int main() { //COMinit(); solv(); return 0; }