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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int n;
ll ans[16];
int cur[16];
int x[16], y[16], p[16];
int minx[40000][16], miny[40000][16];

void dfs(int m, int rx, int ry, int kisuu, int kazu) {
  if (m == n) {
    ll temp = 0;
    for (int i = 0; i < n; i++) {
      temp += 1ll * p[i] * min(minx[rx][i], miny[ry][i]);
    }
    ans[kazu] = min(ans[kazu], temp);
  } else {
    cur[m] = 1;
    dfs(m + 1, rx + kisuu, ry, 2 * kisuu, kazu + 1);
    cur[m] = 0;
    dfs(m + 1, rx, ry, 2 * kisuu, kazu);
    cur[m] = -1;
    dfs(m + 1, rx, ry + kisuu, 2 * kisuu, kazu + 1);
  }
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int i = 0; i < n + 1; i++) {
    ans[i] = 1e18;
  }
  for (int i = 0; i < (1 << n); i++) {
    bool ch[n];
    for (int j = 0; j < n; j++) {
      ch[j] = false;
    }
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1)
        ch[j] = true;
    }
    for (int j = 0; j < n; j++) {
      if (ch[j]) {
        minx[i][j] = 0;
        miny[i][j] = 0;
      } else {
        int tempx = min(abs(x[j]), abs(y[j]));
        int tempy = tempx;
        for (int k = 0; k < n; k++) {
          if (ch[k]) {
            tempx = min(tempx, abs(x[j] - x[k]));
            tempy = min(tempy, abs(y[j] - y[k]));
          }
        }
        minx[i][j] = tempx;
        miny[i][j] = tempy;
      }
    }
  }
  dfs(0, 0, 0, 1, 0);
  for (int i = 0; i <= n; i++) {
    if (ans[i] == 1e18)
      cout << 0 << endl;
    else
      cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int n;
ll ans[16];
int cur[16];
int x[16], y[16], p[16];
int minx[40000][16], miny[40000][16];

void dfs(int m, int rx, int ry, int kisuu) {
  if (m == n) {
    set<ll> memox, memoy;
    for (int i = 0; i < n; i++) {
      if (cur[i] == 1)
        memox.insert(x[i]);
      else if (cur[i] == -1)
        memoy.insert(y[i]);
    }
    int sum = (int)memox.size() + (int)memoy.size();
    ll temp = 0;
    for (int i = 0; i < n; i++) {
      temp += 1ll * p[i] * min(minx[rx][i], miny[ry][i]);
    }
    ans[sum] = min(ans[sum], temp);
  } else {
    cur[m] = 1;
    dfs(m + 1, rx + kisuu, ry, 2 * kisuu);
    cur[m] = 0;
    dfs(m + 1, rx, ry, 2 * kisuu);
    cur[m] = -1;
    dfs(m + 1, rx, ry + kisuu, 2 * kisuu);
  }
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int i = 0; i < n + 1; i++) {
    ans[i] = 1e18;
  }
  for (int i = 0; i < (1 << n); i++) {
    bool ch[n];
    for (int j = 0; j < n; j++) {
      ch[j] = false;
    }
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1)
        ch[j] = true;
    }
    for (int j = 0; j < n; j++) {
      if (ch[j]) {
        minx[i][j] = 0;
        miny[i][j] = 0;
      } else {
        int tempx = min(abs(x[j]), abs(y[j]));
        int tempy = tempx;
        for (int k = 0; k < n; k++) {
          if (ch[k]) {
            tempx = min(tempx, abs(x[j] - x[k]));
            tempy = min(tempy, abs(y[j] - y[k]));
          }
        }
        minx[i][j] = tempx;
        miny[i][j] = tempy;
      }
    }
  }
  dfs(0, 0, 0, 1);
  for (int i = 0; i <= n; i++) {
    if (ans[i] == 1e18)
      cout << 0 << endl;
    else
      cout << ans[i] << endl;
  }
  return 0;
}