結果

問題 No.3268 As Seen in Toasters
ユーザー HoyHoyCharhang
提出日時 2025-09-12 21:51:25
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,769 bytes
コンパイル時間 3,081 ms
コンパイル使用メモリ 281,780 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-12 23:37:58
合計ジャッジ時間 6,635 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using P = pair<int,int>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  int cnt = 0;
  rep(i,0,n) {
    cin >> a[i];
    cnt += (a[i] < 0);
  }
  sort(all(a));
  if (cnt <= 1) {
    ll ans = 0;
    rep(i,0,n-1) ans += abs(a[i]-a[i+1]);
    cout << ans << endl;
    return 0;
  }
  if (cnt == n) {
    ll ans = -a[0]-a[1];
    rep(i,2,n) ans -= 2*a[i];
    cout << ans << endl;
    return 0;
  }
  ll ans1 = 2LL*a[n-1]-a[0]-a[1];
  rep(i,2,cnt) ans1 -= 2*a[i];
  ll ans2 = a[n-1]-a[0];
  rep(i,1,cnt) ans2 -= 2*a[i];
  cout << min(ans1, ans2) << endl;
  return 0;
  // vector<int> ord(n);
  // iota(all(ord), 0);
  // function<int(int,int)> f = [](int x, int y) {
  //   if (x < 0 && y < 0) return -x-y;
  //   return abs(x-y);
  // };
  // int ans = 1000000000;
  // do {
  //   int v = 0;
  //   rep(i,0,n-1) {
  //     v += f(a[ord[i]], a[ord[i+1]]);
  //   }
  //   ans = min(ans, v);
  //   if (v == 4) {
  //     rep(i,0,n) {
  //       cout << a[ord[i]] << " ";
  //     }
  //     cout << endl;
  //   }
  // } while(next_permutation(all(ord)));
  // cout << ans << endl;
  return 0;
}
0