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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int n, m;
vint s, d;
int dp[111][1 << 15];
int table[111][1 << 15];

int cost(int bit, int idx) {
  int sum = 0;
  rep(i, n) if ((bit >> i) & 1) sum += s[i];
  return abs(sum - d[idx]);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while (cin >> n >> m, n + m) {
    resz(s, n);
    rep(i, n) cin >> s[i];
    resz(d, m);
    rep(i, m) cin >> d[i];
    sort(all(d));
    rep(i, m) { rep(bit, 1 << n) table[i][bit] = cost(bit, i); }
    fill(dp[0], dp[111], inf);
    dp[0][0] = 0;
    rep(i, m) {
      rep(bit, 1 << n) {
        if (dp[i][bit] == inf)
          continue;
        dp[i + 1][bit] = dp[i][bit] + table[i][bit];
      }
      rep(bit, 1 << n) {
        if (dp[i + 1][bit] == inf)
          continue;
        // cout << i << " " << static_cast<bitset<8>>(bit) << endl;
        rep(j, n) {
          if ((bit >> j) & 1)
            continue;
          chmin(dp[i + 1][bit | 1 << j],
                dp[i + 1][bit] - table[i][bit] + table[i][bit | 1 << j]);
          // cout<<dp[i+1][bit|1<<j]<<endl;
        }
      }
    }
    /*
    rep(i, m+1) {
      rep(j, 1<<n) cout << dp[i][j] << " ";
      cout << endl;
    }
    //*/
    int ans = inf;
    rep(bit, 1 << n) chmin(ans, dp[m][bit]);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n, m;
  while (cin >> n >> m, n + m) {
    vint s(n);
    rep(i, n) cin >> s[i];
    vint d(m);
    rep(i, m) cin >> d[i];
    sort(all(d));
    int dp[m + 1][1 << n];
    auto cost = [&](int bit, int idx) {
      int sum = 0;
      rep(i, n) if ((bit >> i) & 1) sum += s[i];
      return abs(sum - d[idx]);
    };
    fill(dp[0], dp[m + 1], inf);
    dp[0][0] = 0;
    rep(i, m) {
      rep(bit, 1 << n) if (dp[i][bit] != inf) dp[i + 1][bit] =
          dp[i][bit] + cost(bit, i);
      rep(bit, 1 << n) {
        if (dp[i + 1][bit] == inf)
          continue;
        // cout << i << " " << static_cast<bitset<8>>(bit) << endl;
        rep(j, n) {
          if ((bit >> j) & 1)
            continue;
          chmin(dp[i + 1][bit | 1 << j],
                dp[i + 1][bit] - cost(bit, i) + cost(bit | 1 << j, i));
          // cout<<dp[i+1][bit|1<<j]<<endl;
        }
      }
    }
    /*
    rep(i, m+1) {
      rep(j, 1<<n) cout << dp[i][j] << " ";
      cout << endl;
    }
    //*/
    int ans = inf;
    rep(bit, 1 << n) chmin(ans, dp[m][bit]);
    cout << ans << endl;
  }

  return 0;
}