alexjercan/bugnet huggingface dataset:
Problem p01192 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int b[60];
int dp[1010000];
int main() {
  int a;
  int tc = 0;
  while (scanf("%d", &a), a) {
    tc++;
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    int m = b[a - 1] * b[a - 1] + 100;
    m = min(m, 100000);
    if (b[0] != 1) {
      printf("Case #%d: Cannot pay some amount\\n", tc);
      continue;
    }
    for (int i = 0; i < m; i++)
      dp[i] = i;
    for (int i = 1; i < a; i++) {
      for (int j = 0; j < m - b[i]; j++) {
        dp[j + b[i]] = min(dp[j + b[i]], dp[j] + 1);
      }
    }
    bool ok = true;
    for (int i = 0; i < m; i++) {
      int s = i;
      int t = 0;
      for (int j = a - 1; j >= 0; j--) {
        t += s / b[j];
        s %= b[j];
      }
      if (t > dp[i]) {
        ok = false;
        break;
      }
    }
    if (ok)
      printf("Case #%d: OK\\n", tc);
    else
      printf("Case #%d: Cannot use greedy algorithm\\n", tc);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int b[60];
int dp[1010000];
int t[1010000];
int main() {
  int a;
  int tc = 0;
  while (scanf("%d", &a), a) {
    tc++;
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    int m = b[a - 1] * b[a - 1] + 100;
    m = min(m, 100000);
    if (b[0] != 1) {
      printf("Case #%d: Cannot pay some amount\\n", tc);
      continue;
    }
    bool ok = true;
    for (int i = 0; i < m; i++) {
      int s = i;
      t[i] = 0;
      for (int j = a - 1; j >= 0; j--) {
        t[i] += s / b[j];
        s %= b[j];
      }
      //		if(t>dp[i]){ok=false;break;}
    }
    for (int i = 0; i < m; i++)
      dp[i] = i;
    for (int i = 1; i < a; i++) {
      for (int j = 0; j < m - b[i]; j++) {
        dp[j + b[i]] = min(dp[j + b[i]], dp[j] + 1);
        if (t[j + b[i]] > dp[j + b[i]]) {
          ok = false;
          break;
        }
      }
      if (!ok)
        break;
    }
    if (ok)
      printf("Case #%d: OK\\n", tc);
    else
      printf("Case #%d: Cannot use greedy algorithm\\n", tc);
  }
}
Problem p01192 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
int c[100];

int greedy(int x) {
  int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += x / c[i];
    x %= c[i];
  }
  return res;
}

int dp[5000];

void solve() {
  if (c[0] != 1) {
    printf("Cannot pay some amount");
    return;
  }

  fill(dp, dp + 5000, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    int val = c[i];
    for (int j = 0; j < 2000; j++) {
      if (j - val >= 0)
        dp[j] = min(dp[j], dp[j - val] + 1);
    }
  }

  for (int num = 0; num < 2000; num++) {
    int cost = greedy(num);
    if (cost != dp[num]) {
      //      cout<<num<<' '<<cost<<' '<<dp[num]<<endl;
      printf("Cannot use greedy algorithm");
      return;
    }
  }

  for (int j = 2000; j < 5000; j++) {

    for (int i = 0; i < n; i++) {
      int val = c[i];
      dp[j] = min(dp[j], dp[j - val] + 1);
    }

    if (dp[j] != dp[j - c[n - 1]] + 1) {
      printf("Cannot use greedy algorithm");
      return;
    }
  }
  printf("OK");
}

int main() {
  for (int tc = 1;; tc++) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &c[i]);
    printf("Case #%d: ", tc);
    solve();
    printf("\\n");
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
int c[100];

int greedy(int x) {
  int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += x / c[i];
    x %= c[i];
  }
  return res;
}

int dp[5000];

void solve() {
  if (c[0] != 1) {
    printf("Cannot pay some amount");
    return;
  }

  fill(dp, dp + 5000, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    int val = c[i];
    for (int j = 0; j < 2000; j++) {
      if (j - val >= 0)
        dp[j] = min(dp[j], dp[j - val] + 1);
    }
  }

  for (int num = 0; num < 2000; num++) {
    int cost = greedy(num);
    if (cost != dp[num]) {
      //      cout<<num<<' '<<cost<<' '<<dp[num]<<endl;
      printf("Cannot use greedy algorithm");
      return;
    }
  }

  for (int j = 2000; j < 5000; j++) {

    for (int i = 0; i < n; i++) {
      int val = c[i];
      dp[j] = min(dp[j], dp[j - val] + 1);
    }

    if (dp[j] != dp[j - c[n - 2]] + 1) {
      printf("Cannot use greedy algorithm");
      return;
    }
  }
  printf("OK");
}

int main() {
  for (int tc = 1;; tc++) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &c[i]);
    printf("Case #%d: ", tc);
    solve();
    printf("\\n");
  }
  return 0;
}
Problem p01192 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  const int N = 2000;
  for (int n, t = 1; cin >> n, n; t++) {
    int dp[N + 1];
    fill(begin(dp), end(dp), 1 << 29);
    dp[0] = 0;
    int u[N + 1] = {};
    bool o = false;
    while (n--) {
      int c;
      cin >> c;
      o |= c == 1;
      for (int i = c; i <= N; i++) {
        dp[i] = min(dp[i], dp[i - c] + 1);
        u[i] = c;
      }
    }
    bool f = false;
    for (int i = 1; i <= N; i++) {
      f |= dp[i] - dp[i - u[i]] != 1;
    }
    cout << "Case #" << t << ": "
         << (!o  ? "Cannot pay some amount"
             : f ? "Cannot use greedy algorithm"
                 : "OK")
         << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  for (int n, t; cin >> n, n; t++) {
    static int dp[1000001];
    fill(begin(dp), end(dp), 1 << 29);
    dp[0] = 0;
    static int u[1000001];
    bool o = false;
    while (n--) {
      int c;
      cin >> c;
      o |= c == 1;
      for (int i = c; i <= 1000000; i++) {
        dp[i] = min(dp[i], dp[i - c] + 1);
        u[i] = c;
      }
    }
    bool f = false;
    for (int i = 1; i <= 1000000; i++) {
      f |= dp[i] != dp[i - u[i]] + 1;
    }
    cout << "Case #" << t << ": "
         << (!o  ? "Cannot pay some amount"
             : f ? "Cannot use greedy algorithm"
                 : "OK")
         << endl;
  }
}
Problem p01192 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

int n, c[100];
int dp1[1000000], dp2[1000000];

int main() {
  for (int T = 1; scanf("%d", &n), n; T++) {
    rep(i, n) scanf("%d", c + i);

    if (c[0] != 1) {
      printf("Case #%d: Cannot pay some amount\\n", T);
      continue;
    }

    const int L = 100 * c[n - 1];
    dp1[0] = 0;
    fill(dp1 + 1, dp1 + L, INF);
    rep(k, n) for (int p = c[k]; p < L; p++) dp1[p] =
        min(dp1[p], dp1[p - c[k]] + 1);

    dp2[0] = 0;
    fill(dp2 + 1, dp2 + L, INF);
    rep(p, L) for (int k = n - 1; k >= 0; k--) {
      if (p - c[k] >= 0) {
        dp2[p] = dp2[p - c[k]] + 1;
        break;
      }
    }

    bool ok = true;
    rep(i, L) if (dp1[i] < dp2[i]) {
      ok = false;
      break;
    }
    printf("Case #%d: %s\\n", T, ok ? "OK" : "Cannot use greedy algorithm");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

int n, c[100];

int greedy(int p) {
  int cnt = 0;
  for (int i = n - 1; i >= 0 && p > 0; i--) {
    while (p >= c[i])
      p -= c[i], cnt++;
  }
  return cnt;
}

int dp1[1000000], dp2[1000000];

int main() {
  for (int T = 1; scanf("%d", &n), n; T++) {
    rep(i, n) scanf("%d", c + i);

    if (c[0] != 1) {
      printf("Case #%d: Cannot pay some amount\\n", T);
      continue;
    }

    const int L = c[n - 1] * c[n - 1];
    dp1[0] = 0;
    fill(dp1 + 1, dp1 + L, INF);
    rep(k, n) for (int p = c[k]; p < L; p++) dp1[p] =
        min(dp1[p], dp1[p - c[k]] + 1);

    dp2[0] = 0;
    fill(dp2 + 1, dp2 + L, INF);
    rep(p, L) for (int k = n - 1; k >= 0; k--) {
      if (p - c[k] >= 0) {
        dp2[p] = dp2[p - c[k]] + 1;
        break;
      }
    }

    bool ok = true;
    rep(i, L) if (dp1[i] < dp2[i]) {
      ok = false;
      break;
    }
    printf("Case #%d: %s\\n", T, ok ? "OK" : "Cannot use greedy algorithm");
  }

  return 0;
}
Problem p01192 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

int main() {
  for (int d = 1;; ++d) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> c(n);
    for (int i = 0; i < n; ++i)
      cin >> c[i];

    if (c[0] != 1) {
      cout << "Case #" << d << ": Cannot pay some amount" << endl;
      continue;
    }

    vector<int> dp(10000, INF);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 10000; ++j) {
        int k = j + c[i];
        if (k < 10000 && dp[j] + 1 < dp[k])
          dp[k] = dp[j] + 1;
      }
    }

    bool ok = true;
    for (int i = 0; i < 10000; ++i) {
      int rest = i;
      int coin = 0;
      for (int j = n - 1; j >= 0; --j) {
        coin += rest / c[j];
        rest %= c[j];
      }
      if (coin > dp[i])
        ok = false;
    }

    if (ok)
      cout << "Case #" << d << ": OK" << endl;
    else
      cout << "Case #" << d << ": Cannot use greedy algorithm" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

int main() {
  for (int d = 1;; ++d) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> c(n);
    for (int i = 0; i < n; ++i)
      cin >> c[i];

    if (c[0] != 1) {
      cout << "Case #" << d << ": Cannot pay some amount" << endl;
      continue;
    }

    vector<int> dp(100000, INF);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 100000; ++j) {
        int k = j + c[i];
        if (k < 100000 && dp[j] + 1 < dp[k])
          dp[k] = dp[j] + 1;
      }
    }

    bool ok = true;
    for (int i = 0; i < 100000; ++i) {
      int rest = i;
      int coin = 0;
      for (int j = n - 1; j >= 0; --j) {
        coin += rest / c[j];
        rest %= c[j];
      }
      if (coin > dp[i])
        ok = false;
    }

    if (ok)
      cout << "Case #" << d << ": OK" << endl;
    else
      cout << "Case #" << d << ": Cannot use greedy algorithm" << endl;
  }
}
Problem p01192 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;
const int N = 2000;
int dp[N], memo[N], n, c[50];
int F(int x) {
  if (memo[x] >= 0)
    return memo[x];
  int y = upper_bound(c, c + n, x)[-1];
  return memo[x] = 1 + F(x - y);
}
int main() {
  int i, j, T, m;
  for (T = 1; scanf("%d", &n), n; ++T) {
    fill(memo, memo + N, -1);
    fill(dp, dp + N, INT_MAX);
    memo[0] = dp[0] = 0;
    for (i = 0; i < n; ++i)
      scanf("%d", c + i);
    m = 2 * c[n - 1] + 1;
    printf("Case #%d: ", T);
    if (c[0] - 1)
      puts("Cannot pay some amount");
    else {
      for (i = 0; i < m; ++i)
        F(i);
      for (i = 0; i < n; ++i)
        for (j = 0; j < m - c[i]; ++j)
          dp[c[i] + j] = min(dp[c[i] + j], dp[j] + 1);
      if (equal(memo, memo + m, dp))
        puts("OK");
      else
        puts("Cannot use greedy algorithm");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;
const int N = 1000000;
int dp[N], memo[N], n, c[50];
int F(int x) {
  if (memo[x] >= 0)
    return memo[x];
  int y = upper_bound(c, c + n, x)[-1];
  return memo[x] = 1 + F(x - y);
}
int main() {
  int i, j, T, m;
  for (T = 1; scanf("%d", &n), n; ++T) {
    fill(memo, memo + N, -1);
    fill(dp, dp + N, INT_MAX);
    memo[0] = dp[0] = 0;
    for (i = 0; i < n; ++i)
      scanf("%d", c + i);
    m = c[n - 1] * c[n - 1] + 1;
    printf("Case #1: ", T);
    if (c[0] - 1)
      puts("Cannot pay some amount");
    else {
      for (i = 0; i < m; ++i)
        F(i);
      for (i = 0; i < n; ++i)
        for (j = 0; j < m - c[i]; ++j)
          dp[c[i] + j] = min(dp[c[i] + j], dp[j] + 1);
      if (equal(memo, memo + m, dp))
        puts("OK");
      else
        puts("Cannot use greedy algorithm");
    }
  }
  return 0;
}
Problem p01193 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int r1, r2;
  char r3;
  string str, fm;
  bool f;

  while (cin >> fm) {
    r1 = r2 = 0;
    r3 = ' ';
    f = true;
    str = "";
    for (int i = 0; f && i < fm.size(); ++i) {
      if (isdigit(fm[i])) {
        str += fm[i];
      } else {
        r2 = atoi(str.c_str());
        str = "";

        if (r3 == ' ')
          r1 = r2;
        else if (r3 == '+')
          r1 += r2;
        else if (r3 == '-')
          r1 -= r2;
        else if (r3 == '*')
          r1 *= r2;
        else if (r3 == '=')
          break;

        if (r1 < 0 || r2 < 0 || r2 >= 10000 || r1 >= 10000)
          f = false;
        r3 = fm[i];
      }
    }

    if (!f)
      cout << "E" << endl;
    else
      cout << r1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

long long int r1, r2;
char r3;
string fm;

int main() {
  while (cin >> fm) {
    r1 = r2 = 0;
    stringstream ss(fm);
    ss >> r1;

    while (!ss.eof()) {
      ss >> r3;
      if (r3 == '=') {
        if (r1 <= 0 || r1 >= 10000)
          cout << "E" << endl;
        else
          cout << r1 << endl;
        break;
      }
      ss >> r2;

      if (r3 == '+')
        r1 += r2;
      else if (r3 == '-')
        r1 -= r2;
      else
        r1 *= r2;

      if (r1 <= 0 || r1 >= 10000) {
        cout << 'E' << endl;
        break;
      }
    }
  }

  return 0;
}
Problem p01193 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define P int
using namespace std;
string s;
int p, f;
P bns1();
P g_A() {
  P r = 0;
  if (s[p] == '(') {
    p++;
    r = bns1();
    p++;
  } else {
    while (isdigit(s[p])) {
      r = r * 10 + (s[p] - '0');
      p++;
    }
  }
  if (r < 0 || r > 9999)
    f++;
  return r;
}
P bns1() {
  P res = g_A();
  while (!isdigit(s[p]) && s[p] != '=') {
    int t = p++;
    if (s[t] == '+')
      res += g_A();
    if (s[t] == '-')
      res -= g_A();
    if (s[t] == '*')
      res *= g_A();
    if (res < 0 || res > 9999)
      f++;
  }
  return res;
}
int main() {
  while (cin >> s) {
    p = f = 0;
    int ans = bns1();
    if (f)
      cout << 'E' << endl;
    else
      cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define P int
using namespace std;
string s;
int p, f;
P bns1();
P g_A() {
  P r = 0;
  if (s[p] == '(') {
    p++;
    r = bns1();
    p++;
  } else {
    while (isdigit(s[p])) {
      r = r * 10 + (s[p] - '0');
      p++;
    }
  }
  return r;
}
P bns1() {
  P res = g_A();
  while (!isdigit(s[p])) {
    int t = p++;
    if (s[t] == '+')
      res += g_A();
    if (s[t] == '-')
      res -= g_A();
    if (s[t] == '*')
      res *= g_A();
    if (res < 1 || res > 9999)
      f++;
  }
  return res;
}
int main() {
  while (cin >> s) {
    s.erase(s.end() - 1);
    p = f = 0;
    int ans = bns1();
    if (f)
      cout << 'E' << endl;
    else
      cout << ans << endl;
  }
}
Problem p01202 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  for (int p = 0; p < n; p++) {
    string input;
    cin >> input;

    bool firstIsNatural = true;
    for (int i = 0; i < input.size(); i++) {
      if (i % 2 == 0 && input.substr(i, 1) == "L")
        firstIsNatural = false;
      else if (i % 2 == 1 && input.substr(i, 1) == "R")
        firstIsNatural = false;

      if (i != 0)
        if (input[i] == input[i - 1])
          firstIsNatural = false;
    }
    bool secondIsNatural = true;
    for (int i = 0; i < input.size(); i++) {
      if (i % 2 == 1 && input.substr(i, 1) == "L")
        secondIsNatural = false;
      else if (i % 2 == 2 && input.substr(i, 1) == "R")
        secondIsNatural = false;
      if (i != 0)
        if (input[i] == input[i - 1])
          secondIsNatural = false;
    }
    if (firstIsNatural || secondIsNatural)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  for (int p = 0; p < n; p++) {
    string input;
    cin >> input;

    bool isNatural = true;
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0 && input.substr(i, 1) == "L")
        isNatural = false;
      else if (i % 2 == 1 && input.substr(i, 1) == "R")
        isNatural = false;

      if (i != 0)
        if (input[i] == input[i - 1])
          isNatural = false;
    }
    if (isNatural)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p01203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int n, m;
int bs[20];
int as[20];
int dp[1 << 14];
int combSum[1 << 14];
const int INF = 1000000000;

int main() {
  int tmp[20];
  while (scanf("%d %d", &n, &m) && (n | m)) {
    memset(combSum, 0, sizeof(combSum));
    for (int i = 0; i < n; i++)
      scanf("%d %d", bs + i, as + i);
    for (int mask = 1; mask < (1 << n); mask++)
      for (int i = 0; i < n; i++)
        if ((mask >> i) & 1)
          combSum[mask] += as[i];
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
      int res = INF;
      if (mask == (1 << n) - 1)
        res = 0;
      else {
        int cemp = m;
        int sz = 0;
        for (int i = 0; i < n; i++) {
          if ((mask >> i) & 1)
            cemp += bs[i] - as[i];
          else
            tmp[sz++] = i;
        }
        int revMask = (~mask) & ((1 << n) - 1);
        int sub = revMask;
        do {
          int nmask = mask | (sub);
          int sum = combSum[sub];
          if (nmask != mask && sum <= cemp)
            res = min(res, dp[nmask] + 1);
          sub = (sub - 1) & revMask;
        } while (sub != revMask);
      }
      dp[mask] = res;
    }
    if (dp[0] == INF)
      printf("Impossible\\n");
    else
      printf("%d\\n", dp[0]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int n, m;
int bs[20];
int as[20];
int dp[1 << 14];
const int INF = 1000000000;

int main() {
  // std::ios_base::sync_with_stdio(0);
  int tmp[20];
  // while(cin>>n>>m&&(n|m)){
  while (scanf("%d %d", &n, &m) && (n | m)) {
    for (int i = 0; i < n; i++) {
      // cin>>bs[i]>>as[i];
      scanf("%d %d", bs + i, as + i);
    }
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
      // cout<<mask<<endl;
      int res = INF;
      if (mask == (1 << n) - 1)
        res = 0;
      else {
        int cemp = m;
        int sz = 0;
        for (int i = 0; i < n; i++) {
          if ((mask >> i) & 1)
            cemp += bs[i] - as[i];
          else
            tmp[sz++] = i;
        }
        int revMask = (~mask) & ((1 << n) - 1);
        int sub = revMask;
        do {
          int nmask = mask;
          int sum = 0;
          for (int i = 0; i < n; i++) {
            if ((sub >> i) & 1) {
              nmask |= (1 << i);
              sum += as[i];
            }
          }
          if (nmask != 0 && sum <= cemp)
            res = min(res, dp[nmask] + 1);
          sub = (sub - 1) & revMask;
        } while (sub != revMask);
        // for(int s=1;s<(1<<sz);s++){
        //   int nmask=mask;
        //   int sum=0;
        //   for(int i=0;i<sz;i++){
        //     if((s>>i)&1){
        //       nmask|=(1<<tmp[i]);
        //       sum+=as[tmp[i]];
        //     }
        //   }
        //   if(sum<=cemp)res=min(res,dp[nmask]+1);
        // }
      }
      dp[mask] = res;
    }
    if (dp[0] == INF)
      printf("Impossible\\n");
    else
      printf("%d\\n", dp[0]);
  }

  return 0;
}
Problem p01203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

template <size_t T> bool prevSubset(bitset<T> &bs, const bitset<T> &mask) {
  if (bs.none())
    return false;
  bs = bs.to_ulong() - 1ull;
  bs &= mask;
  return true;
}

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    vector<int> use(1 << n, 0);
    for (int i = 0; i < (1 << n); ++i) {
      bitset<14> bs(i);
      for (int j = 0; j < n; ++j) {
        if (bs[j])
          use[i] += b[j];
      }
    }

    vector<int> dp(1 << n, INF);
    dp[(1 << n) - 1] = 0;
    for (int i = (1 << n) - 1; i >= 0; --i) {
      bitset<14> mask(i);
      int space = m;
      for (int j = 0; j < n; ++j) {
        if (!mask[j])
          space += a[j] - b[j];
      }

      bitset<14> bs(i);
      do {
        if (use[bs.to_ulong()] <= space) {
          bitset<14> bs2 = mask & ~bs;
          dp[bs2.to_ulong()] = min(dp[bs2.to_ulong()], dp[i] + 1);
        }
      } while (prevSubset(bs, mask));
    }

    if (dp[0] == INF)
      cout << "Impossible" << endl;
    else
      cout << dp[0] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

template <size_t T> bool prevSubset(bitset<T> &bs, const bitset<T> &mask) {
  if (bs.none())
    return false;
  bs = bs.to_ulong() - 1ull;
  bs &= mask;
  return true;
}

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    vector<int> dp(1 << n, INF);
    dp[(1 << n) - 1] = 0;
    for (int i = (1 << n) - 1; i >= 0; --i) {
      bitset<14> mask(i);
      int space = m;
      for (int j = 0; j < n; ++j) {
        if (!mask[j])
          space += a[j] - b[j];
      }

      bitset<14> bs(i);
      do {
        int use = 0;
        for (int j = 0; j < n; ++j) {
          if (bs[j])
            use += b[j];
        }
        bitset<14> bs2 = mask & ~bs;
        if (use <= space)
          dp[bs2.to_ulong()] = min(dp[bs2.to_ulong()], dp[i] + 1);
      } while (prevSubset(bs, mask));
    }

    if (dp[0] == INF)
      cout << "Impossible" << endl;
    else
      cout << dp[0] << endl;
  }
}