結果

問題 No.838 Noelちゃんと星々3
ユーザー kimiyuki
提出日時 2019-06-14 22:17:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 199,444 KB
最終ジャッジ日時 2025-01-07 04:33:14
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }

ll solve(int n, vector<ll> & a) {
    sort(ALL(a));
    vector<ll> dp(n + 1, LLONG_MAX / 10);
    dp[0] = 0;
    REP (i, n) {
        if (i >= 1) {
            chmin(dp[i + 1], dp[i - 1] + (a[i] - a[i - 1]));
        }
        if (i >= 2) {
            chmin(dp[i + 1], dp[i - 2] + (a[i] - a[i - 1]) + (a[i    ] - a[i - 2]));
            chmin(dp[i + 1], dp[i - 2] + (a[i] - a[i - 1]) + (a[i - 1] - a[i - 2]));
            chmin(dp[i + 1], dp[i - 2] + (a[i] - a[i - 2]) + (a[i - 1] - a[i - 2]));
        }
    }
    return dp[n];

}

int main() {
    int n; cin >> n;
    vector<ll> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    cout << solve(n, a) << endl;
    return 0;
}
0