結果

問題 No.3268 As Seen in Toasters
ユーザー tnakao0123
提出日時 2025-09-16 17:57:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 42,064 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-16 17:57:07
合計ジャッジ時間 3,538 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:30:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:31:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3268.cc:  No.3268 As Seen in Toasters - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int INF = 1 << 30;

/* typedef */

using ll = long long;

/* global variables */

int as[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int pc = 0, mc = 0, minp = INF, maxp = -INF, maxm0 = -INF, maxm1 = -INF;
  ll msum = 0;
  for (int i = 0; i < n; i++) {
    if (as[i] >= 0) {
      pc++;
      minp = min(minp, as[i]);
      maxp = max(maxp, as[i]);
    }
    else {
      mc++;
      int ai = -as[i];
      if (maxm0 < ai) maxm1 = maxm0, maxm0 = ai;
      else if (maxm1 < ai) maxm1 = ai;
      msum += ai;
    }
  }

  ll sum = 0;
  if (mc == 0)
    sum = maxp - minp;
  else if (pc == 0)
    sum = msum * 2 - maxm0 - maxm1;
  else {
    sum = (ll)maxp + msum * 2 - maxm0;
    if (mc >= 2)
      sum = min(sum, (ll)maxp + msum * 2 - maxm0 - maxm1 + maxp);
  }
  
  printf("%lld\n", sum);
  
  return 0;
}
0