Submission #3081172


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/




int N, K, X[101010];
vector<int> mi, pr;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> K;
    rep(i, 0, N) cin >> X[i];

    mi.push_back(0);
    pr.push_back(0);
    rep(i, 0, N) {
        if (X[i] <= 0) mi.push_back(-X[i]);
        else pr.push_back(X[i]);
    }
    sort(all(mi));
    sort(all(pr));

    int n = mi.size();
    int m = pr.size();
    
    int ans = inf * 2;
    rep(i, 0, n) {
        int j = K - i;
        if (0 <= j and j < m) chmin(ans, mi[i] * 2 + pr[j]);
    }
    rep(i, 0, m) {
        int j = K - i;
        if (0 <= j and j < n) chmin(ans, pr[i] * 2 + mi[j]);
    }
    cout << ans << endl;
}

Submission Info

Submission Time
Task C - Candles
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 300
Code Size 1986 Byte
Status
Exec Time 13 ms
Memory 1404 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 300 / 300 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt
Case Name Status Exec Time Memory
0_00.txt 1 ms 256 KB
0_01.txt 1 ms 256 KB
0_02.txt 1 ms 256 KB
0_03.txt 1 ms 256 KB
1_00.txt 13 ms 1276 KB
1_01.txt 13 ms 1276 KB
1_02.txt 13 ms 1276 KB
1_03.txt 13 ms 1276 KB
1_04.txt 12 ms 1276 KB
1_05.txt 12 ms 1276 KB
1_06.txt 13 ms 1404 KB
1_07.txt 13 ms 1276 KB