Language: C++

#include <bits/stdc++.h>

#define REP(i,n) for(int i = 0;i < (n);i++)
const ll INF = 2 * (int)(1e9);
using namespace std;

int N, K, S;

struct Point{
    int x, y, z;
};

Point p[2003];

bool iscycle[2003];
bool used[2003];
deque<int> graph[2003];

int getS(Point a,Point b){
    if(a.x > b.x) swap(a.x, b.x);
    if(a.y > b.y) swap(a.y, b.y);
    if(a.z > b.z) swap(a.z, b.z);
    return (a.x + S - b.x) * (a.y + S - b.y) * 2
        +  (a.z + S - b.z) * (a.x + S - b.x) * 2
        +  (a.y + S - b.y) * (a.z + S - b.z) * 2;
}

bool connected(int a,int b){
    REP(_,2){
        int x2 = p[b].x;
        int y2 = p[b].y;
        int z2 = p[b].z;
        int xs = S, ys = S;
        REP(i,8){
            if(i && (i % 4 == 0)){
                z2 += S;
            }
            if(p[a].x <= x2 && x2 <= p[a].x + S
               && p[a].y <= y2 && y2 <= p[a].y + S
               && p[a].z <= z2 && z2 <= p[a].z + S){
                return true;
            }
            if(i % 2 == 0){
                x2 += xs;
                xs = -xs;
            }
            if(i % 2 == 1){
                y2 += ys;
                ys = -ys;
            }
        }
        swap(a,b);
    }
    return false;
}

void makegraph(int graphct, int i, bool maincalled, bool back){
    REP(j,N){
        if(used[j])continue;
        if(connected(i,j)){
            used[j] = true;
            if(graph[graphct].size() > 1 && (maincalled || back)){
                graph[graphct].push_front(j);
                makegraph(graphct, j, false, true);
            }else{
                graph[graphct].push_back(j);
                makegraph(graphct, j, false, false);
            }
        }
    }
}


int main(){
    while(cin >> N >> K >> S, N){
        REP(i,N){
            int x, y, z;
            cin >> x >> y >> z;
            p[i] = Point{x, y, z};
            graph[i].clear();
            iscycle[i] = false;
            used[i] = false;
        }

        //make graph
        int graphct = 0;
        REP(i,N){
            if(used[i]) continue;
            graph[graphct].push_back(i);
            used[i] = true;
            makegraph(graphct, i, true, true);
            graphct++;
        }

        //cycle check
        REP(i,graphct){
            if(graph[i].size() > 2 && connected(graph[i][0],graph[i][graph[i].size() - 1]))
                iscycle[i] = true;
        }
        
        long long ans = INF;
        REP(i,graphct){
            if((int)graph[i].size() < K) continue;
            REP(j,graph[i].size()){
                if(j + K - 1 > graph[i].size() - 1) break;
                long long tmpans = 0;
                for(int r = 0; r < K;r++){
                    tmpans += S*S*6;
                    if(r){
                        tmpans -= getS(p[graph[i][j + r]], p[graph[i][j + r - 1]]);
                    }else if(iscycle[i] && K == graph[i].size()){
                        tmpans -= getS(p[graph[i][0]], p[graph[i][graph[i].size() - 1]]);
                    }
                }
                ans = min(tmpans, ans);
            }
        }
        if(ans == INF) cout << -1 << endl;
        else cout << ans << endl;
    }
    return 0;
}

Compiler output:
a.cc:4:7: error: 'll' does not name a type
    4 | const ll INF = 2 * (int)(1e9);
      |       ^~
a.cc: In function 'int main()':
a.cc:101:25: error: 'INF' was not declared in this scope
  101 |         long long ans = INF;
      |                         ^~~