結果

問題 No.3268 As Seen in Toasters
ユーザー Today03
提出日時 2025-09-12 21:58:26
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,680 bytes
コンパイル時間 3,214 ms
コンパイル使用メモリ 287,024 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-12 23:38:34
合計ジャッジ時間 5,821 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)

int lob(const auto& v, auto x) { return lower_bound(ALL(v),x)-v.begin(); }
bool chmax(auto& a, auto b) { return a<b ? a=b, true : false; }
bool chmin(auto& a, auto b) { return a>b ? a=b, true : false; }
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;

#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif


//----------------------------------------------------------

void solve() {
    ll N; cin>>N;
    VL A(N); REP(i,N) cin>>A[i];

    sort(ALL(A)); reverse(ALL(A));
    debug(A);

    if(A[0]<0) {
        ll a=A[N-1], b=A[N-2];
        cout<<-2*reduce(ALL(A))+a+b<<endl;
        return;
    }

    auto f=[&](auto x) {
        ll ret=0;
        REP(i,N-1) {
            ll a=x[i], b=x[i+1];
            if(a<0 && b<0) ret+=-a-b;
            else ret+=abs(a-b);
        }
        return ret;
    };

    ll ans=f(A);
    if(A[N-2]<0) {
        ll a=A[N-2];
        A.erase(A.begin()+N-2);
        A.insert(A.begin(),a);
        chmin(ans,f(A));
        debug(A);
    }

    cout<<ans<<endl;

#ifdef TDY
    reverse(ALL(A));
    ll ansv=INFL;
    VL ansvec;
    do {
        if(chmin(ansv,f(A))) ansvec=A;
    } while(next_permutation(ALL(A)));
    debug(ansv,ansvec);
#endif
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //cout<<fixed<<setprecision(15);
    int T=1; //cin>>T;
    while(T--) solve();
}
0