Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #define ll long long
- #define int long long
- const int inf = 1e9;
- ll M2 = 1e9 + 9, M1 = 1e9 + 7;
- //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
- void solve() {
- int n;
- cin >> n;
- vector<int> a(n);
- for (auto &x : a) {
- cin >> x;
- }
- sort(a.begin(), a.end());
- vector<int> dp(n);
- dp[1] = a[1] - a[0];
- if (n == 2) {
- cout << dp[1];
- return;
- }
- dp[2] = dp[1] + a[2] - a[1];
- for (int i = 3; i < n; ++i) {
- dp[i] = min(dp[i - 1], dp[i - 2]) + a[i] - a[i - 1];
- }
- cout << dp[n - 1];
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- ll thrush = 1;
- //cin >> thrush;
- while (thrush--) { solve();}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement