結果

問題 No.972 選び方のスコア
ユーザー 東前頭十一枚目東前頭十一枚目
提出日時 2020-01-18 17:33:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,186 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 168,856 KB
実行使用メモリ 6,520 KB
最終ジャッジ日時 2023-09-10 06:09:49
合計ジャッジ時間 7,087 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
6,500 KB
testcase_01 AC 127 ms
6,500 KB
testcase_02 AC 122 ms
6,300 KB
testcase_03 AC 60 ms
5,160 KB
testcase_04 AC 120 ms
6,520 KB
testcase_05 AC 122 ms
6,508 KB
testcase_06 AC 120 ms
6,468 KB
testcase_07 AC 96 ms
5,732 KB
testcase_08 AC 112 ms
6,464 KB
testcase_09 AC 107 ms
6,344 KB
testcase_10 AC 109 ms
6,464 KB
testcase_11 AC 115 ms
6,452 KB
testcase_12 AC 114 ms
6,464 KB
testcase_13 AC 116 ms
6,464 KB
testcase_14 AC 115 ms
6,456 KB
testcase_15 AC 114 ms
6,488 KB
testcase_16 AC 117 ms
6,504 KB
testcase_17 AC 116 ms
6,464 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 115 ms
6,492 KB
testcase_20 AC 116 ms
6,504 KB
testcase_21 AC 116 ms
6,472 KB
testcase_22 AC 115 ms
6,352 KB
testcase_23 AC 116 ms
6,508 KB
testcase_24 AC 115 ms
6,520 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0