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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 15;
int n;
ll dp[N + 1][1 << N], v[N][3], TSmem[1 << N][2];
void Min(ll &a, ll b) {
  if (b < a)
    a = b;
}

ll f(int val, int mask, int idx) {
  ll res = 0;
  for (int j = 0; j < n; j++)
    if (mask & (1 << j))
      res += abs(val - v[j][idx]) * v[j][2];
  return res;
}
ll ts(int mask, bool x) {
  ll &res = TSmem[mask][x];
  if (~res)
    return res;
  res = INF;
  int st = -10000, ed = 10000;
  while (ed - st > 3) {
    int md1 = st + (ed - st) / 3, md2 = ed - (ed - st) / 3;
    ll f1 = f(md1, mask, x), f2 = f(md2, mask, x);
    if (f1 > f2)
      st = md1 + 1;
    else if (f2 > f1)
      ed = md2 - 1;
    else
      st = md1, ed = md2;
  }
  for (int i = st; i <= ed; i++)
    res = min(res, f(i, mask, x));
  return res;
}

int main() {
  memset(dp, INF, sizeof dp);
  memset(TSmem, -1, sizeof TSmem);
  run();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  for (int i = 0; i < (1 << n); i++) {
    dp[0][i] = 0;
    for (int j = 0; j < n; j++)
      if (!(i & (1 << j)))
        dp[0][i] += min(abs(v[j][0]), abs(v[j][1])) * v[j][2];
  }
  for (int k = 0; k < n; k++) {
    cout << dp[k][0] << endl;
    for (int i = 0; i < (1 << n); i++) {
      for (int subMask = i; subMask > 0; subMask = (subMask - 1) & i) {
        ll res = dp[k][i] + min(ts(subMask, 0), ts(subMask, 1));
        Min(dp[k + 1][i ^ subMask], res);
      }
    }
  }
  cout << dp[n][0] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 15;
int n;
ll dp[N + 1][1 << N], v[N][3];
void Min(ll &a, ll b) {
  if (b < a)
    a = b;
}

ll f(int val, int mask, int idx) {
  ll res = 0;
  for (int j = 0; j < n; j++)
    if (mask & (1 << j))
      res += abs(val - v[j][idx]) * v[j][2];
  return res;
}
ll ts(int mask, bool x) {
  int st = -10000, ed = 10000;
  while (ed - st > 3) {
    int md1 = st + (ed - st) / 3, md2 = ed - (ed - st) / 3;
    ll f1 = f(md1, mask, x), f2 = f(md2, mask, x);
    if (f1 > f2)
      st = md1 + 1;
    else if (f2 > f1)
      ed = md2 - 1;
    else
      st = md1, ed = md2;
  }
  ll res = INF;
  for (int i = st; i <= ed; i++)
    res = min(res, f(i, mask, x));
  return res;
}

int main() {
  memset(dp, INF, sizeof dp);
  run();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  for (int i = 0; i < (1 << n); i++) {
    dp[0][i] = 0;
    for (int j = 0; j < n; j++)
      if (!(i & (1 << j)))
        dp[0][i] += min(abs(v[j][0]), abs(v[j][1])) * v[j][2];
  }
  for (int k = 0; k < n; k++) {
    cout << dp[k][0] << endl;
    for (int i = 0; i < (1 << n); i++) {
      for (int subMask = i; subMask > 0; subMask = (subMask - 1) & i) {
        ll res = dp[k][i] + min(ts(subMask, 0), ts(subMask, 1));
        Min(dp[k + 1][i ^ subMask], res);
      }
    }
  }
  cout << dp[n][0] << endl;
}