Submission #13194204


Source Code Expand

Copy
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define All(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()   //reverse
#define strall(v) (v).cbegin(),(v).cend() //const_itterator
#define IN(a, b, x) (a<=x&&x<b)
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<ll>>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename t, typename u, typename Comp=less<>>
bool chmax(t& xmax, const u& x, Comp comp={}) { if(comp(xmax, x)) { xmax = x; return true; } return false;}
template<typename t, typename u, typename Comp=less<>>
bool chmin(t& xmin, const u& x, Comp comp={}) { if(comp(x, xmin)) { xmin = x; return true;} return false;}
const int INF = 1e9;
const ll infl = ll(1e18)+5;
/*
*ios::sync_with_stdio(false);
*cin.tie(0);
*/

int main(){
  int N,K;
  cin >> N >> K;
  vector<int> x(N);
  vector<int> mi,pr;
  rep(i,N) cin >> x[i];
  mi.push_back(0);
  pr.push_back(0);
  //0からの距離を格納
  rep(i,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;//往復分
  //minus側でi個とって足りない分をplus側でとる
  rep(i,n) {
      int j=K-i;
      if(0<=j && j < m) chmin(ans,mi[i]*2 + pr[j]); 
  }
  //逆
  rep(i,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 tShun
Language C++ (GCC 9.2.1)
Score 300
Code Size 1553 Byte
Status AC
Exec Time 41 ms
Memory 4288 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 4
AC × 12
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 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 AC 2 ms 3568 KB
0_01.txt AC 2 ms 3472 KB
0_02.txt AC 2 ms 3464 KB
0_03.txt AC 2 ms 3568 KB
1_00.txt AC 35 ms 4120 KB
1_01.txt AC 38 ms 4216 KB
1_02.txt AC 41 ms 4288 KB
1_03.txt AC 37 ms 4116 KB
1_04.txt AC 34 ms 4124 KB
1_05.txt AC 34 ms 4228 KB
1_06.txt AC 35 ms 4264 KB
1_07.txt AC 37 ms 4184 KB