Language: C++

#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define FORR(i,a,b) for (int i = b - 1; i >= a; --i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,0,n)

/*
  すべて正なら0に近い方から順番に火をつければよい。

  正から負、負から正に移動する場合、最も近くに移動するのが効率が良い。
*/

int N, K, x;

typedef struct {
  int p;
  int n;
  int cnt;
  long long cost;
  bool direction; // false: n方向, true: p方向
} State;

int main() {
  scanf("%d%d", &N, &K);
  vector<int> positives, negatives;

  // どちらも昇順に格納
  positives.push_back(0);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &x);
    if (x >= 0) positives.push_back(x);
    else negatives.push_back(x);
  }
  negatives.push_back(0);

  vector<vector<long long>> mat(positives.size(), vector<long long>(negatives.size()));
  for (int p = 0; p < positives.size(); ++p) {
    for (int n = 0; n < negatives.size(); ++n) {
      mat[p][n] = positives[p] - negatives[negatives.size() - 1 - n];
    }
  }

  int minval;
  queue<State> states;
  states.push({0, 0, 0, 0, false});

  while (1) {
    State s = states.front();
    // printf("%d: (%d,%d), cost: %d\n", s.cnt, s.p, s.n, s.cost);
    states.pop();

    if (s.cnt == K) {
      minval = s.cost;
      break;
    }

    if (s.cnt == 0) {
      if (positives.size() > 1) {
        states.push({1, 0, 1, mat[1][0], true});
      }
      if (negatives.size() > 1) {
        states.push({0, 1, 1, mat[0][1], false});
      }
    }
    else {
      if (s.direction) { // p方向から来た
        // p方向に行く
        if (s.p+1 < positives.size()) {
          states.push({s.p+1, s.n, s.cnt+1, s.cost + mat[s.p+1][s.n] - mat[s.p][s.n], true});
        }
        // n方向に行く
        if (s.n+1 < negatives.size()) {
          states.push({s.p, s.n+1, s.cnt+1, s.cost + mat[s.p][s.n+1], false});
        }
      }
      else { // n方向から来た
        // p方向に行く
        if (s.p+1 < positives.size()) {
          states.push({s.p+1, s.n, s.cnt+1, s.cost + mat[s.p+1][s.n], true});
        }
        // n方向に行く
        if (s.n+1 < negatives.size()) {
          states.push({s.p, s.n+1, s.cnt+1, s.cost + mat[s.p][s.n+1] - mat[s.p][s.n], false});
        }
      }
    }

  }
 
  // printf("---\n");
  while (!states.empty()) {
    State s = states.front();
    // printf("%d: (%d,%d), cost: %d\n", s.cnt, s.p, s.n, s.cost);
    states.pop();
    minval = min(minval, s.cost);
  }

  printf("%d\n", minval);
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:100:17: error: no matching function for call to 'min(int&, long long int&)'
  100 |     minval = min(minval, s.cost);
      |              ~~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:100:17: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  100 |     minval = min(minval, s.cost);
      |              ~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:100:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  100 |     minval = min(minval, s.cost);
      |              ~~~^~~~~~~~~~~~~~~~