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

#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
    cout.precision(20);
  }
} fast;

/* define */
#define FOR(I, X, Y) for (long long(I) = (X); (I) < (Y); (I)++)
#define REP(I, X, Y) for (long long(I) = (Y)-1; (I) >= (X); (I)--)
#define ALL(X) (X).begin(), (X).end()
#define pb push_back
#define COUNT(V, X)                                                            \\
  (upper_bound((V).begin(), (V).end(), X) -                                    \\
   lower_bound((V).begin(), (V).end(), X))
#define debug(x) cerr << #x << ':' << x << endl;
#define DEBUG(v)                                                               \\
  {                                                                            \\
    cerr << #v << ':';                                                         \\
    for (auto xv : v)                                                          \\
      cerr << xv << ' ';                                                       \\
    cerr << endl;                                                              \\
  }
#define Yes(X) cout << (X ? "Yes" : "No") << endl;
#define YES(X) cout << (X ? "YES" : "NO") << endl;
#define ctoi(C) (C - '0')
#define pow2(x) ((long long)((long long)1 << x))

/* alias */
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using pi = pair<int, int>;
using pl = pair<long long, long long>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const long long dx[] = {1, 0, -1, 0};
const long long dy[] = {0, 1, 0, -1};
const long long dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long dx9[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
const long long dy9[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int INF = 1000000007;
const long long LINF = 1000000000000000007;

/* func */
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a < b;
}
long long max(long long x, int y) { return max(x, (long long)y); }
long long max(int x, long long y) { return max((long long)x, y); }
long long min(long long x, int y) { return min(x, (long long)y); }
long long min(int x, long long y) { return min((long long)x, y); }

/* liblary */

/* main */

map<ll, ll> m;

ll dfs(ll bits, ll &N, ll &M, vl &s, vl &d) {
  if (m[bits])
    return m[bits];
  if (bits == 0)
    return 0;
  FOR(i, 0, N) {
    if (bits == pow2(i)) {
      ll ans = 0;
      FOR(j, 0, M) {
        if (d[j] <= s[i])
          ans += min(d[j], s[i] - d[j]);
      }
      return m[bits] = ans;
    }
  }
  ll SUM = 0;
  FOR(i, 0, N) {
    if (bits & pow2(i)) {
      SUM += s[i];
    }
  }
  ll ans = LINF;
  FOR(i, 0, N) {
    if ((bits & pow2(i)) == 0)
      continue;
    ll tmpbits = bits ^ pow2(i);
    ll tmp = dfs(tmpbits, N, M, s, d);
    FOR(j, 0, M) {
      if (SUM - s[i] <= d[j] && d[j] <= SUM) {
        tmp += min(abs(d[j] - SUM + s[i]), abs(d[j] - SUM));
      }
    }
    chmin(ans, tmp);
  }
  return m[bits] = ans;
}

ll solve(ll &N, ll &M, vl &s, vl &d) {
  FOR(i, 0, pow2(15)) m[i] = 0;
  ll bits = pow2(N) - 1;
  ll SUM = 0;
  ll ans = LINF;
  FOR(i, 0, N) SUM += s[i];
  FOR(i, 0, N) {
    ll tmpbits = bits ^ pow2(i);
    ll tmp = dfs(tmpbits, N, M, s, d);
    FOR(j, 0, M) {
      if (SUM - s[i] <= d[j]) {
        tmp += min(abs(d[j] - SUM + s[i]), abs(d[j] - SUM));
      }
    }
    chmin(ans, tmp);
  }
  return ans;
}

signed main() {
  ll N, M;
  while (true) {
    cin >> N >> M;
    if (!N && !M)
      return 0;
    vl s(N), d(M);
    FOR(i, 0, N) cin >> s[i];
    FOR(i, 0, M) cin >> d[i];
    cout << solve(N, M, s, d) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
    cout.precision(20);
  }
} fast;

/* define */
#define FOR(I, X, Y) for (long long(I) = (X); (I) < (Y); (I)++)
#define REP(I, X, Y) for (long long(I) = (Y)-1; (I) >= (X); (I)--)
#define ALL(X) (X).begin(), (X).end()
#define pb push_back
#define COUNT(V, X)                                                            \\
  (upper_bound((V).begin(), (V).end(), X) -                                    \\
   lower_bound((V).begin(), (V).end(), X))
#define debug(x) cerr << #x << ':' << x << endl;
#define DEBUG(v)                                                               \\
  {                                                                            \\
    cerr << #v << ':';                                                         \\
    for (auto xv : v)                                                          \\
      cerr << xv << ' ';                                                       \\
    cerr << endl;                                                              \\
  }
#define Yes(X) cout << (X ? "Yes" : "No") << endl;
#define YES(X) cout << (X ? "YES" : "NO") << endl;
#define ctoi(C) (C - '0')
#define pow2(x) ((long long)((long long)1 << x))

/* alias */
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using pi = pair<int, int>;
using pl = pair<long long, long long>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const long long dx[] = {1, 0, -1, 0};
const long long dy[] = {0, 1, 0, -1};
const long long dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long dx9[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
const long long dy9[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int INF = 1000000007;
const long long LINF = 1000000000000000007;

/* func */
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a < b;
}
long long max(long long x, int y) { return max(x, (long long)y); }
long long max(int x, long long y) { return max((long long)x, y); }
long long min(long long x, int y) { return min(x, (long long)y); }
long long min(int x, long long y) { return min((long long)x, y); }

/* liblary */

/* main */

ll dfs(ll bits, ll &N, ll &M, vl &s, vl &d) {
  if (bits == 0)
    return 0;
  FOR(i, 0, N) {
    if (bits == pow2(i)) {
      ll ans = 0;
      FOR(j, 0, M) {
        if (d[j] <= s[i])
          ans += min(d[j], s[i] - d[j]);
      }
      return ans;
    }
  }
  ll SUM = 0;
  FOR(i, 0, N) {
    if (bits & pow2(i)) {
      SUM += s[i];
    }
  }
  ll ans = LINF;
  FOR(i, 0, N) {
    if ((bits & pow2(i)) == 0)
      continue;
    ll tmpbits = bits ^ pow2(i);
    ll tmp = dfs(tmpbits, N, M, s, d);
    FOR(j, 0, M) {
      if (SUM - s[i] <= d[j] && d[j] <= SUM) {
        tmp += min(abs(d[j] - SUM + s[i]), abs(d[j] - SUM));
      }
    }
    chmin(ans, tmp);
  }
  return ans;
}

ll solve(ll &N, ll &M, vl &s, vl &d) {
  ll bits = pow2(N) - 1;
  ll SUM = 0;
  ll ans = LINF;
  FOR(i, 0, N) SUM += s[i];
  FOR(i, 0, N) {
    ll tmpbits = bits ^ pow2(i);
    ll tmp = dfs(tmpbits, N, M, s, d);
    FOR(j, 0, M) {
      if (SUM - s[i] <= d[j]) {
        tmp += min(abs(d[j] - SUM + s[i]), abs(d[j] - SUM));
      }
    }
    chmin(ans, tmp);
  }
  return ans;
}

signed main() {
  ll N, M;
  while (true) {
    cin >> N >> M;
    if (!N && !M)
      return 0;
    vl s(N), d(M);
    FOR(i, 0, N) cin >> s[i];
    FOR(i, 0, M) cin >> d[i];
    cout << solve(N, M, s, d) << endl;
  }
}