Advertisement
ssrtatarin

Untitled

Jul 8th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. #define ll long long
  6. #define int long long
  7. const int inf = 1e9;
  8.  
  9. ll M2 = 1e9 + 9, M1 = 1e9 + 7;
  10. //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  11.  
  12. void solve() {
  13.     int n;
  14.     cin >> n;
  15.     vector<int> a(n);
  16.     for (auto &x : a) {
  17.         cin >> x;
  18.     }
  19.     sort(a.begin(), a.end());
  20.     vector<int> dp(n);
  21.     dp[1] = a[1] - a[0];
  22.     if (n == 2) {
  23.         cout << dp[1];
  24.         return;
  25.     }
  26.     dp[2] = dp[1] + a[2] - a[1];
  27.     for (int i = 3; i < n; ++i) {
  28.         dp[i] = min(dp[i - 1], dp[i - 2]) + a[i] - a[i - 1];
  29.     }
  30.     cout << dp[n - 1];
  31.  
  32. }
  33.  
  34. signed main() {
  35.  
  36.  
  37.     ios_base::sync_with_stdio(false);
  38.     cin.tie(nullptr);
  39.     ll thrush = 1;
  40.     //cin >> thrush;
  41.     while (thrush--) { solve();}
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement