結果

問題 No.1906 Twinkle Town
ユーザー SSRS
提出日時 2022-04-15 22:15:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 201,544 KB
最終ジャッジ日時 2025-01-28 18:20:24
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 WA * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N;
    cin >> N;
    vector<int> A(N);
    for (int j = 0; j < N; j++){
      cin >> A[j];
    }
    sort(A.begin(), A.end());
    for (int j = N - 1; j >= 1; j--){
      A[j] -= A[j - 1];
    }
    if (N % 2 == 0){
      long long ans = 0;
      priority_queue<int> pq;
      for (int j = N - 1; j >= 0; j--){
        pq.push(A[j]);
        if (j % 2 == 1){
          ans += pq.top();
          pq.pop();
        }
      }
      cout << ans << endl;
    }
    if (N % 2 == 1){
      long long ans = A[0];
      priority_queue<int> pq;
      for (int j = 1; j < N; j++){
        pq.push(A[j]);
        if (j % 2 == 0){
          ans += pq.top();
          pq.pop();
        }
      }
      cout << ans << endl;
    }
  }
}
0