prithivMLmods/Coder-Stat huggingface dataset:
Problem p00248 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<int> G[10000];
bool used[100000];

bool dfs(int v, int p) {
  used[v] = true;
  bool ret = true;

  for (int i = 0; i < G[v].size(); i++) {
    int to = G[v][i];
    if (to == p)
      continue;
    if (used[to])
      return false;
    ret &= dfs(to, v);
  }
  return ret;
}

int main() {
  while (cin >> N >> M, N || M) {
    fill_n(G, N, vector<int>());
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    if (M >= N) {
      cout << "no" << endl;
      continue;
    }
    fill_n(used, N, false);
    bool ret = true;

    for (int i = 0; i < N; i++) {
      if (G[i].size() > 2)
        ret = false;
      if (G[i].size() >= 2)
        continue;
      if (used[i])
        continue;
      ret &= dfs(i, -1);
    }

    for (int i = 0; i < N; i++)
      if (!used[i])
        ret = false;
    if (ret)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

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

int N, M;
vector<int> G[100000];
bool used[100000];

bool dfs(int v, int p) {
  used[v] = true;
  bool ret = true;

  for (int i = 0; i < G[v].size(); i++) {
    int to = G[v][i];
    if (to == p)
      continue;
    if (used[to])
      return false;
    ret &= dfs(to, v);
  }
  return ret;
}

int main() {
  while (cin >> N >> M, N || M) {
    fill_n(G, N, vector<int>());
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    if (M >= N) {
      cout << "no" << endl;
      continue;
    }
    fill_n(used, N, false);
    bool ret = true;

    for (int i = 0; i < N; i++) {
      if (G[i].size() > 2)
        ret = false;
      if (G[i].size() >= 2)
        continue;
      if (used[i])
        continue;
      ret &= dfs(i, -1);
    }

    for (int i = 0; i < N; i++)
      if (!used[i])
        ret = false;
    if (ret)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p00250 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> scone(n);
    for (auto &i : scone)
      cin >> i;

    vector<int> sum = {0};
    for (auto i : scone)
      sum.push_back((sum.back() + (i % m)) % m);

    int answer = 0;
    set<int> used = {m};
    for (int j = 1; j < sum.size(); ++j) {
      answer = max(
          answer,
          (sum[j] - *lower_bound(begin(used), end(used), sum[j]) + m + m) % m);
      used.insert(sum[j]);
      used.insert(sum[j] + m);
    }
    cout << answer << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> scone(n);
    for (auto &i : scone)
      cin >> i;

    vector<int> sum = {0};
    for (auto i : scone)
      sum.push_back((sum.back() + (i % m)) % m);

    int answer = 0;
    set<int> used = {m};
    for (int j = 1; j < sum.size(); ++j) {
      answer = max(answer, (sum[j] - *used.lower_bound(sum[j]) + m + m) % m);
      used.insert(sum[j]);
      used.insert(sum[j] + m);
    }
    cout << answer << endl;
  }
}
Problem p00252 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int b1, b2, b3;
  scanf("%d %d %d", b1, b2, b3);
  if (b1 == 0 && b2 == 0 && b3 == 1)
    printf("Open\n");
  else
    printf("Close\n");
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int b1, b2, b3;
  scanf("%d %d %d", &b1, &b2, &b3);
  if (b1 == 1 && b2 == 1 || b3 == 1)
    printf("Open\n");
  else
    printf("Close\n");
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int x, n, h[101], r[10], s[99], i, j, flg;
  while (1) {
    flg = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n + 1; i++) {
      scanf("%d", &h[i]);
    }
    for (i = 0; i < n + 1; i++) {
      for (j = 0; j < n; j++) {
        if (j >= i) {
          r[j] = h[j + 1];
        } else {
          r[j] = h[j];
        }
      }
      for (j = 0; j < n - 1; j++) {
        s[j] = r[j + 1] - r[j];
      }
      for (j = 0; j < n - 2; j++) {
        if (s[j] != s[j + 1]) {
          flg = 0;
          break;
        }
        if (s[j] == s[j + 1]) {
          flg = 1;
        }
      }
      if (flg == 1) {
        x = h[i];
        i = 1000;
      }
    }
    printf("%d\n", x);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int x, n, h[101], r[100], s[99], i, j, flg;
  while (1) {
    flg = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n + 1; i++) {
      scanf("%d", &h[i]);
    }
    for (i = 0; i < n + 1; i++) {
      for (j = 0; j < n; j++) {
        if (j >= i) {
          r[j] = h[j + 1];
        } else {
          r[j] = h[j];
        }
      }
      for (j = 0; j < n - 1; j++) {
        s[j] = r[j + 1] - r[j];
      }
      for (j = 0; j < n - 2; j++) {
        if (s[j] != s[j + 1]) {
          flg = 0;
          break;
        }
        if (s[j] == s[j + 1]) {
          flg = 1;
        }
      }
      if (flg == 1) {
        x = h[i];
        i = 1000;
      }
    }
    printf("%d\n", x);
  }
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, a, c, j;
  int b[105];
  int s[105];
  int i = 0;
  while (cin >> n && n) {
    n++;
    for (j = 0; j < n; j++) {
      cin >> b[j];
    }
    c = b[1] - b[0];
    for (j = 1; j < n - 1; j++) {
      if (c != b[j + 1] - b[j]) {
        if (j == 1) {
          c = b[4] - b[3];
          a = b[1] - b[0];
          if (c != a) {
            if (c == b[2] - b[0]) {
              j = 0;
            } else {
              j = -1;
            }
          }
        }
        if (j == 2) {
          if (b[2] - b[0] == b[4] - b[3]) {
            j = 0;
          }
        }
        j++;
        break;
      }
    }
    s[i] = b[j];
    i++;
  }
  for (j = 0; j < i; j++) {
    cout << s[j] << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, a, c, j;
  int b[105];
  int s[505];
  int i = 0;
  while (cin >> n && n) {
    n++;
    for (j = 0; j < n; j++) {
      cin >> b[j];
    }
    c = b[1] - b[0];
    for (j = 1; j < n - 1; j++) {
      if (c != b[j + 1] - b[j]) {
        if (j == 1) {
          c = b[4] - b[3];
          a = b[1] - b[0];
          if (c != a) {
            if (c == b[2] - b[0]) {
              j = 0;
            } else {
              j = -1;
            }
          }
        }
        if (j == 2) {
          if (b[2] - b[0] == b[4] - b[3]) {
            j = 0;
          }
        }
        j++;
        break;
      }
    }
    s[i] = b[j];
    i++;
  }
  for (j = 0; j < i; j++) {
    cout << s[j] << endl;
  }
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int n, t, f;
  int h[101];
  for (;;) {
    scanf("%d", &n);
    n++;
    rep(i, n) scanf("%d", i + h);
    rep(i, n) {
      f = 1;
      t = h[1 + (i < 2)] - h[i == 0];
      rep(j, n - 2) {
        if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
          f = 0;
          break;
        }
      }
      if (f) {
        printf("%d\n", h[i]);
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int n, t, f;
  int h[101];
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    n++;
    rep(i, n) scanf("%d", i + h);
    rep(i, n) {
      f = 1;
      t = h[1 + (i < 2)] - h[i == 0];
      rep(j, n - 2) {
        if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
          f = 0;
          break;
        }
      }
      if (f) {
        printf("%d\n", h[i]);
        break;
      }
    }
  }
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int n, h[100];
int ans[500], cor = 0;
int main() {
  int i, j;
  while (cin >> n) {
    if (n == 0)
      break;
    n++;
    for (i = 0; i < n; i++)
      cin >> h[i];
    // 雑草を抜いたら等差数列になるかも…
    int before, now, d;
    bool flag;
    for (i = 0; i < n; i++) {
      flag = false;
      for (j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (flag == false) {
          flag = true;
          before = h[j];
          if (i != j + 1)
            d = h[j + 1] - h[j];
          else
            d = h[j + 2] - h[j];
        } else {
          now = h[j];
          if (now - before != d)
            break;
          before = now;
        }
      }
      if (j == n) {
        ans[cor] = h[i];
        break;
      }
    }
    cor++;
  }
  for (i = 0; i < cor; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int n, h[101];
int ans[500], cor = 0;
int main() {
  int i, j;
  while (cin >> n) {
    if (n == 0)
      break;
    n++;
    for (i = 0; i < n; i++)
      cin >> h[i];
    // 雑草を抜いたら等差数列になるかも…
    int before, now, d;
    bool flag;
    for (i = 0; i < n; i++) {
      flag = false;
      for (j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (flag == false) {
          flag = true;
          before = h[j];
          if (i != j + 1)
            d = h[j + 1] - h[j];
          else
            d = h[j + 2] - h[j];
        } else {
          now = h[j];
          if (now - before != d)
            break;
          before = now;
        }
      }
      if (j == n) {
        ans[cor] = h[i];
        break;
      }
    }
    cor++;
  }
  for (i = 0; i < cor; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
int H[100];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> N) {
    if (N == 0)
      break;
    N++;
    rep(i, N) cin >> H[i];
    int w = -1;
    rep(i, N) {
      vector<int> xs;
      rep(j, i) xs.pb(H[j]);
      for (int j = i + 1; j < N; j++)
        xs.pb(H[j]);
      int diff = xs[1] - xs[0];
      bool f = true;
      rep(i, (int)xs.size() - 1) {
        if (diff != xs[i + 1] - xs[i])
          f = false;
      }
      if (f) {
        w = H[i];
        break;
      }
    }
    cout << w << "\n";
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
int H[101];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> N) {
    if (N == 0)
      break;
    N++;
    rep(i, N) cin >> H[i];
    int w = -1;
    rep(i, N) {
      vector<int> xs;
      rep(j, i) xs.pb(H[j]);
      for (int j = i + 1; j < N; j++)
        xs.pb(H[j]);
      int diff = xs[1] - xs[0];
      bool f = true;
      rep(i, (int)xs.size() - 1) {
        if (diff != xs[i + 1] - xs[i])
          f = false;
      }
      if (f) {
        w = H[i];
        break;
      }
    }
    cout << w << "\n";
  }
  return 0;
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  for (int n; cin >> n && n;) {
    vector<int> l(n);
    for (int i = 0; i <= n; i++)
      cin >> l[i];
    for (int s = 0; s <= n; s++) {
      int diff = INT_MAX;
      for (int i = s ? 1 : 2; i <= n; i++) {
        if (i == s)
          continue;
        int be = i == s + 1 ? 2 : 1;
        if (diff == INT_MAX) {
          diff = l[i] - l[i - be];
        } else if (diff != l[i] - l[i - be]) {
          goto BREAK;
        }
      }
      cout << l[s] << endl;
      break;
    BREAK:;
    }
  }
}

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

int main() {
  for (int n; cin >> n && n;) {
    vector<int> l(n + 1);
    for (int i = 0; i <= n; i++)
      cin >> l[i];
    for (int s = 0; s <= n; s++) {
      int diff = INT_MAX;
      for (int i = s ? 1 : 2; i <= n; i++) {
        if (i == s)
          continue;
        int be = i == s + 1 ? 2 : 1;
        if (diff == INT_MAX) {
          diff = l[i] - l[i - be];
        } else if (diff != l[i] - l[i - be]) {
          goto BREAK;
        }
      }
      cout << l[s] << endl;
      break;
    BREAK:;
    }
  }
}
Problem p00253 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N && N) {
    vector<int> a(N + 1);
    vector<int> ans;
    REP(i, N + 1) cin >> a[i];
    REP(i, N + 1) {
      vector<int> b;
      REP(j, N + 1) if (i != j) b.push_back(a[j]);
      set<int> s;
      REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
      if (s.size() == 1) {
        ans.push_back(a[i]);
      }
    }
    assert(ans.size() == 1);
    cout << ans[0] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N && N) {
    vector<int> a(N + 1);
    vector<int> ans;
    REP(i, N + 1) cin >> a[i];
    REP(i, N + 1) {
      vector<int> b;
      REP(j, N + 1) if (i != j) b.push_back(a[j]);
      set<int> s;
      REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
      if (s.size() == 1) {
        ans.push_back(a[i]);
      }
    }
    // assert(ans.size() == 1);
    cout << ans[0] << endl;
  }
  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"

string to_string_keta(const long long int l, const int keta) {
  ostringstream oss;

  oss << setprecision(keta) << l;

  return oss.str();
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    if (N % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int num = 0;
    while (1) {
      if (N == 6174)
        break;
      string st(to_string_keta(N, 4));
      sort(st.begin(), st.end());
      int amin(stoi(st));
      reverse(st.begin(), st.end());
      int amax(stoi(st));
      N = amax - amin;
      num++;
    }
    cout << num << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"

string to_string_keta(const long long int l, const int keta) {
  ostringstream oss;

  oss << std::setw(keta) << std::setfill('0') << l;

  return oss.str();
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    if (N % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int num = 0;
    while (1) {
      if (N == 6174)
        break;
      string st(to_string_keta(N, 4));
      sort(st.begin(), st.end());
      int amin(stoi(st));
      reverse(st.begin(), st.end());
      int amax(stoi(st));
      N = amax - amin;
      num++;
    }
    cout << num << endl;
  }

  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int func2(int *temp) {
  int n = 0;

  n += (temp[0] * 1000);
  n += (temp[1] * 100);
  n += (temp[2] * 10);
  n += temp[3];

  return n;
}

int main() {
  while (true) {
    int N = 0, Nm[4] = {0};
    unsigned int count = 0;
    int judge = 0;
    int Lm[4] = {0}, Sm[4] = {0};
    int S = 0, L = 0;

    cin >> N;

    Nm[0] = N / 1000;
    N %= 1000;
    Nm[1] = N / 100;
    N %= 100;
    Nm[2] = N / 10;
    N %= 10;
    Nm[3] = N;

    if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
      break;

    for (int i = 1; i < 4; i++) {
      if (Nm[i] == Nm[0])
        judge++;
    }

    if (judge != 4) {
      while (true) {
        if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
          break;

        // Sを作成
        sort(Nm, Nm + 4);
        for (int i = 0; i < 4; i++) {
          Sm[i] = Nm[i];
          // cout << Sm[i] << endl;
        }
        S = func2(Sm);

        // Lを作成
        sort(Nm, Nm + 4, greater<int>());
        for (int i = 0; i < 4; i++) {
          Lm[i] = Nm[i];
        }
        L = func2(Lm);

        N = L - S;
        count++;

        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
      }
      cout << count << endl;
    }

    if (judge == 4) {
      cout << "NA" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int func2(int *temp) {
  int n = 0;

  n += (temp[0] * 1000);
  n += (temp[1] * 100);
  n += (temp[2] * 10);
  n += temp[3];

  return n;
}

int main() {
  while (true) {
    int N = 0, Nm[4] = {0};
    unsigned int count = 0;
    int judge = 0;
    int Lm[4] = {0}, Sm[4] = {0};
    int S = 0, L = 0;

    cin >> N;

    Nm[0] = N / 1000;
    N %= 1000;
    Nm[1] = N / 100;
    N %= 100;
    Nm[2] = N / 10;
    N %= 10;
    Nm[3] = N;

    if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
      break;

    for (int i = 0; i < 4; i++) {
      if (Nm[i] == Nm[0])
        judge++;
    }

    if (judge != 4) {
      while (true) {
        if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
          break;

        // Sを作成
        sort(Nm, Nm + 4);
        for (int i = 0; i < 4; i++) {
          Sm[i] = Nm[i];
          // cout << Sm[i] << endl;
        }
        S = func2(Sm);

        // Lを作成
        sort(Nm, Nm + 4, greater<int>());
        for (int i = 0; i < 4; i++) {
          Lm[i] = Nm[i];
        }
        L = func2(Lm);

        N = L - S;
        count++;

        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
      }
      cout << count << endl;
    }

    if (judge == 4) {
      cout << "NA" << endl;
    }
  }
  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

int main() {
  int x, a, b;
  string s;
  while (scanf("%d", &x), x) {
    if (x % 1111 == 0) {
      puts("NA");
    } else {
      int ans;
      for (ans = 0; x != 6174; ++ans) {
        convert(x, s);
        sort(s.begin(), s.end());
        convert(s, a);
        reverse(s.begin(), s.end());
        convert(s, b);
        x = b - a;
      }
      printf("%d\n", ans);
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

int main() {
  int x, a, b;
  string s;
  while (scanf("%d", &x), x) {
    if (x % 1111 == 0) {
      puts("NA");
    } else {
      int ans;
      for (ans = 0; x != 6174; ++ans) {
        convert(x, s);
        s.resize(4, '0');
        sort(s.begin(), s.end());
        convert(s, a);
        reverse(s.begin(), s.end());
        convert(s, b);
        x = b - a;
      }
      printf("%d\n", ans);
    }
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

vector<int> solve(vector<int> a, vector<int> b) {
  string x, y;
  rep(i, a.size()) { x += '0' + a[i]; }
  rep(i, b.size()) { y += '0' + b[i]; }
  int r = stoi(x) - stoi(y);
  vector<int> ret;
  stringstream ss;
  ss << r;
  string z = ss.str();
  rep(i, z.size()) ret.pb(z[i] - '0');
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int x = 0;
    vector<int> c;
    rep(i, s.size()) c.pb(s[i] - '0');
    while (1) {
      if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
        cout << x << endl;
        break;
      }
      if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
        cout << "NA" << endl;
        break;
      }
      vector<int> l = c, s = c;
      sort(all(s));
      sort(all(l), greater<int>());
      c = solve(l, s);
      x++;
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

vector<int> solve(vector<int> a, vector<int> b) {
  string x, y;
  rep(i, a.size()) { x += '0' + a[i]; }
  rep(i, b.size()) { y += '0' + b[i]; }
  int r = stoi(x) - stoi(y);
  vector<int> ret;
  stringstream ss;
  ss << r;
  string z = ss.str();
  string t = "";
  rep(i, 4 - z.size()) t += "0";
  z = t + z;
  rep(i, z.size()) ret.pb(z[i] - '0');
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int x = 0;
    vector<int> c;
    rep(i, s.size()) c.pb(s[i] - '0');
    while (1) {
      if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
        cout << x << endl;
        break;
      }
      if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
        cout << "NA" << endl;
        break;
      }
      vector<int> l = c, s = c;
      sort(all(s));
      sort(all(l), greater<int>());
      c = solve(l, s);
      x++;
    }
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int cnt, a, b;
  char num[5];
  while (1) {
    cin >> num;
    cnt = 0;
    if (strcmp(num, "0000") == 0)
      break;
    if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
      cout << "NA" << endl;
      continue;
    }
    while (1) {
      if (strcmp(num, "6174") == 0)
        break;
      cnt++;
      sort(num, num + 4);
      a = atoi(num);
      sort(num, num + 4, greater<char>());
      b = atoi(num);
      b -= a;
      sprintf(num, "%d", b);
    }
    cout << cnt << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int cnt, a, b;
  char num[5];
  while (1) {
    cin >> num;
    cnt = 0;
    if (strcmp(num, "0000") == 0)
      break;
    if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
      cout << "NA" << endl;
      continue;
    }
    while (1) {
      if (strcmp(num, "6174") == 0)
        break;
      cnt++;
      sort(num, num + 4);
      a = atoi(num);
      sort(num, num + 4, greater<char>());
      b = atoi(num);
      b -= a;
      sprintf(num, "%04d", b);
    }
    cout << cnt << endl;
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;

void solve() {
  string str;
  while (cin >> str) {
    if (str == "0000") {
      break;
    } else if (atoi(str.c_str()) % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int count = 0;
    while (str != "6174") {
      sort(str.begin(), str.end());
      string small = str;
      string large;
      for (int i = str.size() - 1; i >= 0; --i) {
        large += str[i];
      }
      int num = atoi(large.c_str()) - atoi(small.c_str());
      stringstream ss;
      ss << num;
      str = ss.str();
      ++count;
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;

void solve() {
  string str;
  while (cin >> str) {
    if (str == "0000") {
      break;
    } else if (atoi(str.c_str()) % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int count = 0;
    while (str != "6174") {
      sort(str.begin(), str.end());
      string small = str;
      string large;
      for (int i = str.size() - 1; i >= 0; --i) {
        large += str[i];
      }
      int num = atoi(large.c_str()) - atoi(small.c_str());
      stringstream ss;
      ss << num;
      str = ss.str();
      if (str.size() < 4) {
        string buffer = str;
        str.clear();
        for (int i = 0; i < 4 - buffer.size(); ++i) {
          str += "0";
        }
        str += buffer;
      }
      ++count;
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

const std::string answer = "6174";

int main() {
  std::string in_str;
  std::cin >> in_str;
  int L, S;
  int i;
  while (in_str[0] != '0' || in_str[1] != '0' || in_str[2] != '0' ||
         in_str[3] != '0') {
    if (in_str[0] != in_str[1] || in_str[1] != in_str[2] ||
        in_str[2] != in_str[3]) {
      for (i = 0; in_str != answer; i++) {
        {
          std::stringstream sss;
          std::sort(in_str.begin(), in_str.end());
          sss << in_str;
          sss >> S;
        }
        {
          std::stringstream sss;
          std::sort(in_str.begin(), in_str.end(), std::greater<char>());
          sss << in_str;
          sss >> L;
        }
        L -= S;
        {
          std::stringstream sss;
          sss << L;
          in_str = sss.str();
        }
      }
      std::cout << i << std::endl;
    } else {
      std::cout << "NA" << std::endl;
    }
    std::cin >> in_str;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

const std::string answer = "6174";

int main() {
  std::string in_str;
  std::cin >> in_str;
  int L, S;
  int i;
  while (in_str[0] != '0' || in_str[1] != '0' || in_str[2] != '0' ||
         in_str[3] != '0') {
    if (in_str[0] != in_str[1] || in_str[1] != in_str[2] ||
        in_str[2] != in_str[3]) {
      for (i = 0; in_str != answer; i++) {
        {
          std::stringstream sss;
          std::sort(in_str.begin(), in_str.end());
          sss << in_str;
          sss >> S;
        }
        {
          std::stringstream sss;
          std::sort(in_str.begin(), in_str.end(), std::greater<char>());
          sss << in_str;
          sss >> L;
        }
        L -= S;
        {
          std::stringstream sss;
          sss << L;
          in_str = sss.str();
          while (in_str.length() != 4) {
            in_str = '0' + in_str;
          }
        }
      }
      std::cout << i << std::endl;
    } else {
      std::cout << "NA" << std::endl;
    }
    std::cin >> in_str;
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int toInt(string s) {
  stringstream ss(s);
  int ret;
  ss >> ret;
  return ret;
}

string toStr(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

string next(string N) {
  sort(N.begin(), N.end());
  int S = toInt(N);
  reverse(N.begin(), N.end());
  int L = toInt(N);
  return toStr(L - S);
}

int main() {
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    while (N != "6174") {
      N = next(N);
      ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int toInt(string s) {
  stringstream ss(s);
  int ret;
  ss >> ret;
  return ret;
}

string toStr(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

string next(string N) {
  sort(N.begin(), N.end());
  int S = toInt(N);
  reverse(N.begin(), N.end());
  int L = toInt(N);
  string ret = "0000" + toStr(L - S);
  return ret.substr((int)ret.size() - 4);
}

int main() {
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    while (N != "6174") {
      N = next(N);
      ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str)                                                      \
  for (int i = 0; i < (vec).size(); ++i)                                       \
  cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec)                                                           \
  for (int i = 0; i < (vec).size(); ++i)                                       \
  cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main() {
  int n, cnt;

  int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
  vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));

  while (cin >> n, n) {

    if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
      cout << "NA" << endl;
      continue;
    }

    cnt = 0;

    while (n != 6174) {
      stringstream ss;
      ss << n;
      string str = ss.str();

      sort(str.begin(), str.end());

      int s = atoi(str.c_str());

      reverse(str.begin(), str.end());

      int l = atoi(str.c_str());

      n = l - s;
      ++cnt;
    }
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str)                                                      \
  for (int i = 0; i < (vec).size(); ++i)                                       \
  cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec)                                                           \
  for (int i = 0; i < (vec).size(); ++i)                                       \
  cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main() {
  int n, cnt;

  int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
  vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));

  while (cin >> n, n) {

    if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
      cout << "NA" << endl;
      continue;
    }

    cnt = 0;

    while (n != 6174) {
      stringstream ss;
      ss << setfill('0') << setw(4) << n;
      string str = ss.str();

      sort(str.begin(), str.end());

      int s = atoi(str.c_str());

      reverse(str.begin(), str.end());

      int l = atoi(str.c_str());

      n = l - s;
      ++cnt;
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string str;
  while (cin >> str) {
    bool f = false;
    for (auto i : str)
      if (i != '0')
        f = true;

    if (!f)
      break;

    f = false;
    for (auto i : str)
      if (i != str[0])
        f = true;

    if (!f) {
      cout << "NA" << endl;
      continue;
    }

    int count = 0;
    int now;
    stringstream org;
    org << str;
    org >> now;
    while (1) {
      if (now == 6174) {
        cout << count << endl;
        break;
      }
      count++;
      sort(str.begin(), str.end());
      auto rstr = str;
      reverse(rstr.begin(), rstr.end());

      stringstream ss;
      ss << rstr << " " << str;
      int A, B;
      ss >> A >> B;
      now = A - B;
      stringstream next;
      next << now;
      next >> str;
    }
  }

  return 0;
}

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

int main() {
  string str;
  while (cin >> str) {
    bool f = false;
    for (auto i : str)
      if (i != '0')
        f = true;

    if (!f)
      break;

    f = false;
    for (auto i : str)
      if (i != str[0])
        f = true;

    if (!f) {
      cout << "NA" << endl;
      continue;
    }

    int count = 0;
    int now;
    stringstream org;
    org << str;
    org >> now;
    while (1) {
      if (now == 6174) {
        cout << count << endl;
        break;
      }
      count++;
      sort(str.begin(), str.end());
      auto rstr = str;
      reverse(rstr.begin(), rstr.end());

      stringstream ss;
      ss << rstr << " " << str;
      int A, B;
      ss >> A >> B;
      now = A - B;
      stringstream next;
      next << now;
      next >> str;
      while (str.size() < 4)
        str = "0" + str;
    }
  }

  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;

    bool flag = true;
    rep(i, s.size()) if (s[0] != s[i]) flag = false;

    if (flag) {
      cout << "NA" << endl;
    } else {
      int cnt = 0;
      while (true) {
        if (s == "6174") {
          cout << cnt << endl;
          break;
        }

        string temp = s, temp2 = s;
        sort(temp.begin(), temp.end());
        sort(temp2.begin(), temp2.end(), greater<char>());

        int t = atoi(temp.c_str());
        int t2 = atoi(temp2.c_str());

        stringstream ss;
        ss << t2 - t;

        s = ss.str();
        cnt++;
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;

    bool flag = true;
    rep(i, s.size()) if (s[0] != s[i]) flag = false;

    if (flag) {
      cout << "NA" << endl;
    } else {
      int cnt = 0;
      while (true) {
        if (s == "6174") {
          cout << cnt << endl;
          break;
        }

        string temp = s, temp2 = s;
        sort(temp.begin(), temp.end());
        sort(temp2.begin(), temp2.end(), greater<char>());

        int t = atoi(temp.c_str());
        int t2 = atoi(temp2.c_str());

        stringstream ss;
        ss << t2 - t;

        s = "";
        REP(i, ss.str().size(), 4) s += "0";

        s += ss.str();
        cnt++;
      }
    }
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int main() {
  std::string s;
  while (std::cin >> s, s != "0000") {
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      std::cout << "NA" << std::endl;
    } else {
      int t = 0;
      while (s != "6174") {
        int L, S;
        std::sort(s.begin(), s.end(), std::greater<char>());
        L = std::stoi(s);
        std::sort(s.begin(), s.end());
        S = std::stoi(s);

        s = std::to_string(L - S);
        t++;
      }

      std::cout << t << std::endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int main() {
  std::string s;
  while (std::cin >> s, s != "0000") {
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      std::cout << "NA" << std::endl;
    } else {
      int t = 0;
      while (s != "6174") {
        int L, S;
        std::sort(s.begin(), s.end(), std::greater<char>());
        L = std::stoi(s);
        std::sort(s.begin(), s.end());
        S = std::stoi(s);

        s = std::to_string(L - S);
        while (s.length() < 4) {
          s += "0";
        }
        t++;
      }

      std::cout << t << std::endl;
    }
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;

  while (true) {
    cin >> S;

    if (S == "0000") {
      break;
    }

    if (S[0] == S[1] == S[2] == S[3]) {
      cout << "NA" << endl;
    } else {
      int count_ = 0;

      while (S != "6174") {
        string S1 = S, S2 = S;

        sort(S1.begin(), S1.end(), greater<char>());
        sort(S2.begin(), S2.end());

        S = to_string(stoi(S1) - stoi(S2));

        while (S.size() < 4) {
          S = '0' + S;
        }

        count_++;
      }

      cout << count_ << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;

  while (true) {
    cin >> S;

    if (S == "0000") {
      break;
    }

    if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3]) {
      cout << "NA" << endl;
    } else {
      int count_ = 0;

      while (S != "6174") {
        string S1 = S, S2 = S;

        sort(S1.begin(), S1.end(), greater<char>());
        sort(S2.begin(), S2.end());

        S = to_string(stoi(S1) - stoi(S2));

        while (S.size() < 4) {
          S = '0' + S;
        }

        count_++;
      }

      cout << count_ << endl;
    }
  }

  return 0;
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
  while (true) {
    cin >> S;
    if (S == "0000") {
      break;
    }
    if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
      cout << "NA" << endl;
    } else {
      b = stoi(S);
      cnt = 0;
      while (b != 6174) {
        cnt++;
        for (int i = 0; i < 4; i++) {
          a[i] = (b / (int)pow(10, i)) % 10;
        }
        sort(a, a + 4);
        b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
            (a[0] + a[3]);
      }
      cout << cnt << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
  while (true) {
    cin >> S;
    if (S == "0000") {
      break;
    }
    if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
      cout << "NA" << endl;
    } else {
      b = stoi(S);
      cnt = 0;
      while (b != 6174) {
        cnt++;
        for (int i = 0; i < 4; i++) {
          a[i] = (b / (int)pow(10, i)) % 10;
        }
        sort(a, a + 4);
        b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
            (a[0] - a[3]);
      }
      cout << cnt << endl;
    }
  }
}
Problem p00254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

string toString(int n) {
  string ret = "";
  while (n) {
    ret += (n % 10) + '0';
    n /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;

  while (cin >> s) {
    if (s == "0000")
      break;

    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

string toString(int n) {
  string ret = "";
  while (n) {
    ret += (n % 10) + '0';
    n /= 10;
  }
  while (ret.size() < 4)
    ret += "0";
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;

  while (cin >> s) {
    if (s == "0000")
      break;

    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00255 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1000

void calc(int n) {
  int p, sump = 0;
  int joi[SIZE];

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    sump += p;
  }

  for (int i = 0; i < n - 1; i++) {
    scanf("%d", joi + i);
  }

  sort(joi, joi + n - 1);

  ll ans = (ll)sump * n;

  for (int i = n - 2; i >= 0; i--) {
    sump += joi[i];

    ans = max(ans, (ll)sump * (i + 1));
  }

  printf("%lld\n", ans);
}

int main() {

  int n;

  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    calc(n);
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 65010

void calc(int n) {
  int p, sump = 0;
  int joi[SIZE];

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    sump += p;
  }

  for (int i = 0; i < n - 1; i++) {
    scanf("%d", joi + i);
  }

  sort(joi, joi + n - 1);

  ll ans = (ll)sump * n;

  for (int i = n - 2; i >= 0; i--) {
    sump += joi[i];

    ans = max(ans, (ll)sump * (i + 1));
  }

  printf("%lld\n", ans);
}

int main() {

  int n;

  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    calc(n);
  }

  return 0;
}
Problem p00255 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

int main(void) {
  int n, i, e, h[10000];
  unsigned int x, z, a, w;
  while (1) {
    w = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &e);
      w += e;
    }
    for (i = 0; i < n - 1; i++) {
      scanf("%d", &h[i]);
    }
    sort(h, h + n - 1, greater<int>());
    x = n * w;
    z = w;
    for (i = 0; i < n - 1; i++) {
      z = z + h[i];
      a = (n - (i + 1)) * z;
      if (a > x) {
        x = a;
      }
    }
    printf("%u\n", x);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

int main(void) {
  int n, i, e, h[65000];
  unsigned int x, z, a, w;
  while (1) {
    w = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &e);
      w += e;
    }
    for (i = 0; i < n - 1; i++) {
      scanf("%d", &h[i]);
    }
    sort(h, h + n - 1, greater<int>());
    x = n * w;
    z = w;
    for (i = 0; i < n - 1; i++) {
      z = z + h[i];
      a = (n - (i + 1)) * z;
      if (a > x) {
        x = a;
      }
    }
    printf("%u\n", x);
  }
  return 0;
}