結果

問題 No.972 選び方のスコア
ユーザー nebukuro09nebukuro09
提出日時 2020-01-17 22:02:44
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 123,552 KB
実行使用メモリ 13,384 KB
最終ジャッジ日時 2024-06-22 04:13:53
合計ジャッジ時間 4,353 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    A.sort();

    auto B = new long[](N+1);
    foreach (i; 0..N) B[i+1] = B[i] + A[i];

    long sum(int l, int r) {
        if (r < l) return 0;
        return B[r+1] - B[l];
    }

    long calc(int l, int r, long v) {
        int hi = min(l, N - r - 1) + 1;
        int lo = 0;
        while (hi - lo > 1) {
            int mid = (hi + lo) / 2;
            if (A[l - mid] + A[N - mid] > v) {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        return sum(l-lo, l-1) + sum(N-lo, N-1) - v * lo;
    }

    long ans = 0;

    foreach (i; 1..N-1) {
        ans = max(ans, calc(i, i, A[i] * 2));
        if (i < N - 2) ans = max(ans, calc(i, i + 1, A[i] + A[i+1]));
    }

    ans.writeln;
}
0