Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #include <bits/stdc++.h>
- using namespace __gnu_pbds;
- using namespace std;
- // using long doubles saves you from corner cases but is very time consuming
- #define double long double
- #define int long long
- #define pb push_back
- #define pii pair<int,int>
- #define vi vector<int>
- #define vii vector<pii>
- #define mi map<int,int>
- #define mii map<pii,int>
- #define all(a) (a).begin(),(a).end()
- #define sz(x) (int)x.size()
- // just comment the line below in case of interactive problems
- #define endl "\n"
- #define repp(i,a,b) for(int i=a;i<b;i++)
- #define rep(i,a,b) for(int i=a;i<=b;i++)
- #define brep(i,a,b) for(int i=a;i>=b;i--)
- #define deb1(x) cout << #x << "=" << x << endl
- #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
- #define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
- #define trace(v) cout << #v << "=";for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;
- #define tracearr(a,l,r) cout << #a << "=";for(int iii=l;iii<=r;iii++)cout << a[iii] << " ";cout << endl;
- #define PI 3.1415926535897932384626
- #define F first
- #define S second
- #define clr(x,y) memset(x, y, sizeof(x))
- #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- //vector<vector<int> > v( n , vector<int> (m, 0));
- typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- // *s.find_by_order(1) - gives the 2nd samllest element in set
- // s.order_of_key(x) - gives the number of elements in the set which are strictly smaller than x
- const int N=2000 + 5;
- const int MOD=1e9 + 7;
- int n;
- int dp[N][N];
- vi v;
- int solve(int i,int j){
- if(i==-1 && j==n)
- return 0;
- int& ans = dp[i][j];
- if(ans!=-1)
- return ans;
- ans = 1e18;
- deb2(i,j);
- if(i==-1)
- ans = v[j] - v[0] + solve(i,j+1);
- else if(j==n)
- ans = v[n-1] - v[i] + solve(i-1,j);
- else{
- ans = min(v[j]-v[i+1] + solve(i,j+1),v[j-1] - v[i] + solve(i-1,j));
- }
- return ans;
- }
- void test_case()
- {
- cin >> n;
- rep(i,1,n){
- int x;
- cin >> x;
- v.pb(x);
- }
- sort(all(v));
- if(n==1){
- cout << "0" << endl;
- return;
- }
- else if(n==2){
- cout << v[1] - v[0] << endl;
- return;
- }
- clr(dp,-1);
- int mid = n/2;
- int ans = solve(mid-1,mid+1);
- cout << ans << endl;
- }
- int32_t main()
- {
- //uncomment the below 2 lines and for simplicity use t=1,when stress testing,
- //freopen("input1.txt", "r", stdin);
- //freopen("original_output.txt", "w", stdout);
- IOS;
- int T=1;
- //cin >> T;
- // int numOfSetBits = __builtin_popcountll(n);
- std::cout << std::fixed << std::setprecision(12);
- while(T--)
- {
- test_case();
- }
- }
- /*
- * while writing bool comp function for sorting put 1 statement in the end which has no if comdition
- * in case of multiple test cases and N=1e5 don't intialize arrays as global except for graph vector-array
- * always check whether or not you are doing mod of a negative number
- * always use 1LL instead of 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement