Problem p02604 (C++) — buggy submission, verdict: Runtime Error

#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 16;
const lli inf = 1e18 + 7;

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

lli dp[maxn][(1 << maxn) + 5];

lli pre[maxn + 5], su[maxn + 5];
lli f[maxn + 5], g[maxn + 5];

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

int bits(int x) {
  int res = 0;
  while (x > 0) {
    res += (x & 1);
    x >>= 1;
  }
  return res;
}

lli calc(vector<pii> &v) {
  pre[0] = v[0].se;
  for (int i = 1; i < sz(v); ++i)
    pre[i] = pre[i - 1] + v[i].se;
  su[sz(v) - 1] = v[sz(v) - 1].se;
  for (int i = sz(v) - 2; i >= 0; --i)
    su[i] = su[i + 1] + v[i].se;
  f[0] = 0;
  for (int i = 1; i < sz(v); ++i)
    f[i] = f[i - 1] + pre[i - 1] * 1LL * (v[i].fi - v[i - 1].fi);
  g[sz(v) - 1] = 0;
  for (int i = sz(v) - 2; i >= 0; --i)
    g[i] = g[i + 1] + su[i + 1] * 1LL * (v[i + 1].fi - v[i].fi);
  lli res = inf;
  for (int i = 0; i < sz(v); ++i)
    res = min(res, f[i] + g[i]);
  return res;
}

void solve() {
  fill_n(&dp[0][0], sizeof(dp) / sizeof(dp[0][0]), inf);
  for (int mask = 0; mask < (1 << n); ++mask) {
    dp[0][mask] = 0;
    for (int k = 0; k < n; ++k)
      if (mask >> k & 1)
        dp[0][mask] += min(abs(x[k]), abs(y[k])) * 1LL * p[k];
  }
  for (int mask = 1; mask < (1 << n); ++mask) {
    if (bits(mask) == 1) {
      dp[1][mask] = 0;
      continue;
    }
    vector<pii> vx, vy;
    for (int k = 0; k < n; ++k)
      if (mask >> k & 1) {
        vx.push_back(pii(x[k], p[k]));
        vy.push_back(pii(y[k], p[k]));
      }
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    dp[1][mask] = min(calc(vx), calc(vy));
  }
  for (int i = 1; i <= n; ++i)
    for (int mask = 1; mask < (1 << n); ++mask) {
      if (bits(mask) <= i) {
        dp[i][mask] = 0;
        continue;
      }
      dp[i][mask] = min(dp[i][mask], dp[i - 1][mask]);
      for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
        if (submask == mask)
          continue;
        int omask = mask ^ submask;
        for (int j = 0; j <= 1; ++j)
          if (dp[i - j][submask] != inf && dp[i - 1][submask] != inf)
            dp[i][mask] = min(dp[i][mask], dp[i - j][submask] + dp[j][omask]);
        // dp[i][mask] = min(dp[i][mask], dp[i - 1][submask] + dp[1][omask]);
      }
    }
  for (int i = 0; i <= n; ++i)
    cout << dp[i][(1 << n) - 1] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  read_input();
  solve();
}


Error: 0

Fixed submission:

#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 15;
const lli inf = 1e18 + 7;

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

lli dp[maxn][(1 << maxn) + 5];

lli pre[maxn + 5], su[maxn + 5];
lli f[maxn + 5], g[maxn + 5];

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

int bits(int x) {
  int res = 0;
  while (x > 0) {
    res += (x & 1);
    x >>= 1;
  }
  return res;
}

lli calc(vector<pii> &v) {
  pre[0] = v[0].se;
  for (int i = 1; i < sz(v); ++i)
    pre[i] = pre[i - 1] + v[i].se;
  su[sz(v) - 1] = v[sz(v) - 1].se;
  for (int i = sz(v) - 2; i >= 0; --i)
    su[i] = su[i + 1] + v[i].se;
  f[0] = 0;
  for (int i = 1; i < sz(v); ++i)
    f[i] = f[i - 1] + pre[i - 1] * 1LL * (v[i].fi - v[i - 1].fi);
  g[sz(v) - 1] = 0;
  for (int i = sz(v) - 2; i >= 0; --i)
    g[i] = g[i + 1] + su[i + 1] * 1LL * (v[i + 1].fi - v[i].fi);
  lli res = inf;
  for (int i = 0; i < sz(v); ++i)
    res = min(res, f[i] + g[i]);
  return res;
}

void solve() {
  fill_n(&dp[0][0], sizeof(dp) / sizeof(dp[0][0]), inf);
  for (int mask = 0; mask < (1 << n); ++mask) {
    dp[0][mask] = 0;
    for (int k = 0; k < n; ++k)
      if (mask >> k & 1)
        dp[0][mask] += min(abs(x[k]), abs(y[k])) * 1LL * p[k];
  }
  for (int mask = 1; mask < (1 << n); ++mask) {
    if (bits(mask) == 1) {
      dp[1][mask] = 0;
      continue;
    }
    vector<pii> vx, vy;
    for (int k = 0; k < n; ++k)
      if (mask >> k & 1) {
        vx.push_back(pii(x[k], p[k]));
        vy.push_back(pii(y[k], p[k]));
      }
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    dp[1][mask] = min(calc(vx), calc(vy));
  }
  for (int i = 1; i <= n; ++i)
    for (int mask = 1; mask < (1 << n); ++mask) {
      if (bits(mask) <= i) {
        dp[i][mask] = 0;
        continue;
      }
      dp[i][mask] = min(dp[i][mask], dp[i - 1][mask]);
      for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
        if (submask == mask)
          continue;
        int omask = mask ^ submask;
        for (int j = 0; j <= 1; ++j)
          if (dp[i - j][submask] != inf && dp[i - 1][submask] != inf)
            dp[i][mask] = min(dp[i][mask], dp[i - j][submask] + dp[j][omask]);
        // dp[i][mask] = min(dp[i][mask], dp[i - 1][submask] + dp[1][omask]);
      }
    }
  for (int i = 0; i <= n; ++i)
    cout << dp[i][(1 << n) - 1] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  read_input();
  solve();
}