結果

問題 No.972 選び方のスコア
ユーザー parukiparuki
提出日時 2020-01-24 20:35:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,625 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 171,156 KB
実行使用メモリ 6,388 KB
最終ジャッジ日時 2023-10-12 04:18:23
合計ジャッジ時間 4,970 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
6,156 KB
testcase_01 AC 74 ms
6,156 KB
testcase_02 AC 70 ms
6,144 KB
testcase_03 AC 36 ms
4,568 KB
testcase_04 AC 74 ms
6,280 KB
testcase_05 AC 73 ms
6,340 KB
testcase_06 AC 72 ms
6,204 KB
testcase_07 AC 54 ms
5,408 KB
testcase_08 AC 59 ms
6,280 KB
testcase_09 AC 57 ms
6,016 KB
testcase_10 AC 59 ms
6,292 KB
testcase_11 AC 60 ms
6,220 KB
testcase_12 AC 61 ms
6,296 KB
testcase_13 AC 61 ms
6,156 KB
testcase_14 AC 61 ms
6,164 KB
testcase_15 AC 62 ms
6,368 KB
testcase_16 AC 61 ms
6,152 KB
testcase_17 AC 62 ms
6,388 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 71 ms
6,160 KB
testcase_20 AC 70 ms
6,152 KB
testcase_21 AC 70 ms
6,220 KB
testcase_22 AC 69 ms
6,052 KB
testcase_23 AC 71 ms
6,288 KB
testcase_24 AC 71 ms
6,216 KB
testcase_25 AC 1 ms
4,352 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

void solve() {
    int N;
    cin >> N;
    vll A(N);
    rep(i, N) {
        cin >> A[i];
    }
    sort(all(A));
    vll sum(N + 1);
    rep(i, N) {
        sum[i + 1] = sum[i] + A[i];
    }
    ll ans = 0;
    rep(i, N) {
        ll median = A[i];
        int ok = 0, ng = min(N - i, i + 1);
        while (ng - ok > 1) {
            int x = (ok + ng) / 2;
            if (A[i - x] + A[N - x] - 2 * median >= 0)ok = x;
            else ng = x;
        }
        smax(ans, (sum[i] - sum[i - ok]) + (sum[N] - sum[N - ok]) - 2ll * median*ok);
    }

    rep(i, N - 1) {
        ll median2 = A[i] + A[i + 1];
        int ok = 0, ng = min(N - (i + 1), i + 1);
        while (ng - ok > 1) {
            int x = (ok + ng) / 2;
            if (A[i - x] + A[N - x] - median2 >= 0)ok = x;
            else ng = x;
        }
        smax(ans, (sum[i] - sum[i - ok]) + (sum[N] - sum[N - ok]) - median2 * ok);
    }

    cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0