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


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int main(void) {
  string n;
  while (cin >> n && n != "0000") {
    if (count(n.begin(), n.end(), n[0]) == 4) {
      cout << "NA" << endl;
      continue;
    }
    // assert(n != "0000");

    int tmp = toInt(n);
    char str[128];
    sprintf(str, "%04d", tmp);
    n = string(str);
    int cnt = 0;
    while (n != "6174") {
      if (count(n.begin(), n.end(), n[0]) == 4) {
        cout << "NA" << endl;
        goto END;
      }
      string L = n;
      sort(L.rbegin(), L.rend());
      string S = n;
      sort(S.begin(), S.end());
      // cout << n << " " << L << " " << S << endl;
      int tmp = toInt(L) - toInt(S);
      char str[128];
      sprintf(str, "%04d", tmp);
      n = string(str);
      cnt++;
    }
    cout << cnt << endl;
  END:;
  }

  return 0;
}

Error: -6
2cb3985e-c0d5-4dca-ac65-f8d49c15b1f2.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00254/C++/s293024522.cpp:28: int main(): Assertion `count(n.begin(), n.end(), n[0]) != 4' failed.


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int main(void) {
  string n;
  while (cin >> n && n != "0000") {
    if (count(n.begin(), n.end(), n[0]) == 4) {
      cout << "NA" << endl;
    }
    assert(n != "0000");

    int tmp = toInt(n);
    char str[128];
    sprintf(str, "%04d", tmp);
    n = string(str);
    int cnt = 0;
    while (n != "6174") {
      assert(count(n.begin(), n.end(), n[0]) != 4);
      string L = n;
      sort(L.rbegin(), L.rend());
      string S = n;
      sort(S.begin(), S.end());
      // cout << n << " " << L << " " << S << endl;
      int tmp = toInt(L) - toInt(S);
      char str[128];
      sprintf(str, "%04d", tmp);
      n = string(str);
      cnt++;
    }
    cout << cnt << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for (int i = j; i < k; ++i)

using namespace std;

int main() {
  char n[5];
  while (scanf("%s", n) && strcmp(n, "0000") != 0) {
    if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) {
      puts("NA");
      continue;
    }
    int c = 0;
    while (strcmp(n, "6174")) {
      rep(i, 4) FOR(j, i + 1, 4) {
        if (n[i] > n[j]) {
          char tmp = n[i];
          n[i] = n[j];
          n[j] = tmp;
        }
      }
      int a = atoi(n);
      char tmp = n[0];
      n[0] = n[3];
      n[3] = tmp;
      tmp = n[1];
      n[1] = n[2];
      n[2] = tmp;
      int b = atoi(n);
      b -= a;
      sprintf(n, "%04d", b);
      c++;
    }
    printf("%d\\n", c);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for (int i = j; i < k; ++i)

using namespace std;

int main() {
  char n[5];
  while (scanf("%s", n) && strcmp(n, "0000") != 0) {
    if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) {
      puts("NA");
      continue;
    }
    int c = 0;
    while (strcmp(n, "6174") != 0) {
      rep(i, 4) FOR(j, i + 1, 4) {
        if (n[i] > n[j]) {
          char tmp = n[i];
          n[i] = n[j];
          n[j] = tmp;
        }
      }
      int a = atoi(n);
      char tmp = n[0];
      n[0] = n[3];
      n[3] = tmp;
      tmp = n[1];
      n[1] = n[2];
      n[2] = tmp;
      int b = atoi(n);
      b -= a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\\n", c);
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int n[4] = {s[0] - '0', s[1] - '0', s[2] - '0', s[3] - '0'};
    int ans = 0;
    if (n[0] == n[1] && n[2] == n[3] && n[1] == n[2]) {
      ans = -1;
    } else {
      while (1) {
        if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4)
          break;
        sort(n, n + 4);
        int na[4];
        na[0] = n[3] - n[0];
        na[1] = n[2] - n[1];
        na[2] = n[1] - n[2];
        na[3] = n[0] - n[3];
        for (int i = 0; i < 4; i++)
          n[i] = na[i];
        if (n[3] < 0) {
          n[3] += 10;
          n[2]--;
        }
        if (n[2] < 0) {
          n[2] += 10;
          n[1]--;
        }
        if (n[1] < 0) {
          n[1] += 10;
          n[0]--;
        }
        ans++;
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int ans = 0;
    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      ans = -1;
      goto FIN;
    }
    while (s != "6174") {
      string sa, sb;
      char ca[4] = {s[0], s[1], s[2], s[3]};
      sort(ca, ca + 4);
      for (int i = 0; i < 4; i++) {
        sa.push_back(s[i]);
        sb.push_back(s[3 - i]);
      }

      int af = 0, bf = 0;
      for (int i = 0; i < 4; i++) {
        if (sa[i] == 0 && af == 0)
          sa.erase(0, 1);
        else
          af++;

        if (sb[i] == 0 && bf == 0)
          sb.erase(0.1);
        else
          bf++;
      }
      int a = stoi(sa);
      int b = stoi(sb);
      int c = abs(a - b);

      string sc = to_string(c);
      int z = sc.size();
      for (int i = 0; i < 4 - z; i++)
        sc.insert(0, "0");
      s = sc;
      ans++;
    }
  FIN:
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: 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 = "";
        REP(i, ss.str().size(), 4) s += "0";

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

Error: TLE

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 = ss.str();
        cnt++;
      }
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: 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);
  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;
}

Error: TLE

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);
  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;
}
Problem p00254 (C++) — buggy submission, verdict: Runtime Error

#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() {
  char n[5], l[5], h[5], *k;
  int cnt, t;
  k = "6174";
  while (~scanf("%s", n)) {
    cnt = 0;
    strcpy(l, n);
    strcpy(h, n);
    sort(l, l + 4);
    sort(h, h + 4, greater<char>());
    if (*h == '0')
      break;
    if (!strcmp(h, l)) {
      puts("NA");
      continue;
    }
    while (strcmp(n, k)) {
      strcpy(l, n);
      strcpy(h, n);
      sort(l, l + 4);
      sort(h, h + 4, greater<char>());
      for (int i = 3; i >= 0; i--) {
        n[i] = h[i] - l[i] + '0';
        if (n[i] < '0') {
          n[i] += 10;
          h[i - 1]--;
        }
      }
      cnt++;
      if (cnt > 10)
        return 0;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: 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() {
  char n[5], l[5], h[5], *k;
  int cnt, t;
  k = "6174";
  while (~scanf("%s", n)) {
    cnt = 0;
    strcpy(l, n);
    strcpy(h, n);
    sort(l, l + 4);
    sort(h, h + 4, greater<char>());
    if (*h == '0')
      break;
    if (!strcmp(h, l)) {
      puts("NA");
      continue;
    }
    while (strcmp(n, k)) {
      strcpy(l, n);
      strcpy(h, n);
      sort(l, l + 4);
      sort(h, h + 4, greater<char>());
      rep(i, 4) {
        n[i] = h[i] - l[i] + '0';
        if (n[i] < '0') {
          n[i] += 10;
          n[i - 1]--;
        }
      }
      cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;

  while (cin >> s, s != "0000") {
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      cout << "NA" << endl;
      continue;
    }

    int ans = 0;

    while (s != "6174") {
      sort(s.begin(), s.end());
      string S = s;
      sort(s.rbegin(), s.rend());
      string L = s;
      int N = atoi(L.c_str()) - atoi(S.c_str());

      char buff[10];
      sprintf(buff, "%04d", N);
      buff[4] = 0;
      s = string(buff);

      ans++;
    }

    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;

  while (cin >> n, n) {
    n++;
    int t[102];

    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }

    int val1 = -1, val2 = -1;
    int cnt1 = 0, cnt2 = 0;

    for (int i = 1; i < n; i++) {
      int diff = t[i] - t[i - 1];

      if (val1 == -1) {
        val1 = diff;
        cnt1++;
      } else if (val1 == diff) {
        cnt1++;
      } else {
        val2 = diff;
        cnt2++;
      }
    }

    if (cnt2 > cnt1) {
      swap(val1, val2);
    }

    for (int i = 1; i < n; i++) {
      int diff = t[i] - t[i - 1];

      if (val1 != diff) {
        if (i == 1 && t[2] - t[1] == val1) {
          cout << t[0] << endl;
        } else {
          cout << t[i] << endl;
        }
        break;
      }
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int main() {

  while (1) {
    int n = 0, l, s;
    int cnt = 0;
    string tmp;

    cin >> tmp;
    n = atoi(tmp.c_str());

    if (n == 0)
      break;
    if (n % 1111 == 0) {
      printf("NA\\n");
      continue;
    }

    while (n != 6174) {
      sort(tmp.begin(), tmp.end());
      s = atoi(tmp.c_str());

      sort(tmp.begin(), tmp.end(), greater<char>());
      l = atoi(tmp.c_str());
      n = l - s;

      for (int i = 0, p = 1000; i < 4; i++, p /= 10) {
        tmp[i] = '0' + n / p;
        n %= p;
      }
      n = atoi(tmp.c_str());
      cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

void st(char *hoge) {
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      if (hoge[i] < hoge[j]) {
        char t = hoge[i];
        hoge[i] = hoge[j];
        hoge[j] = t;
      }
    }
  }
}

void rst(char *hoge) {
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      if (hoge[i] > hoge[j]) {
        char t = hoge[i];
        hoge[i] = hoge[j];
        hoge[j] = t;
      }
    }
  }
}

int main() {

  while (1) {
    int n = 0, l, s;
    int cnt = 0;
    char tmp[5] = {0};

    scanf("%s", tmp);
    n = atoi(tmp);

    if (n == 0)
      break;
    if (n % 1111 == 0) {
      printf("NA\\n");
      continue;
    }

    while (n != 6174) {
      sprintf(tmp, "%d", n);
      rst(tmp);
      s = atoi(tmp);

      st(tmp);
      l = atoi(tmp);
      n = l - s;

      cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: 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();
          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;
  }
}

Error: TLE

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();
        }
      }
      std::cout << i << std::endl;
    } else {
      std::cout << "NA" << std::endl;
    }
    std::cin >> in_str;
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

string operator-(const string &a, const string &b) {
  char buff[128];
  sprintf(buff, "%04d", stoi(a) - stoi(b));
  return string(buff);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  const string goal = "6174";

  for (string n; cin >> n && n != "0000";) {
    if (all_of(n.begin(), n.end(), [&](char c) { return c == n.front(); })) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (n != goal) {
      string L(n), S(n);
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      n = L - S;
      ++cnt;
    }

    cout << cnt << endl;
  }

  return EXIT_SUCCESS;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

string operator-(const string &a, const string &b) {
  return to_string(stoi(a) - stoi(b));
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  const string goal = "6174";

  for (string n; cin >> n && n != "0000";) {
    if (all_of(n.begin(), n.end(), [&](char c) { return c == n.front(); })) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (n != goal) {
      string L(n), S(n);
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      n = L - S;
      ++cnt;
    }

    cout << cnt << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long long ull;
#define rep(i, a) for (int i = 0; i < a; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

string itos(int s) {
  string res = "0000";
  rep(i, 4) {
    res[res.size() - i - 1] = s % 10 + '0';
    s /= 10;
  }
  return res;
}
int stoi(string s) {
  int res = 0;
  int w = 1000;
  rep(i, 4) {
    // cout<<(int)(s[i]-'0')<<" "<<w<<endl;
    res += (s[i] - '0') * w;
    w /= 10;
  }
  return res;
}

int main(void) {
  string s;
  while (cin >> s) {
    bool brek = true;
    rep(i, 4) if (s[i] != s[0]) brek = false;
    if (brek && s[0] == '0')
      break;
    if (brek && s[0] != '0') {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      cnt++;
      vector<char> used;
      rep(i, 4) { used.push_back(s[i]); }

      sort(used.begin(), used.end());
      string min, max;
      rep(i, used.size()) {
        max += used[used.size() - i - 1];
        min += used[i];
      }
      int maxv = stoi(max), minv = stoi(min);
      int diffv = maxv - minv;
      s = itos(diffv);
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long long ull;
#define rep(i, a) for (int i = 0; i < a; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void) {
  string s;
  while (cin >> s) {
    bool brek = true;
    rep(i, 4) if (s[i] != s[0]) brek = false;
    if (brek && s[0] == '0')
      break;
    if (brek && s[0] != '0') {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      cnt++;
      vector<char> used;
      rep(i, 4) { used.push_back(s[i]); }

      sort(used.begin(), used.end());
      string diff;
      rep(i, used.size()) {
        diff += used[used.size() - i - 1] - used[i] + '0';
        if (diff[i] < '0') {
          diff[i - 1]--;
          diff[i] += 10;
        }
      }
      // cout<<diff<<endl;
      s = diff;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;
int main(void) {
  long long int a, s[65001], d[65000], f, g, h, z;
  int i, j;
  while (1) {
    scanf("%lld", &a);
    if (a == 0)
      break;
    f = 0;
    for (i = 0; i < a; i++) {
      scanf("%lld", &s[i]);
      f += s[i];
    }
    g = f;
    f *= a;
    z = a;
    for (i = 0; i < a - 1; i++)
      scanf("%lld", &d[i]);
    sort(d, d + a - 1, greater<int>());
    for (i = 0; i < a - 1; i++) {
      g += d[i];
      z--;
      if (g * z >= f)
        f = g * z;
      else
        break;
    }
    printf("%lld\\n", f);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int a, s[65001], d[65000], f, g, h, i, j, z;
  while (1) {
    scanf("%d", &a);
    if (a == 0)
      break;
    f = 0;
    for (i = 0; i < a; i++) {
      scanf("%d", &s[i]);
      f += s[i];
    }
    g = f;
    // printf("%d\\n",g);
    f *= a;
    z = a;
    for (i = 0; i < a - 1; i++)
      scanf("%d", &d[i]);
    for (i = 0; i < a - 2; i++) {
      for (j = i + 1; j < a - 1; j++) {
        if (d[i] < d[j]) {
          h = d[j];
          d[j] = d[i];
          d[i] = h;
        }
      }
    }
    for (i = 0; i < a - 1; i++) {
      g += d[i];
      z--;
      //		printf("%d %d %d\\n",g,z,g*z);
      if (g * z > f)
        f = g * z;
    }
    printf("%d\\n", f);
  }
  return 0;
}