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

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

using namespace std;

bool isPrime(int x) {
  if (x == 1) {
    return false;
  }
  if (x == 2) {
    return true;
  }
  if (x % 2 == 0) {
    return false;
  }

  for (int i = 3; i < x; i += 2) {
    if (x % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  while (1) {
    int a, d, n;
    cin >> a >> d >> n;

    if (a == 0 && d == 0 && n == 0) {
      break;
    }

    int count = 0;
    for (int i = a; count < n; i += d) {
      if (isPrime(i)) {
        ++count;
        if (count == n) {
          cout << i << endl;
          break;
        }
      }
    }
  }

  return 0;
}

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

using namespace std;

bool isPrime(int x) {
  if (x == 1) {
    return false;
  }
  if (x == 2) {
    return true;
  }
  if (x % 2 == 0) {
    return false;
  }

  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  while (1) {
    int a, d, n;
    cin >> a >> d >> n;

    if (a == 0 && d == 0 && n == 0) {
      break;
    }

    int count = 0;
    for (int i = a; count < n; i += d) {
      if (isPrime(i)) {
        ++count;
        if (count == n) {
          cout << i << endl;
          break;
        }
      }
    }
  }

  return 0;
}
Problem p00722 (C++)

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

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int a[1000000] = {0, 0, 1, 1}, n, k = 0;
  for (int i = 4; i < 1000000; i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        k++;
        break;
      }
    }
    if (!k)
      a[i]++;
    k = 0;
  }
  int b, c, d, x, s, q;
  while (cin >> b >> c >> d, b != 0 && c != 0 && d != 0) {
    s = x = q = 0;
    while (1) {
      x = b + c * s;
      if (a[x] == 1)
        q++;
      if (q == d)
        break;
      s++;
    }
    cout << x << endl;
  }
  return 0;
}
Problem p00722 (C++)

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

#define int long long

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;

  primes.erase(remove(primes.begin(), primes.end(), 0), primes.end());
  return primes;
}

signed main() {
  int a, d, n;
  vector<int> primes = sieve_of_eratosthenes(1000001);
  while (cin >> a >> d >> n, a) {
    int cnt = 0;
    int ans = 0;
    for (int i = a;; i += d) {
      if (find(primes.begin(), primes.end(), i) != primes.end())
        cnt++;
      if (cnt == n) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define int long long

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;

  primes.erase(remove(primes.begin(), primes.end(), 0), primes.end());
  return primes;
}

signed main() {
  int a, d, n;
  vector<int> primes = sieve_of_eratosthenes(1000001);
  while (cin >> a >> d >> n, a) {
    int cnt = 0;
    int ans = 0;
    for (int i = a;; i += d) {
      if (*lower_bound(primes.begin(), primes.end(), i) == i)
        cnt++;
      if (cnt == n) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
bool isprime(int n, int k = 2) {
  if (n == 1) {
    return false;
  }
  if (n < k * k) {
    return true;
  }
  if (n % k == 0) {
    return false;
  }
  return isprime(n, k + 1);
}
int main() {
  int a, d, n, c;
  while (true) {
    scanf("%d%d%d", &n, &d, &a);
    if (n == 0) {
      break;
    }
    c = 0;
    while (c < n) {
      if (isprime(a)) {
        c++;
      }
      a += d;
    }
    printf("%d\n", a - d);
  }
}

Fixed submission:
#include <cstdio>
bool isprime(int n, int k = 2) {
  if (n == 1) {
    return false;
  }
  if (n < k * k) {
    return true;
  }
  if (n % k == 0) {
    return false;
  }
  return isprime(n, k + 1);
}
int main() {
  int a, d, n, c;
  while (true) {
    scanf("%d%d%d", &a, &d, &n);
    if (n == 0) {
      break;
    }
    c = 0;
    while (c < n) {
      if (isprime(a)) {
        c++;
      }
      a += d;
    }
    printf("%d\n", a - d);
  }
}
Problem p00722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

using namespace std;

int main(void) {
  int flag[MAXNUM + 1], a, d, n, i, j, count;
  fill(flag, flag + MAXNUM, 1);
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    if (flag[i]) {
      for (j = i * 2; j <= MAXNUM; j += i)
        flag[j] = 0;
    }
  }
  while (1) {
    scanf("%d%d%d", &a, &d, &n);
    i = count = 0;
    while (1) {
      if (flag[a + d * i]) {
        count++;
        if (count == n)
          break;
      }
      i++;
    }
    printf("%d\n", a + d * i);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

using namespace std;

int main(void) {
  int flag[MAXNUM + 1], a, d, n, i, j, count;
  fill(flag, flag + MAXNUM, 1);
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    if (flag[i]) {
      for (j = i * 2; j <= MAXNUM; j += i)
        flag[j] = 0;
    }
  }
  while (1) {
    scanf("%d%d%d", &a, &d, &n);
    if (a == 0 && d == 0 && n == 0)
      break;
    i = count = 0;
    while (1) {
      if (flag[a + d * i]) {
        count++;
        if (count == n)
          break;
      }
      i++;
    }
    printf("%d\n", a + d * i);
  }
  return 0;
}
Problem p00722 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

bool is_prime(int n) {
  if (n < 2) {
    return false;
  }
  if (n == 2 || n == 3) {
    return true;
  }
  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1000000;
  int a, d, n;
  cin >> a;
  cin >> d;
  cin >> n;

  std::vector<bool> prime(N);
  for (int i = 0; i < N + 1; i++) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= sqrt(N); i++) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; j++) {
        prime[i * j] = false;
      }
    }
  }

  while (a != 0 || d != 0 || n != 0) {
    bool find = false;
    int count = 0;
    int b;
    for (int i = 0; !find; i++) {
      b = a + (d * i);
      if (prime[b]) {
        count++;
      }
      if (count == n) {
        cout << b << endl;
        find = true;
      }
    }

    cin >> a;
    cin >> d;
    cin >> n;
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

bool is_prime(int n) {
  if (n < 2) {
    return false;
  }
  if (n == 2 || n == 3) {
    return true;
  }
  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1100000;
  int a, d, n;
  cin >> a;
  cin >> d;
  cin >> n;

  std::vector<bool> prime(N);
  for (int i = 0; i < N + 1; i++) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= sqrt(N); i++) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; j++) {
        prime[i * j] = false;
      }
    }
  }

  while (a != 0 || d != 0 || n != 0) {
    bool find = false;
    int count = 0;
    int b;
    for (int i = 0; !find; i++) {
      b = a + (d * i);
      if (prime[b]) {
        count++;
      }
      if (count == n) {
        cout << b << endl;
        find = true;
      }
    }

    cin >> a;
    cin >> d;
    cin >> n;
  }

  return 0;
}
Problem p00722 (C++)

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

int main() {
  int a = 0, d = 0, n = 0;
  int sum = 0;

  while (1) {
    int count = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    sum = a;
    while (1) {
      if (sum == 2) {
        count++;
      } else if (sum > 2) {
        bool flag = true;
        for (int i = 3; i < sum; i += 2) {
          if (sum % i == 0) {
            flag = false;
            break;
          }
        }
        if (flag)
          count++;
      }

      if (count == n)
        break;
      sum += d;
    }
    cout << sum << endl;
  }

  return 0;
}

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

int main() {
  int a = 0, d = 0, n = 0;
  int sum = 0;

  while (1) {
    int count = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    sum = a;
    while (1) {
      if (sum == 2) {
        count++;
      } else if (sum > 2) {
        bool flag = true;
        if (sum % 2 == 0)
          flag = false;
        for (int i = 3; i <= sqrt(sum); i += 2) {
          if (sum % i == 0) {
            flag = false;
            break;
          }
        }
        if (flag)
          count++;
      }

      if (count == n)
        break;
      sum += d;
    }
    cout << sum << endl;
  }

  return 0;
}
Problem p00723 (C++)

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

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    set<string> kinds;
    string str;
    cin >> str;

    for (int i = 1; i < str.size(); i++) {
      string s1 = str.substr(i), s2 = str.substr(0, i);
      string r1 = s1, r2 = s2;
      reverse(r1.begin(), r1.end());
      reverse(r2.begin(), r2.end());

      string s[8] = {
          s1 + s2, s1 + r2, r1 + s2, r1 + r2,
          s2 + s1, r2 + s1, s2 + r1, r2 + r1,
      };
      for (int j = 0; j < 8; j++)
        kinds.insert(s[i]);
    }
    cout << kinds.size() << endl;
  }
}

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

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    set<string> kinds;
    string str;
    cin >> str;

    for (int i = 1; i < str.size(); i++) {
      string s1 = str.substr(i), s2 = str.substr(0, i);
      string r1 = s1, r2 = s2;
      reverse(r1.begin(), r1.end());
      reverse(r2.begin(), r2.end());

      string s[8] = {
          s1 + s2, s1 + r2, r1 + s2, r1 + r2,
          s2 + s1, r2 + s1, s2 + r1, r2 + r1,
      };
      for (int j = 0; j < 8; j++)
        kinds.insert(s[j]);
    }
    cout << kinds.size() << endl;
  }
}
Problem p00723 (C++)

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

#define ALL(c) (c).begin(), (c).end()

int main() {
  int M;
  cin >> M;
  while (M--) {
    string s;
    cin >> s;
    vector<string> v;
    for (int i = 0; i < (int)s.length() - 1; i++) {
      string front = s.substr(0, i + 1);
      string back = s.substr(i + 1);

      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      reverse(ALL(back));
      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      v.push_back(front + back);
      v.push_back(back + front);
    }
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());

    cerr << "-----" << endl;
    for (vector<string>::const_iterator iter = v.begin(); iter != v.end();
         ++iter)
      cerr << *iter << endl;
    cerr << "-----" << endl;

    cout << v.size() << endl;
  }
}

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

#define ALL(c) (c).begin(), (c).end()

int main() {
  int M;
  cin >> M;
  while (M--) {
    string s;
    cin >> s;
    vector<string> v;
    for (int i = 0; i < (int)s.length() - 1; i++) {
      string front = s.substr(0, i + 1);
      string back = s.substr(i + 1);

      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      reverse(ALL(back));
      v.push_back(front + back);
      v.push_back(back + front);

      reverse(ALL(front));
      v.push_back(front + back);
      v.push_back(back + front);
    }
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());

#if 0
		cerr << "-----" << endl;
		for(vector<string>::const_iterator iter = v.begin(); iter != v.end(); ++iter)
			cerr << *iter << endl;
		cerr << "-----" << endl;
#endif

    cout << v.size() << endl;
  }
}
Problem p00723 (C++)

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

using namespace std;

int main(void) {
  vector<string> str;
  vector<string> ans;
  string s[120];
  int n;

  cin >> n;

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

  for (int k = 0; k < n; k++) {
    for (unsigned int i = 0; i < s[k].size(); i++) {
      string str1(s[k].substr(0, i));
      string str2(s[k].substr(i, s[k].size() - i));
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      reverse(str2.begin(), str2.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);
    }

    unsigned int n = 1;
    ans.push_back(s[k]);
    for (unsigned int i = 0; i < str.size(); i++) {
      bool f = true;
      for (unsigned int j = 0; j < ans.size(); j++) {
        if (str[i] == ans[j]) {
          f = false;
          break;
        }
      }
      if (f) {
        n++;
        ans.push_back(str[i]);
      }
    }

    cout << n << endl;
  }

  return (0);
}

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

using namespace std;

int main(void) {
  vector<string> str;
  vector<string> ans;
  string s[120];
  int n;

  cin >> n;

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

  for (int k = 0; k < n; k++) {
    str.clear();
    ans.clear();
    for (unsigned int i = 0; i < s[k].size(); i++) {
      string str1(s[k].substr(0, i));
      string str2(s[k].substr(i, s[k].size() - i));
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      reverse(str2.begin(), str2.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);

      reverse(str1.begin(), str1.end());
      str.push_back(str1 + str2);
      str.push_back(str2 + str1);
    }

    unsigned int n = 1;
    ans.push_back(s[k]);
    for (unsigned int i = 0; i < str.size(); i++) {
      bool f = true;
      for (unsigned int j = 0; j < ans.size(); j++) {
        if (str[i] == ans[j]) {
          f = false;
          break;
        }
      }
      if (f) {
        n++;
        ans.push_back(str[i]);
      }
    }

    cout << n << endl;
  }

  return (0);
}
Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define MAX 1000000000
using namespace std;
int w, h;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int func(int y, int x, int now, int tab[][20]) {
  int ans = MAX, nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1)
      continue;
    while (nx >= 0 && nx < w && ny >= 0 && ny < h) {
      if (tab[ny][nx] == 1 || tab[ny][nx] == 3)
        break;
      nx += dx[i];
      ny += dy[i];
    }
    if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) {
      continue;
    } else if (tab[ny][nx] == 1) {
      tab[ny][nx] = 0;
      ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab));
      tab[ny][nx] = 1;
    } else if (tab[ny][nx] == 3) {
      ans = min(ans, now);
    }
  }
  return ans;
}
int main() {
  int sy, sx;
  while (cin >> w >> h, w || h) {
    int tab[20][20];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> tab[i][j];
        if (tab[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }

    int ans = func(sy, sx, 1, tab);
    if (ans != MAX && ans < 11)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define MAX 1000000000
using namespace std;
int w, h;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int func(int y, int x, int now, int tab[][20]) {
  if (now > 10)
    return MAX;
  int ans = MAX, nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1)
      continue;
    while (nx >= 0 && nx < w && ny >= 0 && ny < h) {
      if (tab[ny][nx] == 1 || tab[ny][nx] == 3)
        break;
      nx += dx[i];
      ny += dy[i];
    }
    if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) {
      continue;
    } else if (tab[ny][nx] == 1) {
      tab[ny][nx] = 0;
      ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab));
      tab[ny][nx] = 1;
    } else if (tab[ny][nx] == 3) {
      ans = min(ans, now);
    }
  }
  return ans;
}
int main() {
  int sy, sx;
  while (cin >> w >> h, w || h) {
    int tab[20][20];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> tab[i][j];
        if (tab[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }

    int ans = func(sy, sx, 1, tab);
    if (ans != MAX && ans < 11)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
Problem p00725 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define foreach(itr, a)                                                        \
  for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

int w, h, ans = 0;
int sx, sy;
int f[21][21];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); }

void dfs(int x, int y, int depth) {
  if (depth + 1 >= ans)
    return;

  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];

    if (!ng(nx, ny) and f[ny][nx] != 1) {
      while (!ng(nx, ny)) {
        // goal
        if (f[ny][nx] == 3) {
          ans = min(ans, depth + 1);
          return;
        }

        if (f[ny][nx] == 1) {
          f[ny][nx] = 0;
          dfs(nx - dx[i], ny - dy[i], depth + 1);
          f[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
  }

  return;
}

int main() {
  while (cin >> w >> h and w) {
    rep(i, h) rep(j, w) {
      cin >> f[i][j];
      if (f[i][j] == 2) {
        sx = j;
        sy = i;
        f[i][j] = 0;
      }
    }

    ans = 114514;
    dfs(sx, sy, 0);
    if (ans <= 10)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }

  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define foreach(itr, a)                                                        \
  for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

int w, h, ans = 0;
int sx, sy;
int f[21][21];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); }

void dfs(int x, int y, int depth) {
  if (depth + 1 >= ans)
    return;

  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];

    if (!ng(nx, ny) and f[ny][nx] != 1) {
      while (!ng(nx, ny)) {
        // goal
        if (f[ny][nx] == 3) {
          ans = min(ans, depth + 1);
          return;
        }

        if (f[ny][nx] == 1) {
          f[ny][nx] = 0;
          dfs(nx - dx[i], ny - dy[i], depth + 1);
          f[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
  }

  return;
}

int main() {
  while (cin >> w >> h and w) {
    rep(i, h) rep(j, w) {
      cin >> f[i][j];
      if (f[i][j] == 2) {
        sx = j;
        sy = i;
        f[i][j] = 0;
      }
    }

    ans = 11;
    dfs(sx, sy, 0);
    if (ans <= 10)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }

  return 0;
}
Problem p00725 (C++)

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

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int sx, sy;
int ans = 100;

bool outofrange(int y, int x) {
  if (y < 0 || y >= h || x < 0 || x >= w) {
    return true;
  } else {
    return false;
  }
}

void dfs(vector<vector<int>> grid, int sy, int sx, int depth) {
  if (depth + 1 >= ans) {
    return;
  }

  for (int i = 0; i < 4; i++) {
    int ny = sy + dy[i];
    int nx = sx + dx[i];

    if (outofrange(ny, nx) == true || grid[ny][nx] == 1) {
      continue;
    }

    while (outofrange(ny, nx) == false) {
      if (grid[ny][nx] == 3) {
        ans = min(ans, depth + 1);
        return;
      }

      if (grid[ny][nx] == 1) {
        grid[ny][nx] = 0;
        dfs(grid, ny - dy[i], nx - dx[i], depth + 1);
        grid[ny][nx] = 1;
        break;
      }

      ny += dy[i];
      nx += dx[i];
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0) {
      break;
    }

    ans = 100;
    vector<vector<int>> grid(h, vector<int>(w));
    pair<int, int> start, goal;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> grid[y][x];

        if (grid[y][x] == 2) {
          sy = y;
          sx = x;
        }
      }
    }

    dfs(grid, sy, sx, 0);

    cout << (ans <= 10 ? ans : -1) << endl;
  }

  return 0;
}

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

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int sx, sy;
int ans = 100;

bool outofrange(int y, int x) {
  if (y < 0 || y >= h || x < 0 || x >= w) {
    return true;
  } else {
    return false;
  }
}

void dfs(vector<vector<int>> grid, int sy, int sx, int depth) {
  if (depth + 1 >= ans || depth + 1 > 10) {
    return;
  }

  for (int i = 0; i < 4; i++) {
    int ny = sy + dy[i];
    int nx = sx + dx[i];

    if (outofrange(ny, nx) == true || grid[ny][nx] == 1) {
      continue;
    }

    while (outofrange(ny, nx) == false) {
      if (grid[ny][nx] == 3) {
        ans = min(ans, depth + 1);
        return;
      }

      if (grid[ny][nx] == 1) {
        grid[ny][nx] = 0;
        dfs(grid, ny - dy[i], nx - dx[i], depth + 1);
        grid[ny][nx] = 1;
        break;
      }

      ny += dy[i];
      nx += dx[i];
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0) {
      break;
    }

    ans = 100;
    vector<vector<int>> grid(h, vector<int>(w));
    pair<int, int> start, goal;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> grid[y][x];

        if (grid[y][x] == 2) {
          sy = y;
          sx = x;
        }
      }
    }

    dfs(grid, sy, sx, 0);

    cout << (ans <= 10 ? ans : -1) << endl;
  }

  return 0;
}
Problem p00726 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

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

using namespace std;

const int imax = 1234567;

int n;
string S;
size_t cur = 0;

int digit();
int number();
string expression();

int digit() { return S[cur++] - '0'; }

int number() {
  int res = digit();
  while (isdigit(S[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

string expression(void) {
  string rec = "";
  while (cur < n) {
    // cout << cur << endl;
    if (rec.size() >= imax)
      return rec;

    if (isupper(S[cur])) {

      rec += S[cur++];

    } else if (isdigit(S[cur])) {

      int r = number();
      string c = "";

      if (S[cur] == '(') {
        cur++;
        c = expression();
      } else
        c = S[cur++];

      rep(i, r) {
        if (rec.size() >= imax)
          return rec;
        rec += c;
      }
    } else if (S[cur] == ')') {
      cur++;
      return rec;
    }
  }
  return rec;
}

int main(void) {
  int i;
  while (cin >> S >> i) {
    if (S == "0")
      break;
    n = S.size();
    cur = 0;
    string res = expression();
    if (res[i] == '\0')
      cout << 0 << endl;
    else
      cout << res[i] << endl;
  }
  return 0;
}

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

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

using namespace std;

const int imax = 1234567;

int n;
string S;
size_t cur = 0;

int digit();
int number();
string expression();

int digit() { return S[cur++] - '0'; }

int number() {
  int res = digit();
  while (isdigit(S[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

string expression(void) {
  string rec = "";
  while (cur < n) {
    // cout << cur << endl;
    if (rec.size() >= imax)
      return rec;

    if (isupper(S[cur])) {

      rec += S[cur++];

    } else if (isdigit(S[cur])) {

      int r = number();
      string c = "";

      if (S[cur] == '(') {
        cur++;
        c = expression();
      } else
        c = S[cur++];

      rep(i, r) {
        if (rec.size() >= imax)
          return rec;
        rec += c;
      }
    } else if (S[cur] == ')') {
      cur++;
      return rec;
    }
  }
  return rec;
}

int main(void) {
  int i;
  while (cin >> S >> i) {
    if (S == "0")
      break;
    n = S.size();
    cur = 0;
    string res = expression();
    if (i >= res.size())
      cout << 0 << endl;
    else
      cout << res[i] << endl;
  }
  return 0;
}
Problem p00726 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, char>;

string S;
int n, it;

int num() {
  int res = 0;
  while (it < n && isdigit(S[it])) {
    res = res * 10 + S[it] - '0';
    it++;
  }
  return res;
}

P parser(int t) {
  P res(0, '0');
  while (it < n && S[it] != ')') {
    if (isdigit(S[it])) {
      int nm = num();
      if (S[it] == '(') {
        it++;
        int tmp = it;
        auto p = parser(t);
        if (p.second != '0') {
          res.second = p.second;
          break;
        } else if (t < p.first * nm) {
          it = tmp;
          t %= p.first;
          res.second = parser(t).second;
        }
        t -= p.first * nm;
        res.first += p.first * nm;
        assert(S[it++] == ')');
      } else {
        if (t < nm) {
          res.second = S[it];
        }
        t -= nm;
        res.first += nm;
        it++;
      }
    } else {
      if (t == 0) {
        res.second = S[it];
      }
      t--;
      res.first++;
      it++;
    }
    if (t < 0) {
      break;
    }
  }
  return res;
}

int main() {
  int i;
  while (cin >> S >> i, S != "0" || i != 0) {
    n = S.size();
    it = 0;
    cout << parser(i).second << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, char>;

string S;
int n, it;

int num() {
  int res = 0;
  while (it < n && isdigit(S[it])) {
    res = res * 10 + S[it] - '0';
    it++;
  }
  return res;
}

P parser(int t) {
  P res(0, '0');
  while (it < n && S[it] != ')') {
    if (isdigit(S[it])) {
      int nm = num();
      if (S[it] == '(') {
        it++;
        int tmp = it;
        auto p = parser(t);
        if (p.second != '0') {
          res.second = p.second;
          break;
        } else if (t < p.first * nm) {
          it = tmp;
          t %= p.first;
          res.second = parser(t).second;
          break;
        }
        t -= p.first * nm;
        res.first += p.first * nm;
        assert(S[it++] == ')');
      } else {
        if (t < nm) {
          res.second = S[it];
        }
        t -= nm;
        res.first += nm;
        it++;
      }
    } else {
      if (t == 0) {
        res.second = S[it];
      }
      t--;
      res.first++;
      it++;
    }
    if (t < 0) {
      break;
    }
  }
  return res;
}

int main() {
  int i;
  while (cin >> S >> i, S != "0" || i != 0) {
    n = S.size();
    it = 0;
    cout << parser(i).second << endl;
  }
  return 0;
}
Problem p00726 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string s;
const int MAX = 1e6 + 1;

typedef long long Int;

Int cnt(int &p);

Int get_num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += (s[p] - '0');
    p++;
  }
  return res;
}

Int cnt_large(int &p) {
  Int res = 0;
  while (s[p] != ')') {
    Int val = cnt(p);
    res += val;
    if (res > MAX)
      return MAX;
  }
  return res;
}

Int cnt(int &p) {
  if (isalpha(s[p])) {
    p++;
    return 1;
  }
  if (isdigit(s[p])) {
    Int num = get_num(p);
    if (isalpha(s[p])) {
      p++;
      return num;
    }
    assert(s[p] == '(');
    p++;
    Int val = cnt_large(p);
    assert(s[p] == ')');
    p++;
    if (val > MAX) {
      return MAX;
    }
    if (num * val > MAX)
      return MAX;
    return num * val;
  }
  return -1000000;
}

char target(int &p, Int x) {
  while (p < s.size()) {
    int prep = p;
    Int val = cnt(p);
    if (val >= x) {
      p = prep;
      if (isalpha(s[p])) {
        return s[p];
      }
      Int num = get_num(p);
      if (isalpha(s[p])) {
        return s[p];
      }
      assert(s[p] == '(');
      p++;
      int prep2 = p;
      Int val2 = cnt_large(p);
      p = prep2;
      return target(p, (x % val2 == 0) ? val2 : x % val2);
    }
    x -= val;
  }
  return '0';
}

int main() {
  int n;
  while (cin >> s >> n, s != "0") {

    int p = 0;
    cout << target(p, n + 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string s;
const int MAX = 1e6 + 1;

typedef long long Int;

Int cnt(int &p);

Int get_num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += (s[p] - '0');
    p++;
  }
  return res;
}

Int cnt_large(int &p) {
  Int res = 0;
  while (s[p] != ')') {
    Int val = cnt(p);
    res += val;
    if (res > MAX)
      res = MAX;
  }
  return res;
}

Int cnt(int &p) {
  if (isalpha(s[p])) {
    p++;
    return 1;
  }
  if (isdigit(s[p])) {
    Int num = get_num(p);
    if (isalpha(s[p])) {
      p++;
      return num;
    }
    assert(s[p] == '(');
    p++;
    Int val = cnt_large(p);
    assert(s[p] == ')');
    p++;
    if (val > MAX) {
      return MAX;
    }
    if (num * val > MAX)
      return MAX;
    return num * val;
  }
  return -1000000;
}

char target(int &p, Int x) {
  while (p < s.size()) {
    int prep = p;
    Int val = cnt(p);
    if (val >= x) {
      p = prep;
      if (isalpha(s[p])) {
        return s[p];
      }
      Int num = get_num(p);
      if (isalpha(s[p])) {
        return s[p];
      }
      assert(s[p] == '(');
      p++;
      int prep2 = p;
      Int val2 = cnt_large(p);
      p = prep2;
      return target(p, (x % val2 == 0) ? val2 : x % val2);
    }
    x -= val;
  }
  return '0';
}

int main() {
  int n;
  while (cin >> s >> n, s != "0") {

    int p = 0;
    cout << target(p, n + 1) << endl;
  }
  return 0;
}
Problem p00728 (C++)

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

int n;

int main() {
  int ave;
  while (cin >> n) {
    int a[20];
    int sum = 0;
    for (int i = 0; i <= n - 1; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int j = 1; j <= n - 2; j++) {
      sum += a[j];
    }
    ave = floor(sum / (n - 2));
    cout << ave << endl;
  }
}

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

int n;

int main() {
  int ave;
  while (cin >> n && (n > 0)) {
    int a[100];
    int sum = 0;
    for (int i = 0; i <= n - 1; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int j = 1; j <= n - 2; j++) {
      sum += a[j];
    }
    ave = floor(sum / (n - 2));
    cout << ave << endl;
  }
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

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

using namespace std;

int main() {
  vector<int> vc;
  int n;
  while (cin >> n) {
    int tmp;
    vc.clear();
    REP(i, n) {
      cin >> tmp;
      vc.push_back(tmp);
    }
    sort(vc.begin(), vc.end());
    vc.erase(vc.begin());
    vc.erase(--vc.end());
    int sum = 0;
    vector<int>::iterator it = vc.begin();
    while (it != vc.end()) {
      sum += (*it);
      it++;
    }
    cout << sum / (n - 2) << endl;
  }
  return 0;
}

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

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

using namespace std;

int main() {
  vector<int> vc;
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int tmp;
    vc.clear();
    REP(i, n) {
      cin >> tmp;
      vc.push_back(tmp);
    }
    sort(vc.begin(), vc.end());
    vc.erase(vc.begin());
    vc.erase(--vc.end());
    int sum = 0;
    vector<int>::iterator it = vc.begin();
    while (it != vc.end()) {
      sum += (*it);
      it++;
    }
    cout << sum / (n - 2) << endl;
  }
  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int n, s;
  while (cin >> n && n > 0) {
    int large = 0;
    int small = 1000;
    int sum = 0;
    for (int i = 0; i > n; ++i) {
      cin >> s;
      sum = sum + s;
      if (large < s) {
        large = s;
      }
      if (small > s) {
        small = s;
      }
    }
    int ans = 0;
    ans = (sum - large - small) / (n - 2);
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, s;
  while (cin >> n && n > 0) {
    int large = 0;
    int small = 1000;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      cin >> s;
      sum = sum + s;
      if (large < s) {
        large = s;
      }
      if (small > s) {
        small = s;
      }
    }
    int ans = 0;
    ans = (sum - large - small) / (n - 2);
    cout << ans << endl;
  }
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int a[20];
  int b, sum = 0, point;
  while (1) {
    cin >> b;
    if (b == 0)
      break;
    for (int i = 0; i < b; i++) {
      cin >> a[i];
    }
    sort(a, a + b);
    for (int j = 1; j < b - 1; j++) {
      sum += a[j];
    }
    point = sum / (b - 2);
    cout << point << endl;
    sum = 0;
  }
  return 0;
}

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

using namespace std;

int main() {
  int a[100];
  int b, sum = 0, point;
  while (1) {
    cin >> b;
    if (b == 0)
      break;
    for (int i = 0; i < b; i++) {
      cin >> a[i];
    }
    sort(a, a + b);
    for (int j = 1; j < b - 1; j++) {
      sum += a[j];
    }
    point = sum / (b - 2);
    cout << point << endl;
    sum = 0;
  }
  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

void remove_minmax(vector<int> &data) {
  int max = 0;
  int min = 1000000;
  int min_i = 0;
  int max_i = 0;
  int count = 0;
  vector<int>::iterator it = data.begin();
  while (it != data.end()) {
    if (*it < min) {
      min = *it;
      min_i = count;
    }
    if (*it > max) {
      max = *it;
      max_i = count;
    }
    ++count;
    ++it;
  }
  data[max_i] = 0;
  data[min_i] = 0;
}

int main(void) {
  int n;
  while (cin >> n) {
    if (n == 0) {
      return -1;
    }

    vector<int> data;

    for (int i = 0; i < n; ++i) {
      int num;
      cin >> num;
      data.push_back(num);
    }
    remove_minmax(data);

    int total =
        floor((double)accumulate(data.begin(), data.end(), 0) / (n - 2));
    cout << total << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

void remove_minmax(vector<int> &data) {
  int max = 0;
  int min = 1000000;
  int min_i = 0;
  int max_i = 0;
  int count = 0;
  vector<int>::iterator it = data.begin();
  while (it != data.end()) {
    if (*it < min) {
      min = *it;
      min_i = count;
    }
    if (*it > max) {
      max = *it;
      max_i = count;
    }
    ++count;
    ++it;
  }
  data[max_i] = 0;
  data[min_i] = 0;
}

int main(void) {
  int n;
  while (cin >> n) {
    if (n == 0) {
      return 0;
    }

    vector<int> data;

    for (int i = 0; i < n; ++i) {
      int num;
      cin >> num;
      data.push_back(num);
    }
    remove_minmax(data);

    int total =
        floor((double)accumulate(data.begin(), data.end(), 0) / (n - 2));
    cout << total << endl;
  }
  return 0;
}
Problem p00728 (C++)

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

int main() {
  int in, a, maxer = 0, miner = 1000;
  int sum = 0;
  int i = 0;
  double ave = 0.0;
  while (scanf("%d", &in) != 0) {
    for (i = 0; i < in; i++) {
      cin >> a;
      maxer = max(a, maxer);
      miner = min(a, miner);
      sum += a;
    }
    sum = sum - maxer - miner;
    ave = sum / (in - 2);
    cout << floor(ave) << endl;
    sum = maxer = ave = 0;
    miner = 1000;
  }
}

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

int main() {
  int in, a, maxer = 0, miner = 1000;
  int sum = 0;
  int i = 0;
  double ave = 0.0;
  while (cin >> in, in) {
    for (i = 0; i < in; i++) {
      cin >> a;
      maxer = max(a, maxer);
      miner = min(a, miner);
      sum += a;
    }
    sum = sum - maxer - miner;
    ave = sum / (in - 2);
    cout << floor(ave) << endl;
    sum = maxer = ave = 0;
    miner = 1000;
  }
}
Problem p00728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#define S 20

int main(void) {
  int a[S];
  int i;
  int n, max, min, sum = 0;

  for (;;) {
    max = 0, min = 1000, sum = 0;

    scanf("%d", &n);
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }

    for (i = 0; i < n; i++) {
      if (max < a[i])
        max = a[i];
      if (min > a[i])
        min = a[i];
    }

    for (i = 0; i < n; i++) {
      sum += a[i];
    }

    sum = sum - max - min;
    printf("%d\n", sum / (n - 2));
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define S 100

int main(void) {
  int a[S];
  int i;
  int n, max, min, sum = 0;

  for (;;) {
    max = 0, min = 1000, sum = 0;

    scanf("%d", &n);
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }

    for (i = 0; i < n; i++) {
      if (max < a[i])
        max = a[i];
      if (min > a[i])
        min = a[i];
    }

    for (i = 0; i < n; i++) {
      sum += a[i];
    }

    sum = sum - max - min;
    printf("%d\n", sum / (n - 2));
  }

  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int main(int argc, char *argv[]) {
  int ninzu = 0;
  while (cin >> ninzu) {
    int ans = 0;
    int i = 0;
    int temp = 0;
    int a = 0;

    vector<int> s;

    for (i = 0; i < ninzu; i++) {
      cin >> a;
      if (temp < a) {
        temp = a;
      }
      s.push_back(a);
    }

    sort(ALL(s));

    for (i = 0; i < s.size() - 1; i++)
      ans += s[i];

    ans = (ans - s[0]) / (i - 1);

    cout << ans << endl;
    ans = 0;
  }

  return 0;
}

Fixed submission:

#include <algorithm>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int main(int argc, char *argv[]) {
  int ninzu = 0;
  while (cin >> ninzu, ninzu) {
    int ans = 0;
    int i = 0;
    int temp = 0;
    int a = 0;

    vector<int> s;

    for (i = 0; i < ninzu; i++) {
      cin >> a;
      if (temp < a) {
        temp = a;
      }
      s.push_back(a);
    }

    sort(ALL(s));

    for (i = 0; i < s.size() - 1; i++)
      ans += s[i];

    ans = (ans - s[0]) / (i - 1);

    cout << ans << endl;
    ans = 0;
  }

  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, s[120], count;
  ;
  while (true) {
    cin >> n;
    count = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      count += s[i];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (s[j] < s[j - 1]) {
          int tmp = s[j];
          s[j] = s[j - 1];
          s[j - 1] = tmp;
        }
      }
    }
    count -= s[0];
    count -= s[n - 1];
    cout << count / (n - 2) << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, s[120], count;
  ;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    count = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      count += s[i];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (s[j] < s[j - 1]) {
          int tmp = s[j];
          s[j] = s[j - 1];
          s[j - 1] = tmp;
        }
      }
    }
    count -= s[0];
    count -= s[n - 1];
    cout << count / (n - 2) << endl;
  }
  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int judge;
    int maxscore = 0;
    int minscore = 0;
    cin >> judge;
    if (judge != 0) {
      int score[judge];
      for (int i = 0; i < judge; i++) {
        cin >> score[i];
        if (score[i] > maxscore) {
          maxscore = score[i];
        }
        if (score[i] < minscore) {
          minscore = score[i];
        }
        if (i == 0) {
          minscore = score[0];
        }
      }
      int lastscore = 0;
      for (int j = 0; j < judge; j++) {
        lastscore += score[j];
      }
      lastscore = (lastscore - maxscore - minscore) / (judge - 2);
      cout << lastscore << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int judge;
    int maxscore = 0;
    int minscore = 0;
    cin >> judge;
    if (judge != 0) {
      int score[judge];
      for (int i = 0; i < judge; i++) {
        cin >> score[i];
        if (score[i] > maxscore) {
          maxscore = score[i];
        }
        if (score[i] < minscore) {
          minscore = score[i];
        }
        if (i == 0) {
          minscore = score[0];
        }
      }
      int lastscore = 0;
      for (int j = 0; j < judge; j++) {
        lastscore += score[j];
      }
      lastscore = (lastscore - maxscore - minscore) / (judge - 2);
      cout << lastscore << endl;
    } else
      break;
  }
  return 0;
}
Problem p00728 (C++)

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

int main() {
  int n, a[21], b[21];
  while (1) {

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

    int max = 0, min = 10001;
    for (int i = 0; i < n; i++) {
      if (max < a[i]) {
        max = a[i];
      }
      if (min > a[i]) {
        min = a[i];
      }
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += a[i];
    }
    sum -= (max + min);

    cout << sum / (n - 2) << endl;
  }
  return 0;
}

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

int main() {
  int n, a[101], b[101];
  while (1) {

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

    int max = 0, min = 10001;
    for (int i = 0; i < n; i++) {
      if (max < a[i]) {
        max = a[i];
      }
      if (min > a[i]) {
        min = a[i];
      }
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += a[i];
    }
    sum -= (max + min);

    cout << sum / (n - 2) << endl;
  }
  return 0;
}
Problem p00728 (C++)

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

int main() {
  int n[100];
  int max[2] = {0, -1}, min[2] = {1000, -1}, sum = 0;
  int m;
  while (true) {
    cin >> m;
    max[0] = 0;
    max[1] = -1;
    min[0] = 1000;
    min[1] = -1;
    sum = 0;
    for (int i = 0; i < m; i++) {
      cin >> n[i];
      if (n[i] > max[0]) {
        max[1] = i;
        max[0] = n[i];
      }
      if (n[i] < min[0]) {
        min[1] = i;
        min[0] = n[i];
      }
    }
    for (int i = 0; i < m; i++) {
      if (max[1] != i && min[1] != i) {
        sum += n[i];
      }
    }
    cout << sum / (m - 2) << endl;
  }
  return 0;
}

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

int main() {
  int n[100];
  int max[2] = {0, -1}, min[2] = {1000, -1}, sum = 0;
  int m;
  while (true) {
    cin >> m;
    if (m == 0) {
      break;
    }
    max[0] = 0;
    max[1] = -1;
    min[0] = 1000;
    min[1] = -1;
    sum = 0;
    for (int i = 0; i < m; i++) {
      cin >> n[i];
      if (n[i] > max[0]) {
        max[1] = i;
        max[0] = n[i];
      }
      if (n[i] < min[0]) {
        min[1] = i;
        min[0] = n[i];
      }
    }
    for (int i = 0; i < m; i++) {
      if (max[1] != i && min[1] != i) {
        sum += n[i];
      }
    }
    cout << sum / (m - 2) << endl;
  }
  return 0;
}