Advertisement
aqibm

Untitled

May 17th, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <ext/pb_ds/assoc_container.hpp>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace __gnu_pbds;
  6. using namespace std;
  7.  
  8. // using long doubles saves you from corner cases but is very time consuming
  9. #define double long double
  10. #define int long long
  11. #define pb push_back
  12. #define pii pair<int,int>
  13. #define vi vector<int>
  14. #define vii vector<pii>
  15. #define mi map<int,int>
  16. #define mii map<pii,int>
  17. #define all(a) (a).begin(),(a).end()
  18. #define sz(x) (int)x.size()
  19. // just comment the line below in case of interactive problems
  20. #define endl "\n"
  21. #define repp(i,a,b) for(int i=a;i<b;i++)
  22. #define rep(i,a,b) for(int i=a;i<=b;i++)
  23. #define brep(i,a,b) for(int i=a;i>=b;i--)
  24. #define deb1(x) cout << #x << "=" << x << endl
  25. #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
  26. #define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
  27. #define trace(v) cout << #v << "=";for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;
  28. #define tracearr(a,l,r) cout << #a << "=";for(int iii=l;iii<=r;iii++)cout << a[iii] << " ";cout << endl;
  29. #define PI 3.1415926535897932384626
  30. #define F first
  31. #define S second
  32. #define clr(x,y) memset(x, y, sizeof(x))
  33. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  34. //vector<vector<int> > v( n , vector<int> (m, 0));
  35.  
  36.  
  37. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  38. // *s.find_by_order(1) - gives the 2nd samllest element in set
  39. // s.order_of_key(x) - gives the number of elements in the set which are strictly smaller than x
  40.  
  41.  
  42. const int N=2000 + 5;
  43. const int MOD=1e9 + 7;
  44.  
  45. int n;
  46. int dp[N][N];
  47. vi v;
  48.  
  49. int solve(int i,int j){
  50. if(i==-1 && j==n)
  51. return 0;
  52. int& ans = dp[i][j];
  53. if(ans!=-1)
  54. return ans;
  55. ans = 1e18;
  56. deb2(i,j);
  57. if(i==-1)
  58. ans = v[j] - v[0] + solve(i,j+1);
  59. else if(j==n)
  60. ans = v[n-1] - v[i] + solve(i-1,j);
  61. else{
  62. ans = min(v[j]-v[i+1] + solve(i,j+1),v[j-1] - v[i] + solve(i-1,j));
  63. }
  64. return ans;
  65. }
  66.  
  67. void test_case()
  68. {
  69. cin >> n;
  70. rep(i,1,n){
  71. int x;
  72. cin >> x;
  73. v.pb(x);
  74. }
  75. sort(all(v));
  76. if(n==1){
  77. cout << "0" << endl;
  78. return;
  79. }
  80. else if(n==2){
  81. cout << v[1] - v[0] << endl;
  82. return;
  83. }
  84. clr(dp,-1);
  85. int mid = n/2;
  86. int ans = solve(mid-1,mid+1);
  87. cout << ans << endl;
  88. }
  89.  
  90. int32_t main()
  91. {
  92. //uncomment the below 2 lines and for simplicity use t=1,when stress testing,
  93. //freopen("input1.txt", "r", stdin);
  94. //freopen("original_output.txt", "w", stdout);
  95. IOS;
  96. int T=1;
  97. //cin >> T;
  98. // int numOfSetBits = __builtin_popcountll(n);
  99. std::cout << std::fixed << std::setprecision(12);
  100. while(T--)
  101. {
  102. test_case();
  103. }
  104. }
  105.  
  106. /*
  107. * while writing bool comp function for sorting put 1 statement in the end which has no if comdition
  108. * in case of multiple test cases and N=1e5 don't intialize arrays as global except for graph vector-array
  109. * always check whether or not you are doing mod of a negative number
  110. * always use 1LL instead of 1
  111. */
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement