結果

問題 No.370 道路の掃除
ユーザー はむこ
提出日時 2016-05-13 22:40:06
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,068 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 162,928 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-14 16:43:43
合計ジャッジ時間 2,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using ll = long long; using ld = long double; using vll = vector<ll>; using vld = vector<ld>;

static const long long INF = 1e18;

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll n, m; cin >> n >> m;
    vll a(m); rep(i, a.size()) cin >> a[i];
    sort(all(a));
    ll negnum = (ll)(upper_bound(all(a), 0) - a.begin());
    ll posnum = m - negnum;

    ll ret = INF;
    rep(j, n + 1) { // j個正のを取る
        if (j > posnum || n - j > negnum) continue;
        ll sp = 0; if (j != 0) sp = +a[negnum+j-1];
        ll sn = 0; if (j != n) sn = -a[negnum+j-n];
        chmin(ret, sp + sn * 2);
        chmin(ret, sn + sp * 2);
    }

    cout << ret << endl;
           
    return 0;
}          
0