結果

問題 No.3268 As Seen in Toasters
ユーザー V_Melville
提出日時 2025-09-12 22:03:50
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,061 bytes
コンパイル時間 2,986 ms
コンパイル使用メモリ 284,556 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-12 23:39:03
合計ジャッジ時間 6,884 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

int main() {
    int n;
    cin >> n;
    
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    
    vector<ll> pos, neg;
    ll s = 0;
    for (ll x : a) {
        if (x >= 0) pos.push_back(x);
        else {
            neg.push_back(-x);
            s += -x;
        }
    }
    sort(neg.begin(), neg.end(), greater<>());

    ll ans = 1e18;
    if (!pos.size()) {
        ll mx1 = neg[0], mx2 = neg[1];
        ans = 2*s-(mx1+mx2);
    }
    else {
        ll pmx = *max_element(pos.begin(), pos.end());
        if (!neg.size()) {
            ll pmn = *min_element(pos.begin(), pos.end());
            ans = min(ans, pmx-pmn);
            cout << ans << '\n';
            return 0;
        }
        
        ll mx1 = neg[0];
        ans = min(ans, 2*s-mx1+pmx);
        if (neg.size() >= 2) {
            ll mx2 = neg[1];
            ans = min(ans, 2*s-(mx1+mx2) + 2*pmx);
        }
    }
    
    cout << ans << '\n';
    
    return 0;
}
0