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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <ctype.h>
const int bufSize = 1e6;
#define DEBUG
inline char nc() {
#ifdef DEBUG
  return getchar();
#endif
  static char buf[bufSize], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(char *s) {
  static char c;
  for (; !isalpha(c); c = nc())
    ;
  for (; isalpha(c); c = nc())
    *s++ = c;
  *s = '\\0';
}
template <typename T> inline T read(T &r) {
  static char c;
  static int flag;
  flag = 1, r = 0;
  for (c = nc(); !isdigit(c); c = nc())
    if (c == '-')
      flag = -1;
  for (; isdigit(c); c = nc())
    r = r * 10 + c - 48;
  return r *= flag;
}
const int maxn = 19;
int n;
int X[maxn], Y[maxn], P[maxn];
long long xdis[maxn][maxn], ydis[maxn][maxn];
long long xval[maxn][1 << maxn], yval[maxn][1 << maxn];
long long ans[maxn];
int id[maxn];
template <typename T> inline T abs(const T &x) { return x > 0 ? x : -x; }
int main() {
  read(n);
  for (int i = 0; i < n; ++i)
    read(X[i]), read(Y[i]), read(P[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      xdis[i][j] = abs(X[i] - X[j]), ydis[i][j] = abs(Y[i] - Y[j]);
  for (int i = 0; i <= n; ++i)
    ans[i] = 1ll << 62;
  int maxx = (1 << n) - 1;
  for (int i = 0; i <= maxx; ++i)
    for (int j = 0; j < n; ++j) {
      xval[j][i] = abs(X[j]), yval[j][i] = abs(Y[j]);
      for (int k = 0; k < n; ++k)
        if (i & (1 << k))
          xval[j][i] = std::min(xval[j][i], xdis[k][j]),
          yval[j][i] = std::min(yval[j][i], ydis[k][j]);
      xval[j][i] *= P[j], yval[j][i] *= P[j];
    }
  for (int i = 0; i <= maxx; ++i) {
    int num = 0;
    for (int j = i; j > 0; j >>= 1)
      num += j & 1;
    for (int j = i; j >= 0; --j) {
      j &= i;
      long long tans = 0;
      for (int k = 0; k < n; ++k)
        if (!((1 << k) & i))
          tans += std::min(xval[k][j], yval[k][(~j) & i]);
      ans[num] = std::min(ans[num], tans);
    }
  }
  for (int i = 0; i <= n; ++i)
    printf("%lld\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast", "-funroll-loops")
#pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <algorithm>
#include <cstdio>
#include <ctype.h>
#define DEBUG
const int bufSize = 1e6;
inline char nc() {
#ifdef DEBUG
  return getchar();
#endif
  static char buf[bufSize], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(char *s) {
  static char c;
  for (; !isalpha(c); c = nc())
    ;
  for (; isalpha(c); c = nc())
    *s++ = c;
  *s = '\\0';
}
template <typename T> inline T read(T &r) {
  static char c;
  static int flag;
  flag = 1, r = 0;
  for (c = nc(); !isdigit(c); c = nc())
    if (c == '-')
      flag = -1;
  for (; isdigit(c); c = nc())
    r = r * 10 + c - 48;
  return r *= flag;
}
const int maxn = 40;
int n;
struct node {
  int x, y, p;
} A[maxn];
int mindis[maxn], steps;
long long ans[maxn], tans;
template <typename T> inline T _abs(const T &x) { return x > 0 ? x : -x; }
void dfs(int x) {
  if (steps > n)
    return;
  tans = 0;
  for (int i = 1; i <= n; ++i)
    tans += 1ll * A[i].p * mindis[i];
  ans[steps] = std::min(ans[steps], tans);
  if (x > n)
    return;
  //    printf("%d %d %d\\n",steps,ans[steps],tans);
  dfs(x + 1);
  int copy[16];
  for (int i = 1; i <= n; ++i)
    copy[i] = mindis[i];

  ++steps;
  for (int i = 1; i <= n; ++i)
    mindis[i] = std::min(mindis[i], _abs(A[i].x - A[x].x));
  dfs(x + 1);
  --steps;
  for (int i = 1; i <= n; ++i)
    mindis[i] = copy[i];

  ++steps;
  for (int i = 1; i <= n; ++i)
    mindis[i] = std::min(mindis[i], _abs(A[i].y - A[x].y));
  dfs(x + 1);
  --steps;
  for (int i = 1; i <= n; ++i)
    mindis[i] = copy[i];

  steps += 2;
  for (int i = 1; i <= n; ++i)
    mindis[i] = std::min(
        mindis[i], std::min(_abs(A[i].x - A[x].x), _abs(A[i].y - A[x].y)));
  dfs(x + 1);
  steps -= 2;
  for (int i = 1; i <= n; ++i)
    mindis[i] = copy[i];
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(A[i].x), read(A[i].y), read(A[i].p);
  for (int i = 1; i <= n; ++i)
    mindis[i] = std::min(_abs(A[i].x), _abs(A[i].y));
  for (int i = 0; i <= n; ++i)
    ans[i] = 1ll << 62;
  dfs(1);
  for (int i = 0; i <= n; ++i)
    printf("%lld\\n", ans[i]);
  return 0;
}