Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 105;
- int n, x[N], dp[N];
- int main(){
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> x[i];
- }
- sort(x + 1, x + n + 1);
- dp[1] = 0;
- dp[2] = x[2] - x[1];
- dp[3] = x[3] - x[1];
- dp[4] = (x[2] - x[1]) + (x[4] - x[3]);
- for (int i = 5; i <= n; i++) {
- int first_value = x[i] - x[i - 1] + dp[i - 2];
- int second_value = x[i] - x[i - 2] + dp[i - 3];
- dp[i] = min(first_value, second_value);
- }
- cout << dp[n] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment