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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

struct P {
  int64_t x;
  int64_t y;
  int64_t p;
};

int64_t val[1 << 15] = {};
int64_t rem[1 << 15] = {};
int pos[1 << 15][15] = {};

int64_t evaluate(vector<P> &ps, int s, int64_t v, bool is_x) {
  int64_t sum = 0;
  FOR_LT(i, 0, ps.size()) {
    if (s & (1 << i)) {
      if (is_x) {
        sum += abs(ps[i].x - v) * ps[i].p;
      } else {
        sum += abs(ps[i].y - v) * ps[i].p;
      }
    }
  }
  return sum;
}

int64_t dp[16][(1 << 15)];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;

  vector<P> ps(n);
  FOR_LT(i, 0, n) {
    auto &p = ps[i];
    cin >> p.x >> p.y >> p.p;
  }

  val[0] = 0;
  rem[0] = 0;
  FOR_LT(i, 1, (1 << n)) {
    int64_t vmin = INT64_MAX;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        vmin = min(vmin, evaluate(ps, i, ps[j].x, true));
        vmin = min(vmin, evaluate(ps, i, ps[j].y, false));
      }
    }
    val[i] = vmin;

    int c = 0;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        pos[i][c] = (1 << j);
        c++;
      }
    }

    int64_t vrem = 0;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        vrem += min(abs(ps[j].x), abs(ps[j].y)) * ps[j].p;
      }
    }
    rem[i] = vrem;
  }

  FOR_LT(i, 0, (1 << n)) { dp[0][i] = rem[i]; }

  FOR_LE(i, 1, n) {
    FOR_LT(j, 0, (1 << n)) {
      dp[i][j] = INT64_MAX;
      int k = j;
      for (int k = j; k != 0; k = (k - 1) & j) {
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + val[j - k]);
      }
      dp[i][j] = min(dp[i][j], dp[i - i][0] + val[j]);
    }
  }

  FOR_LE(i, 0, n) { cout << dp[i][(1 << n) - 1] << endl; }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

struct P {
  int64_t x;
  int64_t y;
  int64_t p;
};

int64_t val[1 << 15] = {};
int64_t rem[1 << 15] = {};
int pos[1 << 15][15] = {};

int64_t evaluate(vector<P> &ps, int s, int64_t v, bool is_x) {
  int64_t sum = 0;
  FOR_LT(i, 0, ps.size()) {
    if (s & (1 << i)) {
      if (is_x) {
        sum += abs(ps[i].x - v) * ps[i].p;
      } else {
        sum += abs(ps[i].y - v) * ps[i].p;
      }
    }
  }
  return sum;
}

int64_t dp[16][(1 << 15)];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;

  vector<P> ps(n);
  FOR_LT(i, 0, n) {
    auto &p = ps[i];
    cin >> p.x >> p.y >> p.p;
  }

  val[0] = 0;
  rem[0] = 0;
  FOR_LT(i, 1, (1 << n)) {
    int64_t vmin = INT64_MAX;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        vmin = min(vmin, evaluate(ps, i, ps[j].x, true));
        vmin = min(vmin, evaluate(ps, i, ps[j].y, false));
      }
    }
    val[i] = vmin;

    int c = 0;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        pos[i][c] = (1 << j);
        c++;
      }
    }

    int64_t vrem = 0;
    FOR_LT(j, 0, n) {
      if (i & (1 << j)) {
        vrem += min(abs(ps[j].x), abs(ps[j].y)) * ps[j].p;
      }
    }
    rem[i] = vrem;
  }

  FOR_LT(i, 0, (1 << n)) { dp[0][i] = rem[i]; }

  FOR_LE(i, 1, n) {
    FOR_LT(j, 0, (1 << n)) {
      dp[i][j] = INT64_MAX;
      FOR_LT(k, 0, (1 << n)) {
        if ((j | k) == j) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + val[j - k]);
        }
      }
      // cout << i << " " << j << " " << dp[i][j] << endl;
    }
  }

  FOR_LE(i, 0, n) { cout << dp[i][(1 << n) - 1] << endl; }

  return 0;
}