Problem p01097 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
ll inf = (ll)20000000000000;
vector<ll> x, y, z;
int n, k, s;
ll calc(int i, int j) {
  ll tx = 0, ty = 0, tz = 0;
  if (x[i] <= x[j] && x[j] <= x[i] + s) {
    tx = x[i] + s - x[j];
  }
  if (y[i] <= y[j] && y[j] <= y[i] + s) {
    ty = y[i] + s - y[j];
  }
  if (z[i] <= z[j] && z[j] <= z[i] + s) {
    tz = z[i] + s - z[j];
  }
  if (x[j] <= x[i] && x[i] <= x[j] + s) {
    tx = x[j] + s - x[i];
  }
  if (y[j] <= y[i] && y[i] <= y[j] + s) {
    ty = y[j] + s - y[i];
  }
  if (z[j] <= z[i] && z[i] <= z[j] + s) {
    tz = z[j] + s - z[i];
  }
  if (tx == 0 || ty == 0 || tz == 0) {
    return -1;
  }
  return 2 * (tx * ty + ty * tz + tz * tx);
}

ll saiki(int st, int i, int b, int c, vector<vector<pair<int, ll>>> &g) {
  if (c == k) {
    ll p = 0;
    for (auto x : g[i]) {
      if (x.first == st && k != 2) {
        p = x.second;
      }
    }
    return p;
  }
  ll p = -inf;
  for (auto x : g[i]) {
    if (x.first != st && x.first != b) {
      p = max(p, x.second + saiki(st, x.first, i, c + 1, g));
    }
  }
  return p;
}

int main() {

  while (scanf("%d %d %d", &n, &k, &s) && n != 0) {
    x.resize(n);
    y.resize(n);
    z.resize(n);
    for (int i = 0; i < n; i++)
      scanf("%lld %lld %lld", &x[i], &y[i], &z[i]);
    vector<vector<pair<int, ll>>> g(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i < j) {
          ll sm = calc(i, j);
          if (sm != -1) {
            g[i].push_back(make_pair(j, sm));
            g[j].push_back(make_pair(i, sm));
          }
        }
      }
    }
    ll ans = -1;
    if (k == 1) {
      cout << s * s * 6 << endl;
      continue;
    }
    for (int i = 0; i < n; i++) {
      ans = max(ans, saiki(i, i, -1, 1, g));
    }
    if (ans == -1)
      cout << -1 << endl;
    else
      cout << s * s * k * 6 - ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
ll inf = (ll)20000000000000;
vector<ll> x, y, z;
int n, k, s;
ll calc(int i, int j) {
  ll tx = 0, ty = 0, tz = 0;
  if (x[i] <= x[j] && x[j] <= x[i] + s) {
    tx = x[i] + s - x[j];
  }
  if (y[i] <= y[j] && y[j] <= y[i] + s) {
    ty = y[i] + s - y[j];
  }
  if (z[i] <= z[j] && z[j] <= z[i] + s) {
    tz = z[i] + s - z[j];
  }
  if (x[j] <= x[i] && x[i] <= x[j] + s) {
    tx = x[j] + s - x[i];
  }
  if (y[j] <= y[i] && y[i] <= y[j] + s) {
    ty = y[j] + s - y[i];
  }
  if (z[j] <= z[i] && z[i] <= z[j] + s) {
    tz = z[j] + s - z[i];
  }
  if (tx == 0 || ty == 0 || tz == 0) {
    return -1;
  }
  return 2 * (tx * ty + ty * tz + tz * tx);
}

ll saiki(int st, int i, int b, int c, vector<vector<pair<int, ll>>> &g) {
  if (c == k) {
    ll p = 0;
    for (auto x : g[i]) {
      if (x.first == st) {
        p = x.second;
      }
    }
    return p;
  }
  ll p = -inf;
  for (auto x : g[i]) {
    if (x.first != st && x.first != b) {
      p = max(p, x.second + saiki(st, x.first, i, c + 1, g));
    }
  }
  return p;
}

int main() {

  while (scanf("%d %d %d ", &n, &k, &s) && n != 0) {
    x.resize(n);
    y.resize(n);
    z.resize(n);
    for (int i = 0; i < n; i++)
      scanf("%lld %lld %lld ", &x[i], &y[i], &z[i]);
    vector<vector<pair<int, ll>>> g(n);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          ll sm = calc(i, j);
          if (sm != -1) {
            g[i].push_back(make_pair(j, sm));
            g[j].push_back(make_pair(i, sm));
          }
        }
      }
    }
    ll ans = -1;
    if (k == 1) {
      cout << s * s * 6 << endl;
      continue;
    }
    for (int i = 0; i < n; i++) {
      ans = max(ans, saiki(i, i, -1, 1, g));
    }
    if (ans == -1)
      cout << -1 << endl;
    else
      cout << s * s * k * 6 - ans << endl;
  }
  return 0;
}