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

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

const int maxn = 16;
LL n, x[maxn], y[maxn], p[maxn], ans[maxn], m[maxn];
LL xx[maxn], yy[maxn];
int a[maxn], nx, ny;

void solve(int q) {
  int tot = 0;
  REP(i, 1, n) {
    a[i] = q % 3;
    q /= 3;
    if (a[i] > 0)
      tot++;
  }

  nx = ny = 0;
  REP(i, 1, n)
  if (a[i] == 1)
    xx[++nx] = x[i];
  else if (a[i] == 2)
    yy[++ny] = y[i];
  REP(i, 1, n) m[i] = 3e18;
  LL t = 0;
  REP(i, 1, n) {
    REP(j, 1, nx) m[i] = min(m[i], abs(xx[j] - x[i]));
    REP(j, 1, ny) m[i] = min(m[i], abs(yy[j] - y[i]));
    m[i] = min(m[i], abs(x[i]));
    m[i] = min(m[i], abs(y[i]));
    t += m[i] * p[i];
    if (t >= ans[tot])
      return;
  }
  ans[tot] = min(ans[tot], t);
}

int main() {
  n = read();
  REP(i, 1, n) x[i] = read(), y[i] = read(), p[i] = read();
  REP(i, 0, n) ans[i] = 3e18;
  int q = 1;
  REP(i, 1, n) q *= 3;
  REP(i, 0, q - 1) solve(i);
  REP(i, 0, n) printf("%lld\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

const int maxn = 16;
LL n, x[maxn], y[maxn], p[maxn], ans[maxn], m[maxn];
int a[maxn];

void dfs(int k, int tot) {
  if (k > n) {
    vector<LL> xx, yy;
    REP(i, 1, n)
    if (a[i] == 1)
      xx.pb(x[i]);
    else if (a[i] == 2)
      yy.pb(y[i]);
    REP(i, 1, n) m[i] = 3e18;
    REP(i, 1, n) {
      for (LL j : xx)
        m[i] = min(m[i], abs(j - x[i]));
      for (LL j : yy)
        m[i] = min(m[i], abs(j - y[i]));
      m[i] = min(m[i], abs(x[i]));
      m[i] = min(m[i], abs(y[i]));
    }
    LL t = 0;
    REP(i, 1, n) t += m[i] * p[i];
    ans[tot] = min(ans[tot], t);
    return;
  }
  a[k] = 0;
  dfs(k + 1, tot);
  a[k] = 1;
  dfs(k + 1, tot + 1);
  a[k] = 2;
  dfs(k + 1, tot + 1);
}

int main() {
  n = read();
  REP(i, 1, n) x[i] = read(), y[i] = read(), p[i] = read();
  REP(i, 0, n) ans[i] = 3e18;
  dfs(1, 0);
  REP(i, 0, n) printf("%lld\\n", ans[i]);

  return 0;
}