Advertisement
LEGEND2004

D - Nails

Sep 7th, 2023
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int N = 1e3 + 5;
  6. const int mod = 1e9 + 7;
  7. const int INF = 1e18;
  8. int dp[N][5];
  9.  
  10. signed main()
  11. {
  12.     int n;
  13.     cin >> n;
  14.     vector<int> a(n);
  15.     for(int i = 0; i < n; i++){
  16.         cin >> a[i];
  17.     }
  18.     sort(a.begin() , a.end());
  19.     dp[1][1] = a[1] - a[0];
  20.     dp[1][0] = INF;
  21.     for(int i = 2; i < n; i++){
  22.         dp[i][0] = dp[i - 1][1];
  23.         dp[i][1] = min(dp[i - 1][0] , dp[i - 1][1]) + a[i] - a[i - 1];
  24.     }
  25.     cout << dp[n - 1][1] << endl;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement