結果
問題 |
No.972 選び方のスコア
|
ユーザー |
![]() |
提出日時 | 2020-01-18 17:33:24 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 139 ms / 2,000 ms |
コード長 | 1,186 bytes |
コンパイル時間 | 1,593 ms |
コンパイル使用メモリ | 170,432 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-27 21:40:29 |
合計ジャッジ時間 | 6,376 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int64_t a[n]; for(int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int64_t sum[n + 1] = {}; for(int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i - 1]; } int64_t ans = 0; for(int i = 2; i <= 2 * (n - 2); ++i) { if(i % 2 == 0) { int c = i / 2; int64_t ok = 1, ng = c + 1; while(abs(ok - ng) > 1) { int d = (ok + ng) / 2; if((a[c - d] + a[n - d]) - 2 * a[c] > 0) { ok = d; } else { ng = d; } } int64_t r = sum[n] - sum[n - ok]; int64_t l = sum[c] - sum[c - ok]; int64_t tmp = r + l - a[c] * 2 * ok; ans = max(ans, tmp); // fprintf(stderr, "%d: %ld\n", i, tmp); } else { int lc = (i - 1) / 2, rc = (i + 1) / 2; int64_t ok = 1, ng = lc + 1; while(abs(ok - ng) > 1) { int d = (ok + ng) / 2; if((a[lc - d] + a[n - d]) - (a[lc] + a[rc]) > 0) { ok = d; } else { ng = d; } } int64_t r = sum[n] - sum[n - ok]; int64_t l = sum[lc] - sum[lc - ok]; int64_t tmp = r + l - (a[lc] + a[rc]) * ok; ans = max(ans, tmp); // fprintf(stderr, "%d: %ld\n", i, tmp); } } cout << ans << '\n'; return 0; }