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

#include <bits/stdc++.h>
using namespace std;

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[(1 << 15)][20];

int n;
int x[30], y[30], p[30];

int get(int bit) {
  vector<pair<int, int>> tate, yoko;
  int ans = LLONG_MAX;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (bit & (1 << i)) {
      tate.push_back({y[i], p[i]});
      yoko.push_back({x[i], p[i]});
      cnt += p[i];
    }
  }

  {
    sort(tate.begin(), tate.end());
    int tmp = 0;
    int now;
    for (int i = 0; i < tate.size(); i++) {
      tmp += tate[i].second;
      now = tate[i].first;
      if (tmp >= (cnt + 1) / 2)
        break;
    }

    int ret = 0;
    for (int i = 0; i < tate.size(); i++) {
      ret += abs(now - tate[i].first) * tate[i].second;
    }
    chmin(ans, ret);
  }

  {
    sort(yoko.begin(), yoko.end());
    int tmp = 0;
    int now;
    for (int i = 0; i < yoko.size(); i++) {
      tmp += yoko[i].second;
      now = yoko[i].first;
      if (tmp >= (cnt + 1) / 2)
        break;
    }

    int ret = 0;
    for (int i = 0; i < yoko.size(); i++) {
      ret += abs(now - yoko[i].first) * yoko[i].second;
    }
    chmin(ans, ret);
  }
  return ans;
}

int get2(int bit) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!(bit & (1 << i))) {
      ans += min(abs(x[i]), abs(y[i])) * p[i];
    }
  }
  return ans;
}

int memo[(1 << 15)];
signed main() {
  cin >> n;
  for (int i = 0; i < (1 << n); i++) {
    dp[i][0] = 0;
    for (int j = 1; j < 20; j++) {
      dp[i][j] = LLONG_MAX / 10;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }

  cout << get2(0) << endl;

  int aa = LLONG_MAX;
  for (int i = 0; i < (1 << n); i++) {
    memo[i] = dp[i][1] = get(i);
    chmin(aa, dp[i][1] + get2(i));
  }
  cout << aa << endl;

  for (int k = 2; k <= n; k++) {
    int ans = LLONG_MAX;
    for (int b = 0; b < (1 << n); b++) {
      for (int b2 = b; b2 >= 0; b2 = (b2 - 1) & b) {
        chmin(dp[b][k], dp[b2][k - 1] + memo[b ^ b2]);
        if (b2 == 0)
          break;
      }
      chmin(ans, dp[b][k] + get2(b));
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[(1 << 15)][20];

int n;
int x[30], y[30], p[30];

int get(int bit) {
  vector<pair<int, int>> tate, yoko;
  int ans = LLONG_MAX;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (bit & (1 << i)) {
      tate.push_back({y[i], p[i]});
      yoko.push_back({x[i], p[i]});
      cnt += p[i];
    }
  }

  {
    sort(tate.begin(), tate.end());
    int tmp = 0;
    int now;
    for (int i = 0; i < tate.size(); i++) {
      tmp += tate[i].second;
      now = tate[i].first;
      if (tmp >= (cnt + 1) / 2)
        break;
    }

    int ret = 0;
    for (int i = 0; i < tate.size(); i++) {
      ret += abs(now - tate[i].first) * tate[i].second;
    }
    chmin(ans, ret);
  }

  {
    sort(yoko.begin(), yoko.end());
    int tmp = 0;
    int now;
    for (int i = 0; i < yoko.size(); i++) {
      tmp += yoko[i].second;
      now = yoko[i].first;
      if (tmp >= (cnt + 1) / 2)
        break;
    }

    int ret = 0;
    for (int i = 0; i < yoko.size(); i++) {
      ret += abs(now - yoko[i].first) * yoko[i].second;
    }
    chmin(ans, ret);
  }
  return ans;
}

int get2(int bit) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!(bit & (1 << i))) {
      ans += min(abs(x[i]), abs(y[i])) * p[i];
    }
  }
  return ans;
}

signed main() {
  cin >> n;
  for (int i = 0; i < (1 << n); i++) {
    dp[i][0] = 0;
    for (int j = 1; j < 20; j++) {
      dp[i][j] = LLONG_MAX / 10;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }

  cout << get2(0) << endl;

  int aa = LLONG_MAX;
  for (int i = 0; i < (1 << n); i++) {
    dp[i][1] = get(i);
    chmin(aa, dp[i][1] + get2(i));
  }
  cout << aa << endl;

  for (int k = 2; k <= n; k++) {
    int ans = LLONG_MAX;
    for (int b = 0; b < (1 << n); b++) {
      for (int b2 = b; b2 >= 0; b2 = (b2 - 1) & b) {
        chmin(dp[b][k], dp[b2][k - 1] + get(b ^ b2));
        if (b2 == 0)
          break;
      }
      chmin(ans, dp[b][k] + get2(b));
    }
    cout << ans << endl;
  }

  return 0;
}