Language: C++

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct edge
{
  int to, cost;
};

int 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(0LL, S - abs(X[i] - X[j]));
        int malta = max(0LL, S - abs(Y[i] - Y[j]));
        int beeet = max(0LL, 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 >= 3) 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);
        }
        v[idx] = false;
      };
      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:25:24: error: no matching function for call to 'max(long long int, int)'
   25 |         int latte = max(0LL, 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:25:24: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   25 |         int latte = max(0LL, 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:25:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   25 |         int latte = max(0LL, S - abs(X[i] - X[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:26:24: error: no matching function for call to 'max(long long int, int)'
   26 |         int malta = max(0LL, 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:26:24: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   26 |         int malta = max(0LL, 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:26:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   26 |         int malta = max(0LL, S - abs(Y[i] - Y[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:27:24: error: no matching function for call to 'max(long long int, int)'
   27 |         int beeet = max(0LL, 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:27:24: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   27 |         int beeet = max(0LL, 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:27:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   27 |         int beeet = max(0LL, S - abs(Z[i] - Z[j]));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In lambda function:
a.cc:41:17: error: use of an operand of type 'bool' in 'operator++' is forbidden in C++17
   41 |         if(v[idx]++) return;
      |            ~~~~~^