結果

問題 No.972 選び方のスコア
ユーザー tentententen
提出日時 2023-02-01 08:39:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 463 ms / 2,000 ms
コード長 2,278 bytes
コンパイル時間 2,603 ms
コンパイル使用メモリ 87,708 KB
実行使用メモリ 62,248 KB
最終ジャッジ日時 2023-09-13 22:00:12
合計ジャッジ時間 16,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 463 ms
62,220 KB
testcase_01 AC 420 ms
59,992 KB
testcase_02 AC 413 ms
62,024 KB
testcase_03 AC 291 ms
57,496 KB
testcase_04 AC 424 ms
60,132 KB
testcase_05 AC 421 ms
60,008 KB
testcase_06 AC 412 ms
60,456 KB
testcase_07 AC 363 ms
58,640 KB
testcase_08 AC 349 ms
61,836 KB
testcase_09 AC 341 ms
58,912 KB
testcase_10 AC 324 ms
61,300 KB
testcase_11 AC 336 ms
61,820 KB
testcase_12 AC 327 ms
61,924 KB
testcase_13 AC 334 ms
62,248 KB
testcase_14 AC 329 ms
61,700 KB
testcase_15 AC 344 ms
61,892 KB
testcase_16 AC 348 ms
61,916 KB
testcase_17 AC 345 ms
61,752 KB
testcase_18 AC 42 ms
49,620 KB
testcase_19 AC 409 ms
60,280 KB
testcase_20 AC 413 ms
59,920 KB
testcase_21 AC 417 ms
60,056 KB
testcase_22 AC 436 ms
60,600 KB
testcase_23 AC 425 ms
59,804 KB
testcase_24 AC 410 ms
59,208 KB
testcase_25 AC 44 ms
49,600 KB
testcase_26 AC 44 ms
49,528 KB
testcase_27 AC 45 ms
49,488 KB
testcase_28 AC 43 ms
49,492 KB
testcase_29 AC 41 ms
49,552 KB
testcase_30 AC 46 ms
49,672 KB
testcase_31 AC 44 ms
49,556 KB
testcase_32 AC 45 ms
49,608 KB
testcase_33 AC 50 ms
48,084 KB
testcase_34 AC 47 ms
49,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        Arrays.sort(values);
        long[] sums = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            sums[i] = sums[i - 1] + values[i - 1];
        }
        long ans = 0;
        for (int i = 1; i < n - 1; i++) {
            int left = 1;
            int right = i + 1;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (values[i - m] + values[n - m] > 2 * values[i]) {
                    left = m;
                } else {
                    right = m;
                }
            }
            ans = Math.max(ans, sums[i] - sums[i - left] + sums[n] - sums[n - left] - left * 2L * values[i]);
        }
        System.out.println(ans);
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0