結果

問題 No.1391 ±1 Abs Sum
ユーザー se1ka2se1ka2
提出日時 2021-02-12 21:54:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 934 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 65,244 KB
実行使用メモリ 5,104 KB
最終ジャッジ日時 2023-09-27 03:49:48
合計ジャッジ時間 3,899 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 79 ms
5,032 KB
testcase_12 AC 59 ms
4,600 KB
testcase_13 AC 70 ms
4,836 KB
testcase_14 AC 44 ms
4,500 KB
testcase_15 AC 47 ms
4,384 KB
testcase_16 AC 59 ms
4,568 KB
testcase_17 AC 53 ms
4,572 KB
testcase_18 AC 70 ms
4,808 KB
testcase_19 AC 51 ms
4,504 KB
testcase_20 AC 77 ms
4,908 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 78 ms
4,904 KB
testcase_32 AC 41 ms
4,380 KB
testcase_33 AC 46 ms
4,380 KB
testcase_34 AC 75 ms
4,840 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 86 ms
5,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    int n, k;
    cin >> n >> k;
    ll a[200005];
    for(int i = 0; i < n; i++) cin >> a[i];
    ll ans = 0;
    if(k * 2 < n){
        ll s = 0;
        for(int i = 0; i < k; i++) s += a[i] - a[0];
        for(int i = k; i < n; i++) s -= a[i] - a[0];
        ans = s;
        s = 0;
        for(int i = 0; i < n - k; i++) s -= a[n - 1] - a[i];
        for(int i = n - k; i < n; i++) s += a[n - 1] - a[i];
        ans = min(ans, s);
    }
    else{
        int x = (k * 2 - n) / 2;
        ll s = 0;
        for(int i = 0; i < k; i++) s += abs(a[i] - a[x]);
        for(int i = k; i < n; i++) s -= abs(a[i] - a[x]);
        ans = s;
        x = n - 1 - x;
        s = 0;
        for(int i = 0; i < n - k; i++) s -= abs(a[x] - a[i]);
        for(int i = n - k; i < n; i++) s += abs(a[x] - a[i]);
        ans = min(ans, s);
    }
    cout << ans << endl;
}
0