結果

問題 No.609 Noelちゃんと星々
ユーザー ei1333333
提出日時 2017-12-09 00:20:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 543 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 194,660 KB
最終ジャッジ日時 2025-01-05 05:04:31
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:12:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |   scanf("%d", &N);
      |   ~~~~~^~~~~~~~~~
main.cpp:14:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |     scanf("%d", &S[i]);
      |     ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 60;

int main()
{
  int N, S[1000000];

  scanf("%d", &N);
  for(int i = 0; i < N; i++) {
    scanf("%d", &S[i]);
  }
  sort(S, S + N);

  int64 ret = INF;
  int64 pre = 0, nxt = 0;
  for(int i = 0; i < N; i++) {
    nxt += S[i] - S[0];
  }
  for(int i = 0; i < N; i++) {
    if(i > 0) pre += 1LL * (S[i] - S[i - 1]) * i;
    ret = min(ret, pre + nxt);
    if(i + 1 < N) nxt -= 1LL * (S[i + 1] - S[i]) * (N - i - 1);
  }
  cout << ret << endl;
}
0