alexjercan/bugnet huggingface dataset:
Problem p01840 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  int n, m, t, a[105], b[10010];
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a[i];
    b[max((ll)0, a[i] - m)]++;
    b[min(t, a[i] + m)]--;
  }

  FOR(i, 1, t) b[i] += b[i - 1];
  int ret = 0;
  REP(i, t) if (b[i] == 0) ret++;
  cout << ret << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  int n, m, t, a[105], b[10010];
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a[i];
    b[a[i] - m]++;
    b[a[i] + m]--;
  }

  FOR(i, 1, t) b[i] += b[i - 1];
  int ret = 0;
  REP(i, t) if (b[i] == 0) ret++;
  cout << ret << endl;

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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

struct Node {
  int l, r;
  int num;
  Node() { l = r = -1; }
};

int number(string &s, int &i) {
  int res = 0;
  while (isdigit(s[i])) {
    res *= 10;
    res += s[i] - '0';
    i++;
  }
  return res;
}

int dfs(vector<Node> &node, string &s, int l, int r, int &idx) {
  if (r - l == 0)
    return -1;

  int cur = idx;

  int cnt = 0, i = l;
  while (true) {
    if (s[i] == '(')
      cnt++;
    else if (s[i] == ')')
      cnt--;
    else if (cnt == 0) {
      idx++;
      node[cur].l = dfs(node, s, l + 1, i - 1, idx);
      i++;
      node[cur].num = number(s, i);
      i++;
      idx++;
      node[cur].r = dfs(node, s, i + 1, r - 1, idx);
      break;
    }
    i++;
  }
  return cur;
}

string merge(vector<Node> &x, int xi, vector<Node> &y, int yi) {
  if (xi == -1 or yi == -1)
    return "";

  string left = merge(x, x[xi].l, y, y[yi].l);
  string num = to_string(x[xi].num + y[yi].num);
  string right = merge(x, x[xi].r, y, y[yi].r);

  return "(" + left + ")" + "[" + num + "]" + "(" + right + ")";
}

int main() {
  string a, b;
  cin >> a >> b;

  vector<Node> x(10000), y(10000);
  int idx = 0;
  dfs(x, a, 0, a.size(), idx);
  idx = 0;
  dfs(y, b, 0, b.size(), idx);

  string ans = merge(x, 0, y, 0);
  cout << merge(x, 0, y, 0) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

struct Node {
  int l, r;
  int num;
  Node() { l = r = -1; }
};

int number(string &s, int &i) {
  int res = 0;
  while (isdigit(s[i])) {
    res *= 10;
    res += s[i] - '0';
    i++;
  }
  return res;
}

int dfs(vector<Node> &node, string &s, int l, int r, int &idx) {
  if (r - l == 0)
    return -1;

  int cur = idx;

  int cnt = 0, i = l;
  while (true) {
    if (s[i] == '(')
      cnt++;
    else if (s[i] == ')')
      cnt--;
    else if (cnt == 0) {
      idx++;
      node[cur].l = dfs(node, s, l + 1, i - 1, idx);
      i++;
      node[cur].num = number(s, i);
      i++;
      idx++;
      node[cur].r = dfs(node, s, i + 1, r - 1, idx);
      break;
    }
    i++;
  }
  return cur;
}

string merge(vector<Node> &x, int xi, vector<Node> &y, int yi) {
  if (xi == -1 or yi == -1)
    return "";

  string left = merge(x, x[xi].l, y, y[yi].l);
  string num = to_string(x[xi].num + y[yi].num);
  string right = merge(x, x[xi].r, y, y[yi].r);

  return "(" + left + ")" + "[" + num + "]" + "(" + right + ")";
}

int main() {
  string a, b;
  cin >> a >> b;

  vector<Node> x(200), y(200);
  int idx = 0;
  dfs(x, a, 0, a.size(), idx);
  idx = 0;
  dfs(y, b, 0, b.size(), idx);

  string ans = merge(x, 0, y, 0);
  cout << merge(x, 0, y, 0) << endl;
}
Problem p01841 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

int const INF = 1 << 29;

struct tree;
typedef tree *tree_ptr;

struct tree {
  tree_ptr lch, rch;
  int num;
  tree() : lch(NULL), rch(NULL), num(-INF) {}
};

void consume(string &S, char expected) {
  if (S[0] == expected) {
    S = S.substr(1);
    return;
  }
  cout << S << ", " << expected << endl;
  assert(0);
}

bool consume_if(string &S, char expected) {
  if (S[0] == expected) {
    consume(S, expected);
    return true;
  } else {
    return false;
  }
}

int read_num(string &S) {
  stringstream ss(S);
  int x;
  ss >> x;
  ss >> S;
  return x;
}

void make_tree(string &S, tree_ptr curr) {
  if (consume_if(S, '(')) {
    tree_ptr lch = new (tree);
    curr->lch = lch;
    make_tree(S, lch);
    consume(S, ')');

    consume(S, '[');
    curr->num = read_num(S);
    consume(S, ']');

    consume(S, '(');
    tree_ptr rch = new (tree);
    curr->rch = rch;
    make_tree(S, rch);
    consume(S, ')');
    return;
  }
}

void dfs(tree *ptr1, tree *ptr2) {
  if (ptr1->num == -INF || ptr2->num == -INF) {
    return;
  }

  if (ptr1->lch && ptr2->lch) {
    cout << "(";
    dfs(ptr1->lch, ptr2->lch);
    cout << ")";
  }
  cout << "[" << (ptr1->num + ptr2->num) << "]";
  if (ptr1->rch && ptr2->rch) {
    cout << "(";
    dfs(ptr1->rch, ptr2->rch);
    cout << ")";
  }
}

int main() {

  string a;
  cin >> a;
  string b;
  cin >> b;

  tree_ptr atree = new (tree);
  tree_ptr btree = new (tree);

  make_tree(a, atree);
  make_tree(b, btree);

  dfs(atree, btree);
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  for (int N, M; ~scanf("%d%d", &N, &M) && N;) {
    int sum[N + 1];
    sum[0] = 0;

    int res = 0;
    rep(i, N) {
      int A;
      scanf("%d", &A);
      A %= M;
      sum[i + 1] = sum[i] + A;
      sum[i + 1] %= M;
      maximize(res, sum[i + 1]);
    }

    set<int> st;

    REP(i, 1, N - 1) {
      maximize(res, sum[i] + M - *st.upper_bound(sum[i]));
      st.insert(sum[i]);
    }

    cout << res << endl;
  }

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

#include <cstdio>
#include <cstdlib>
#include <cstring>

typedef struct _tree {
  struct _tree *left, *right;
  int node;
} tree;

char *p;
bool analyze(tree *dst) {
  dst->left = dst->right = NULL;
  switch (*p) {
  case ')':
    p++;
    return false;
  case '(':
    dst->left = (tree *)malloc(sizeof(tree));
    p++;
    if (!analyze(dst->left))
      dst->left = NULL;
    break;
  }
  p++;
  dst->node = 0;
  while ('0' <= *p && *p <= '9')
    dst->node *= 10, dst->node += (*p - '0'), p++;
  p++;
  p++;

  dst->right = (tree *)malloc(sizeof(tree));
  if (!analyze(dst->right))
    dst->right = NULL;
  p++;
  return true;
}

void print_tree(tree *tr) {
  printf("(");
  if (tr->left)
    print_tree(tr->left);
  printf(")");

  printf("[");
  printf("%d", tr->node);
  printf("]");

  printf("(");
  if (tr->right)
    print_tree(tr->right);
  printf(")");
}

void unite(tree *tr1, tree *tr2) {
  (tr1->left && tr2->left)   ? unite(tr1->left, tr2->left),
      tr1->left              : tr1->left = NULL;
  (tr1->right && tr2->right) ? unite(tr1->right, tr2->right),
      tr1->left              : tr1->right = NULL;
  tr1->node += tr2->node;
}

int main(void) {
  char s[2000], t[2000];
  tree st, tt;

  scanf("%s%s", s, t);
  p = s;
  analyze(&st);
  p = t;
  analyze(&tt);

  unite(&st, &tt);

  print_tree(&st);
  puts("");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

int st[1000000], tt[1000000];

char *p;
void analyze(int *dst, int i) {
  switch (*p) {
  case ')':
    p++;
    return;
  case '(':
    p++;
    analyze(dst, i * 2 + 1);
    break;
  }
  p++;
  dst[i] = 0;
  while ('0' <= *p && *p <= '9')
    dst[i] *= 10, dst[i] += (*p - '0'), p++;
  p++;
  p++;

  analyze(dst, i * 2 + 2);
  p++;
}

void print_tree(int *tr, int i) {
  printf("(");
  if (tr[i * 2 + 1] != -1)
    print_tree(tr, i * 2 + 1);
  printf(")");

  printf("[");
  printf("%d", tr[i]);
  printf("]");

  printf("(");
  if (tr[i * 2 + 2] != -1)
    print_tree(tr, i * 2 + 2);
  printf(")");
}

int main(void) {
  memset(st, 0xFF, sizeof(st));
  memset(tt, 0xFF, sizeof(tt));

  char s[2000], t[2000];
  scanf("%s%s", s, t);
  p = s;
  analyze(st, 0);
  p = t;
  analyze(tt, 0);

  for (int i = 0; i < 1000000; i++)
    if (st[i] == -1 || tt[i] == -1)
      st[i] = -1;
    else
      st[i] += tt[i];

  print_tree(st, 0);
  puts("");
  return 0;
}
Problem p01841 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct no {
  no *l, *r;
  int key;
  no() {
    l = r = NULL;
    key = -1;
  };
};
typedef no *node;
string s[2];
node head[2];
int x, p;
int get_num() {
  int res = 0;
  while (p < s[x].size() && isdigit(s[x][p]))
    res = res * 10 + (s[x][p++] - '0');
  return res;
}
node make_tree() {
  node po = new no();
  if (s[x][p] == '(')
    ++p, po->l = make_tree();
  if (isdigit(s[x][p]))
    po->key = get_num();
  if (p < s[x].size() && s[x][p] == '(')
    ++p, po->r = make_tree();
  p++;
  return po;
}
void ch(node n1, node n2, int p) {
  if (p)
    cout << '(';
  if (n1->l != NULL && n2->l != NULL)
    ch(n1->l, n2->l, 1);
  if (n1->key >= 0 && n2->key >= 0)
    cout << '[' << n1->key + n2->key << ']';
  if (n1->r != NULL && n2->r != NULL)
    ch(n1->r, n2->r, 1);
  if (p)
    cout << ')';
}
main() {
  r(i, 2) cin >> s[i];
  r(i, 2) r(j, s[i].size()) if (s[i][j] == ']' || s[i][j] == '[') s[i].erase(
      s[i].begin() + j),
      j--;
  r(i, 2) p = 0, x = i, head[i] = make_tree();
  ch(head[0], head[1], 0);
  cout << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct no {
  long int key;
  no *left, *right;
  no() { left = right = NULL; }
};
typedef no *node;
int n;
string s1[2];
vector<long int> v[2];
void v_make(int t) {
  v[t].push_back('(');
  for (int i = 0; i < s1[t].size(); i++) {
    if (!isdigit(s1[t][i]))
      v[t].push_back(s1[t][i]);
    else {
      int p = s1[t][i] - '0';
      while (isdigit(s1[t][i + 1]))
        p *= 10, p += s1[t][++i] - '0';
      v[t].push_back(p + 100);
    }
  }
  v[t].push_back(')');
}
node con(int l, int r) {
  node po = new no();
  int p = 0, c = -1;
  for (int i = l + 1; i < r; i++)
    if ((char)v[n][i] == '(')
      p++;
    else if ((char)v[n][i] == ')')
      p--;
    else if (v[n][i] >= 100 && !p) {
      c = i;
      po->key = v[n][i] - 100;
      break;
    }
  if (l + 1 != c)
    po->left = con(l + 1, c - 1);
  if (r - 1 != c)
    po->right = con(c + 1, r - 1);
  return po;
}
string make_num(long int p) {
  string t;
  while (p) {
    t += p % 10 + '0';
    p /= 10;
  }
  reverse(t.begin(), t.end());
  return t;
}
string init(node a, node b) {
  string l, r, x = make_num(a->key + b->key);
  if (a->left != NULL && b->left != NULL)
    l = init(a->left, b->left);
  if (a->right != NULL && b->right != NULL)
    r = init(a->right, b->right);
  return "(" + l + ")[" + x + "](" + r + ")";
}
int main() {
  cin >> s1[0] >> s1[1];
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < (int)s1[j].size() - 1; i++)
      if (s1[j][i] == '(' && s1[j][i + 1] == ')')
        s1[j].erase(s1[j].begin() + i), s1[j].erase(s1[j].begin() + i);
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < (int)s1[j].size(); i++)
      if (s1[j][i] == '[' || s1[j][i] == ']')
        s1[j].erase(s1[j].begin() + i);

  v_make(0);
  v_make(1);
  n = 0;
  node a = con(0, v[n].size() - 1);
  n = 1;
  node b = con(0, v[n].size() - 1);
  cout << init(a, b) << endl;
}
Problem p01842 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int const MAX = 51;

int n[2];
int deck[2][MAX];
int sum[2][MAX];

const int IniVal = (1 << 29) + 1234321;
int dp[MAX][MAX][MAX][MAX][2][4];

int rec(int l1, int r1, int l2, int r2, int which, int pass) {

  auto &ret = dp[l1][r1][l2][r2][which][pass];
  if (ret != IniVal)
    return ret;

  if (pass == 3) {
    return ret = 0;
  }

  // pass
  ret = -rec(r2, r2, r1, r1, !which, pass + 1) +
        (sum[which][r1] - sum[which][l1]) - (sum[!which][r2] - sum[!which][l2]);

  if (r1 == n[which])
    return ret;

  if (deck[which][r1] != -1)
    ret = max(ret, -rec(l2, r2, l1, r1 + 1, !which, 0));
  else
    ret = max(ret, -rec(r2, r2, l1, r1 + 1, !which, 0));

  return ret;
}

int main() {

  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      for (int k = 0; k < MAX; k++)
        for (int l = 0; l < MAX; l++)
          for (int a = 0; a < 2; a++)
            for (int b = 0; b < 4; b++)
              dp[i][j][k][l][a][b] = IniVal;

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

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < n[i]; j++)
      cin >> deck[i][j];

  for (int i = 0; i < 2; i++) {
    sum[i][0] = 0;
    for (int j = 0; j < n[i]; j++) {
      int point = deck[i][j];
      if (point == -1)
        point = 0;
      sum[i][j + 1] = sum[i][j] + point;
    }
  }

  cout << rec(0, 0, 0, 0, 0, 0) << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int const MAX = 55;

int n[2];
int deck[2][MAX];
int sum[2][MAX];

const int IniVal = (1 << 29) + 1234321;
int dp[MAX][MAX][MAX][MAX][2][4];

int rec(int l1, int r1, int l2, int r2, int which, int pass) {

  auto &ret = dp[l1][r1][l2][r2][which][pass];
  if (ret != IniVal)
    return ret;

  if (pass == 3) {
    return ret = 0;
  }

  // pass
  ret = -rec(r2, r2, r1, r1, !which, pass + 1) +
        (sum[which][r1] - sum[which][l1]) - (sum[!which][r2] - sum[!which][l2]);

  if (r1 == n[which])
    return ret;

  if (deck[which][r1] != -1)
    ret = max(ret, -rec(l2, r2, l1, r1 + 1, !which, 0));
  else
    ret = max(ret, -rec(r2, r2, l1, r1 + 1, !which, 0));

  return ret;
}

int main() {

  fill(dp[0][0][0][0][0], &dp[MAX][MAX][MAX][MAX][2][4], IniVal);

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

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < n[i]; j++)
      cin >> deck[i][j];

  for (int i = 0; i < 2; i++) {
    sum[i][0] = 0;
    for (int j = 0; j < n[i]; j++) {
      int point = deck[i][j];
      if (point == -1)
        point = 0;
      sum[i][j + 1] = sum[i][j] + point;
    }
  }

  cout << rec(0, 0, 0, 0, 0, 0) << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int r, w, c, R;
  while (cin >> r >> w >> c >> R, r) {
    int ans = (w * c - r) / R;
    if ((w * c - r) % R)
      ++ans;
    if (w * c <= r)
      ans = 0;
    cout << ans << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int r, w, c, R;
  while (cin >> r >> w >> c >> R) {
    int ans = (w * c - r) / R;
    if ((w * c - r) % R)
      ++ans;
    if (w * c <= r)
      ans = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p01845 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int r, w, c, R;
  while (1) {
    int count = 0;
    cin >> r >> w >> c >> R;
    if (r == 0)
      break;
    if (w * c <= r) {
    } else if ((w * c - r) % R == 0) {
      count = (w * c - r) / R;
    } else
      count = (w * c - r) / R + 1;
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int r, w, c, R;
  while (1) {
    int count = 0;
    cin >> r >> w >> c >> R;
    if (r == 0)
      break;
    while (1) {
      if ((double)(r + count * R) / w == c) {
        break;
      } else if ((double)(r + count * R) / w < c) {
        count++;
      } else if ((double)(r + count * R) / w > c) {
        w++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01845 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  double R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R, R0 + W0 + C + R != 0) {
    int ans = 0;
    while (R0 / W0 < C) {
      R0 += R;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  double R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R, R0 + W0 + C + R != 0) {
    int ans = 0;
    while (!(R0 / W0 == C)) {
      if (R0 / W0 > C) {
        W0 += 1.0;
      } else if (R0 / W0 < C) {
        R0 += R;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01845 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int i, y;
  double r, w, c, x;
  while (1) {
    i = 0;
    cin >> r >> w >> c >> x;
    if (r == 0 && w == 0 && c == 0 && x == 0) {
      break;
    }
    y = r / (double)w;
    while (y != c) {
      if (y > c) {
        w = w + 0.1;
      } else if (y < c) {
        r = r + x;
        i++;
      }
      y = r / (double)w;
    }
    cout << i << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  double r, w, c, x, y, i;
  while (1) {
    i = 0;
    cin >> r >> w >> c >> x;
    if (r == 0 && w == 0 && c == 0 && x == 0) {
      break;
    }
    y = r / w;
    while (y != c) {
      if (y > c) {
        w++;
      } else if (y < c) {
        r = r + x;
        i++;
      }
      y = r / w;
    }
    cout << i << endl;
  }
}
Problem p01845 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
using namespace std;
int main() {
  int R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R) {
    if (R0 == 0 && W0 == 0 && C == 0 && R == 0)
      break;
    for (int i = 0;; i++) {
      if (C * W0 <= (R0 + R * i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
using namespace std;
int main() {
  int R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R) {
    int key = 0;
    if (R0 == 0 && W0 == 0 && C == 0 && R == 0)
      break;
    for (int i = 0;; i++) {
      for (int j = 0; j <= R0 + R * i + 1; j++) {
        if ((R0 + R * i) % (C * (W0 + j)) == 0) {
          cout << i << endl;
          key = 1;
          break;
        }
      }
      if (key == 1)
        break;
    }
  }
  return 0;
}
Problem p01845 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, w, c, r1, ans;
  while (1) {
    cin >> r >> w >> c >> r1;
    if (r == 0 && w == 0 && c == 0 && r1 == 0)
      break;
    if (r / w >= c)
      ans = 0;
    else if ((c * w - r) % r1 == 0)
      ans = (c * w - r) / r1;
    else if ((c * w - r) % r1 != 0)
      ans = (c * w - r) / r1 + 1;
    cout << ans << endl;
  }
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, w, c, r1;

  while (1) {
    cin >> r >> w >> c >> r1;
    if (r == 0 && r == 0 && w == 0 && &r1 == 0)
      break;
    if (r / w >= c)
      cout << "0" << endl;
    else if (r / w < c) {
      if ((c * w - r) % r1 == 0)
        cout << (c * w - r) / r1 << endl;
      else if ((c * w - r) % r1 != 0)
        cout << (c * w - r) / r1 + 1 << endl;
    }
  }
  return 0;
}
Problem p01848 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 200
int n, m;
double t[MAX_N];
int G[MAX_N][MAX_N];
bool visited[MAX_N];
int par[MAX_N];

void rdfs(int pos, int si) {
  visited[pos] = true;
  par[pos] = si;
  for (int i = 0; i < n; i++) {
    if (G[i][pos] == 0)
      continue;
    if (visited[i])
      continue;
    rdfs(i, si);
  }
}

void dfs(int pos, vector<int> &v) {
  visited[pos] = true;
  for (int i = 0; i < n; i++) {
    if (G[pos][i] == 0)
      continue;
    if (visited[i])
      continue;
    dfs(i, v);
  }
  v.push_back(pos);
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    memset(G, 0, sizeof(G));
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        a--;
        G[i][a] = 1;
      }
    }

    vector<int> v;
    memset(visited, false, sizeof(visited));
    for (int i = 0; i < n; i++) {
      if (visited[i])
        continue;
      dfs(i, v);
    }

    memset(visited, false, sizeof(visited));
    reverse(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      int si = v[i];
      if (visited[si])
        continue;
      rdfs(si, si);
    }

    vector<bool> flg(n, false);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (G[i][j] == 0)
          continue;
        if (par[i] != par[j]) {
          flg[par[j]] = true;
        }
      }
    }

    double ans = 1.0;
    for (int i = 0; i < n; i++) {
      if (par[i] != i)
        continue;
      if (flg[i])
        continue;
      double p = 1.0;
      for (int j = 0; j < n; j++) {
        if (par[j] != i)
          continue;
        p *= t[j];
      }
      ans *= (1.0 - p);
    }

    printf("%.10f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1e9)
int N, M;
int s[15];
int d[100];
int sum[(1 << 15)];
int dp[(1 << 15)];

void init() {
  for (int i = 0; i < (1 << N); i++) {
    sum[i] = 0;
    dp[i] = 0;
  }
}

int solve() {
  for (int i = 0; i < M; i++) {
    for (int bit = 0; bit < (1 << N); bit++) {
      for (int j = 0; j < N; j++) {
        int nbit = bit | (1 << j);
        dp[nbit] = min(dp[nbit], dp[bit]);
      }
      dp[bit] += abs(sum[bit] - d[i]);
    }
  }
  int ans = INF;
  for (int bit = 0; bit < (1 << N); bit++) {
    ans = min(ans, dp[bit]);
  }
  return ans;
}

int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    init();
    for (int i = 0; i < N; i++)
      cin >> s[i];
    for (int i = 0; i < M; i++)
      cin >> d[i];
    sort(d, d + M);
    for (int bit = 0; bit < (1 << N); bit++)
      for (int i = 0; i < N; i++)
        if (bit >> i & 1)
          sum[bit] += s[i];
    cout << solve() << endl;
  }
  return 0;
}