Language: C++

#include <bits/stdc++.h>

using namespace std;

#define int long long
const int INF = 1LL << 58;

struct edge
{
  int to, cost;
};

signed main()
{
  int N, K, S;
  int X[2000], Y[2000], Z[2000];

  while(cin >> N >> K >> S, N) {
    for(int i = 0; i < N; i++) {
      cin >> X[i] >> Y[i] >> Z[i];
    }

    vector< edge > g[2000];
    for(int i = 0; i < N; i++) {
      for(int j = 0; j < i; j++) {
        int latte = max(0, S - abs(X[i] - X[j]));
        int malta = max(0, S - abs(Y[i] - Y[j]));
        int beeet = max(0, S - abs(Z[i] - Z[j]));
        int ukuku = latte * malta + malta * beeet + beeet * latte;
        if(latte > 0 && malta > 0 && beeet > 0) {
          g[i].emplace_back((edge) {j, ukuku});
          g[j].emplace_back((edge) {i, ukuku});
        }
      }
    }

    int ret = -1;
    for(int i = 0; i < N; i++) {
      bool v[2000] = {};
      function< void(int, int, int) > dfs = [&](int idx, int k, int sz)
      {
        if(v[idx]++) return;
        if(k == 0) {
          if(K >= 2) for(auto &e: g[idx]) if(e.to == i) sz += e.cost;
          ret = max(ret, sz);
        } else {
          for(auto &e : g[idx]) dfs(e.to, k - 1, sz + e.cost);
        }
      };
      dfs(i, K - 1, 0);
    }
    if(ret == -1) cout << -1 << endl;
    else cout << 6 * S * S * K - ret * 2 << endl;
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:26:24: error: no matching function for call to 'max(int, long long int)'
   26 |         int latte = max(0, S - abs(X[i] - X[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:26:24: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   26 |         int latte = max(0, S - abs(X[i] - X[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:26:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   26 |         int latte = max(0, S - abs(X[i] - X[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:27:24: error: no matching function for call to 'max(int, long long int)'
   27 |         int malta = max(0, S - abs(Y[i] - Y[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:27:24: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   27 |         int malta = max(0, S - abs(Y[i] - Y[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:27:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   27 |         int malta = max(0, S - abs(Y[i] - Y[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:28:24: error: no matching function for call to 'max(int, long long int)'
   28 |         int beeet = max(0, S - abs(Z[i] - Z[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:28:24: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   28 |         int beeet = max(0, S - abs(Z[i] - Z[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:28:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   28 |         int beeet = max(0, S - abs(Z[i] - Z[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In lambda function:
a.cc:42:17: error: use of an operand of type 'bool' in 'operator++' is forbidden in C++17
   42 |         if(v[idx]++) return;
      |            ~~~~~^