結果

問題 No.972 選び方のスコア
ユーザー 37zigen37zigen
提出日時 2020-04-02 03:49:43
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,067 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 6,106 ms
コンパイル使用メモリ 74,268 KB
実行使用メモリ 67,384 KB
最終ジャッジ日時 2023-09-09 23:16:14
合計ジャッジ時間 29,974 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,067 ms
66,868 KB
testcase_01 AC 1,015 ms
65,564 KB
testcase_02 AC 1,002 ms
66,424 KB
testcase_03 AC 708 ms
63,500 KB
testcase_04 AC 1,047 ms
67,364 KB
testcase_05 AC 1,005 ms
66,700 KB
testcase_06 AC 1,039 ms
66,668 KB
testcase_07 AC 871 ms
66,980 KB
testcase_08 AC 829 ms
66,752 KB
testcase_09 AC 845 ms
67,308 KB
testcase_10 AC 829 ms
64,596 KB
testcase_11 AC 892 ms
67,384 KB
testcase_12 AC 891 ms
66,852 KB
testcase_13 AC 937 ms
66,844 KB
testcase_14 AC 890 ms
66,684 KB
testcase_15 AC 890 ms
66,964 KB
testcase_16 AC 885 ms
67,168 KB
testcase_17 AC 873 ms
67,052 KB
testcase_18 AC 122 ms
56,080 KB
testcase_19 AC 1,013 ms
66,672 KB
testcase_20 AC 1,026 ms
66,936 KB
testcase_21 AC 1,032 ms
67,112 KB
testcase_22 AC 1,044 ms
67,292 KB
testcase_23 AC 959 ms
66,856 KB
testcase_24 AC 1,012 ms
66,824 KB
testcase_25 AC 121 ms
56,004 KB
testcase_26 AC 120 ms
55,932 KB
testcase_27 AC 121 ms
56,036 KB
testcase_28 AC 119 ms
55,928 KB
testcase_29 AC 120 ms
54,476 KB
testcase_30 AC 121 ms
56,384 KB
testcase_31 AC 121 ms
55,880 KB
testcase_32 AC 122 ms
55,988 KB
testcase_33 AC 140 ms
56,328 KB
testcase_34 AC 131 ms
56,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long sum(int l,int r,long[] sum) {
		return (r>0?sum[r-1]:0)-(l>0?sum[l-1]:0);
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		long[] A=new long[N];
		long[] sum=new long[N];
		for(int i=0;i<N;++i)A[i]=sc.nextLong();
		Arrays.sort(A);
		for(int i=0;i<N;++i)sum[i]=A[i]+(i>0?sum[i-1]:0);

		long ans=0;
		for(int i=0;i<N;++i) {
			int ok=0;
			int ng=1+Math.min(i,N-1-i);
			while(ng-ok>1) {
				int middle=(ok+ng)/2;
				if(A[i-middle]+A[N-middle]-2*A[i]>0) {
					ok=middle;
				}else {
					ng=middle;
				}
			}
			ans=Math.max(ans, sum(N-ok,N,sum)+sum(i-ok,i,sum)-A[i]*2*ok);
		}
		
		
		for(int i=0;i+1<N;++i) {
			int ok=0;
			int ng=1+Math.min(i,N-2-i);//[0,i)(i+1,N-1]
			while(ng-ok>1) {
				int middle=(ok+ng)/2;
				if(A[i-middle]+A[i+1+middle]-A[i]-A[i+1]>0) {
					ok=middle;
				}else {
					ng=middle;
				}
			}
			ans=Math.max(ans, sum(N-ok,N,sum)+sum(i-ok,i,sum)-A[i]*ok-A[i+1]*ok);
		}
		System.out.println(ans);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0