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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint x[20], y[20], p[20];
P px[20], py[20];
llint ansx[1 << 15][20], ansy[1 << 15][20];
llint cost[20][20], zcost[20][20];
llint dp[20][20];

void calc(llint mask, P px[], llint ans[1 << 15][20]) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i > j)
        continue;

      llint sum = 0;
      for (int k = i; k <= j; k++) {
        llint id = px[k].second, x = px[k].first;
        if ((mask & (1 << id)) == 0)
          continue;
        sum += p[id];
      }

      cost[i][j] = 0, zcost[i][j] = 0;
      if (sum == 0)
        continue;

      llint mid, tmp = 0;
      for (int k = i; k <= j; k++) {
        llint id = px[k].second;
        if ((mask & (1 << id)) == 0)
          continue;
        tmp += p[id];
        if (tmp >= sum / 2) {
          mid = px[k].first;
          break;
        }
      }

      for (int k = i; k <= j; k++) {
        llint id = px[k].second;
        if ((mask & (1 << id)) == 0)
          continue;
        cost[i][j] += p[id] * abs(mid - px[k].first);
        zcost[i][j] += p[id] * abs(px[k].first);
      }
    }
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < i; k++) {
        if (j - 1 >= 0)
          dp[i][j] = min(dp[i][j], dp[k][j - 1] + cost[k][i - 1]);
        dp[i][j] = min(dp[i][j], dp[k][j] + zcost[k][i - 1]);
      }
    }
  }
  ans[mask][0] = dp[n][0];
  for (int i = 1; i <= n; i++)
    ans[mask][i] = min(ans[mask][i - 1], dp[n][i]);
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

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

  llint N = 1 << n;
  for (int i = 0; i < N; i++) {
    calc(i, px, ansx);
    calc(i, py, ansy);
    // for(int j = 0; j <= n; j++) cout << ansx[i][j] << " "; cout << endl;
  }

  for (int i = 0; i <= n; i++) {
    llint ans = inf;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= i; k++) {
        ans = min(ans, ansx[j][k] + ansy[(N - 1) - j][i - k]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint x[20], y[20], p[20];
P px[20], py[20];
llint ansx[1 << 15][20], ansy[1 << 15][20];
llint cost[20][20], zcost[20][20];
bool flag[20][20];
llint dp[20][20];

llint calc(llint mask, P px[], llint ans[1 << 15][20]) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i > j)
        continue;

      llint sum = 0, mn = inf, mx = -inf;
      for (int k = i; k <= j; k++) {
        llint id = px[k].second, x = px[k].first;
        if ((mask & (1 << id)) == 0)
          continue;
        sum += p[id];
        mn = min(mn, x);
        mx = max(mx, x);
      }

      cost[i][j] = 0, zcost[i][j] = 0;
      if (sum == 0)
        continue;

      llint mid, tmp = 0;
      for (int k = i; k <= j; k++) {
        llint id = px[k].second;
        if ((mask & (1 << id)) == 0)
          continue;
        tmp += p[id];
        if (tmp >= sum / 2) {
          mid = px[k].first;
          break;
        }
      }

      for (int k = i; k <= j; k++) {
        llint id = px[k].second;
        if ((mask & (1 << id)) == 0)
          continue;
        cost[i][j] += p[id] * abs(mid - px[k].first);
        zcost[i][j] += p[id] * abs(px[k].first);
      }
    }
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < i; k++) {
        if (j - 1 >= 0)
          dp[i][j] = min(dp[i][j], dp[k][j - 1] + cost[k][i - 1]);
        dp[i][j] = min(dp[i][j], dp[k][j] + zcost[k][i - 1]);
      }
    }
  }
  ans[mask][0] = dp[n][0];
  for (int i = 1; i <= n; i++)
    ans[mask][i] = min(ans[mask][i - 1], dp[n][i]);
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

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

  llint N = 1 << n;
  for (int i = 0; i < N; i++) {
    calc(i, px, ansx);
    calc(i, py, ansy);
    // for(int j = 0; j <= n; j++) cout << ansx[i][j] << " "; cout << endl;
  }

  for (int i = 0; i <= n; i++) {
    llint ans = inf;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= i; k++) {
        ans = min(ans, ansx[j][k] + ansy[(N - 1) - j][i - k]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}