結果

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