結果

問題 No.972 選び方のスコア
ユーザー ks2mks2m
提出日時 2020-01-17 22:42:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 584 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 2,611 ms
コンパイル使用メモリ 74,964 KB
実行使用メモリ 74,668 KB
最終ジャッジ日時 2023-09-08 05:20:46
合計ジャッジ時間 17,190 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 582 ms
73,292 KB
testcase_01 AC 583 ms
72,800 KB
testcase_02 AC 510 ms
72,296 KB
testcase_03 AC 312 ms
64,248 KB
testcase_04 AC 568 ms
74,404 KB
testcase_05 AC 514 ms
73,844 KB
testcase_06 AC 548 ms
74,668 KB
testcase_07 AC 558 ms
67,188 KB
testcase_08 AC 367 ms
72,224 KB
testcase_09 AC 372 ms
72,468 KB
testcase_10 AC 393 ms
71,880 KB
testcase_11 AC 452 ms
73,568 KB
testcase_12 AC 418 ms
74,088 KB
testcase_13 AC 405 ms
74,112 KB
testcase_14 AC 419 ms
74,132 KB
testcase_15 AC 400 ms
73,756 KB
testcase_16 AC 403 ms
73,540 KB
testcase_17 AC 416 ms
73,540 KB
testcase_18 AC 44 ms
49,704 KB
testcase_19 AC 488 ms
72,708 KB
testcase_20 AC 490 ms
72,864 KB
testcase_21 AC 584 ms
70,684 KB
testcase_22 AC 487 ms
73,596 KB
testcase_23 AC 475 ms
73,140 KB
testcase_24 AC 517 ms
73,040 KB
testcase_25 AC 43 ms
50,172 KB
testcase_26 AC 46 ms
49,856 KB
testcase_27 AC 49 ms
49,828 KB
testcase_28 AC 43 ms
49,300 KB
testcase_29 AC 43 ms
49,312 KB
testcase_30 AC 46 ms
49,864 KB
testcase_31 AC 47 ms
49,692 KB
testcase_32 AC 48 ms
50,028 KB
testcase_33 AC 50 ms
49,808 KB
testcase_34 AC 50 ms
49,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		if (n <= 2) {
			System.out.println(0);
			return;
		}

		Arrays.parallelSort(a);
		long[] b = new long[n + 1];
		for (int i = 0; i < n; i++) {
			b[i + 1] = b[i] + a[i];
		}

		long ans = 0;
		for (int i = 1; i < n - 1; i++) {
			int ok = 1;
			int ng = Math.min(i + 1, n - i);
			while (Math.abs(ng - ok) > 1) {
				int mid = (ng + ok) / 2;
				if (calc(a, i, mid)) {
					ok = mid;
				} else {
					ng = mid;
				}
			}
			long val = b[i] - b[i - ok] + b[n] - b[n - ok];
			val -= a[i] * ok * 2;
			ans = Math.max(ans, val);
		}
		System.out.println(ans);
	}

	static boolean calc(long[] a, int i, int mid) {
		int n = a.length;
		long m = a[i] - a[i - mid];
		long p = a[n - mid] - a[i];
		return p >= m;
	}
}
0