結果

問題 No.1391 ±1 Abs Sum
ユーザー se1ka2
提出日時 2021-02-12 22:01:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 555 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 67,200 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-19 21:29:03
合計ジャッジ時間 3,417 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const ll INF = 100000000000000000;

int main()
{
    int n, k;
    cin >> n >> k;
    ll a[200005];
    for(int i = 0; i < n; i++) cin >> a[i];
    ll ans = INF;
    int x = 0;
    if(k * 2 >= n) x = (k * 2 - n) / 2;
    for(int c = 0; c < 2; c++){
        ll s = 0;
        reverse(a, a + n);
        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 = min(ans, s);
    }
    cout << ans << endl;
}
0