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

#include <iostream>
#define inf 1000000000

using namespace std;

struct state {
  int coin, cost;
  state() {}
  state(int c, int s) { coin = c, cost = s; }
  bool operator<(const state &ope) const {
    return coin < ope.coin || (coin == ope.coin && cost > ope.cost);
  }
};

int n;
int p[105];
state dp[105][250005];

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> p[i];

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < 250005; j++) {
        dp[i][j] = state(-inf, inf);
      }
    }
    dp[0][0] = state(0, 0);

    int pay, ret;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 250005; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

        pay = (p[i + 1] + 999) / 1000 * 1000;
        ret = pay - p[i + 1];
        if (ret >= 500)
          dp[i + 1][j + ret - 500] =
              max(dp[i + 1][j + ret - 500],
                  state(dp[i][j].coin + 1, dp[i][j].cost + p[i + 1]));
        else
          dp[i + 1][j + ret] =
              max(dp[i + 1][j + ret],
                  state(dp[i][j].coin, dp[i][j].cost + p[i + 1]));

        ret = (p[i + 1] + 500) % 1000;
        if (ret <= j) {
          dp[i + 1][j - ret] =
              max(dp[i + 1][j - ret],
                  state(dp[i][j].coin + 1, dp[i][j].cost + p[i + 1]));
        }
      }
    }

    state ans = state(-inf, inf);
    for (int i = 0; i < 250005; i++) {
      ans = max(ans, dp[n][i]);
    }

    cout << ans.coin << " " << ans.cost << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int n, p[100];

void getSolution(int gain, int used, int kozeni, int i, int *max_gain,
                 int *min_used) {
  if (i >= n) {
    *max_gain = gain;
    *min_used = used;
    return;
  }

  int now_max_gain, now_min_used;
  int tmp_max_gain, tmp_min_used;

  getSolution(gain, used, kozeni, i + 1, &now_max_gain, &now_min_used);

  if ((p[i] + 500) % 1000 <= kozeni) {
    getSolution(gain + 1, used + p[i], kozeni - (p[i] + 500) % 1000, i + 1,
                &tmp_max_gain, &tmp_min_used);
    if (tmp_max_gain > now_max_gain ||
        (tmp_max_gain == now_max_gain && tmp_min_used < now_min_used)) {
      now_max_gain = tmp_max_gain;
      now_min_used = tmp_min_used;
    }
  }

  if (p[i] % 1000 != 500) {
    int ex = (1000 - p[i] % 1000) % 1000;
    int dgain = ex / 500;
    ex -= dgain * 500;
    getSolution(gain + dgain, used + p[i], kozeni + ex, i + 1, &tmp_max_gain,
                &tmp_min_used);
    if (tmp_max_gain > now_max_gain ||
        (tmp_max_gain == now_max_gain && tmp_min_used < now_min_used)) {
      now_max_gain = tmp_max_gain;
      now_min_used = tmp_min_used;
    }
  }

  *max_gain = now_max_gain;
  *min_used = now_min_used;
}

int main(void) {

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

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

    int max_gain, min_used;
    getSolution(0, 0, 0, 0, &max_gain, &min_used);

    printf("%d %d\\n", max_gain, min_used);
  }

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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 5000 * 100 * 3;
const int max_x = 102;
const int max_y = 12000;

int dp[max_x + 10][max_y + 10];

void solve(int n) {
  // init
  for (int j = 0; j < max_x + 10; j++)
    for (int k = 0; k < max_y + 10; k++)
      dp[j][k] = INF;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];

  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    const int &pi = p[i];
    for (int x = n; x >= 0; x--) {
      for (int y = max_y; y >= 0; y--) {
        if (pi % 1000 <= 500) {
          if (pi % 1000 > 0 && y - (500 - pi % 1000) >= 0 && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y - (500 - pi % 1000)] + pi);
          if (y + (pi % 1000 + 500) <= max_y && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y + (pi % 1000 + 500)] + pi);
        } else {
          if (y + (pi % 1000 - 500) <= max_y && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y + (pi % 1000 - 500)] + pi);
          if (y - (1000 - pi % 1000) >= 0)
            dp[x][y] = min(dp[x][y], dp[x][y - (1000 - pi % 1000)] + pi);
        }
      }
    }
  }

  for (int i = 100; i >= 0; i--) {
    int mn = INF;
    for (int j = 0; j <= max_y; j++) {
      if (dp[i][j] < INF) {
        mn = min(mn, dp[i][j]);
      }
    }
    if (mn != INF) {
      cout << i << " " << mn << endl;
      return;
    }
  }
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    solve(n);
  }
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 5000 * 100 * 3;
const int max_x = 102;
const int max_y = 25000;

int dp[max_x + 10][max_y + 10];

void solve(int n) {
  // init
  for (int j = 0; j < max_x + 10; j++)
    for (int k = 0; k < max_y + 10; k++)
      dp[j][k] = INF;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];

  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    const int &pi = p[i];
    for (int x = n; x >= 0; x--) {
      for (int y = max_y; y >= 0; y--) {
        if (pi % 1000 <= 500) {
          if (pi % 1000 > 0 && y - (500 - pi % 1000) >= 0 && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y - (500 - pi % 1000)] + pi);
          if (y + (pi % 1000 + 500) <= max_y && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y + (pi % 1000 + 500)] + pi);
        } else {
          if (y + (pi % 1000 - 500) <= max_y && x - 1 >= 0)
            dp[x][y] = min(dp[x][y], dp[x - 1][y + (pi % 1000 - 500)] + pi);
          if (y - (1000 - pi % 1000) >= 0)
            dp[x][y] = min(dp[x][y], dp[x][y - (1000 - pi % 1000)] + pi);
        }
      }
    }
  }

  for (int i = 100; i >= 0; i--) {
    int mn = INF;
    for (int j = 0; j <= max_y; j++) {
      if (dp[i][j] < INF) {
        mn = min(mn, dp[i][j]);
      }
    }
    if (mn != INF) {
      cout << i << " " << mn << endl;
      return;
    }
  }
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    solve(n);
  }
}
Problem p01089 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
bool ok[1028];
string s;
int main() {
  while (cin >> n, n) {
    cin >> s;
    bool flag = false;
    for (int i = 1; i < 1024; i++) {
      for (int j = 0; j < 1024; j++)
        ok[j] = false;
      int bit = 0;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == 'u')
          bit = 0;
        else {
          if (i & (1 << (s[j] - 48)))
            ok[bit] = true;
          bit |= 1 << (s[j] - 48);
        }
      }
      for (int j = 1; j <= i; j++) {
        for (int k = j; k > 0; k = (k - 1) & j) {
          if ((j & k) == k && ok[j - k] && ok[k])
            ok[j] = true;
        }
      }
      if (ok[i])
        flag = true;
    }
    cout << (flag ? "UNSAFE" : "SAFE") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
bool ok[1028];
string s;
int main() {
  while (cin >> n, n) {
    cin >> s;
    bool flag = false;
    for (int i = 1; i < 1024; i++) {
      for (int j = 0; j < 1024; j++)
        ok[j] = false;
      int bit = 0;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == 'u')
          bit = 0;
        else {
          if (i & (1 << (s[j] - 48)))
            ok[bit] = true;
          bit |= 1 << (s[j] - 48);
        }
      }
      for (int j = 1; j <= i; j++) {
        for (int k = 0; k < j; k++) {
          if ((j & k) == k && ok[j - k] && ok[k])
            ok[j] = true;
        }
      }
      if (ok[i])
        flag = true;
    }
    cout << (flag ? "UNSAFE" : "SAFE") << endl;
  }
  return 0;
}
Problem p01089 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n && n != 0) {
    string s;
    cin >> s;
    set<pair<int, int>> st;
    int tmp = 0;
    bool flag = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'u') {
        tmp = 0;
      } else {
        if (s[i + 1] != 'u') {
          tmp += 1 << (s[i] - '0');
          int tmp2 = 1 << (int)(s[i + 1] - '0');
          st.insert(make_pair(tmp, tmp2));
          if ((tmp & tmp2) == tmp2) {
            flag = 1;
          }
        }
      }
    }

    set<pair<int, int>> st2 = st;
    for (int j = 0; j < 10; j++) {
      set<pair<int, int>> st3;
      for (auto x : st) {
        for (auto y : st2) {
          if ((x.first & y.first) == 0) {
            int p = x.first | y.first;
            int q = x.second | y.second;
            st3.insert(make_pair(p, q));
            if ((p & q) == q) {
              flag = 1;
              break;
            }
          }
        }
        if (flag)
          break;
      }
      st2 = st3;
    }
    if (flag) {
      cout << "UNSAFE" << endl;
    } else {
      cout << "SAFE" << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n && n != 0) {
    string s;
    cin >> s;
    set<pair<int, int>> st;
    int tmp = 0;
    bool flag = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'u') {
        tmp = 0;
      } else {
        if (s[i + 1] != 'u') {
          tmp += 1 << (s[i] - '0');
          int tmp2 = 1 << (int)(s[i + 1] - '0');
          st.insert(make_pair(tmp, tmp2));
          if ((tmp & tmp2) == tmp2) {
            flag = 1;
          }
        }
      }
    }

    set<pair<int, int>> st2 = st;
    for (int j = 0; j < 10; j++) {
      set<pair<int, int>> st3;
      st3 = st2;
      for (auto x : st) {
        for (auto y : st2) {
          if ((x.first & y.first) == 0) {
            int p = x.first | y.first;
            int q = x.second | y.second;
            st3.insert(make_pair(p, q));
            if ((p & q) == q) {
              flag = 1;
              break;
            }
          }
        }
        if (flag)
          break;
      }
      st2 = st3;
    }
    if (flag) {
      cout << "UNSAFE" << endl;
    } else {
      cout << "SAFE" << endl;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> PP;

int N;
char S[10010];

int main() {
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    scanf("%s", S);
    set<PP> pat1, pat2;

    int prev = 0;
    ;
    for (int i = 0; i < N; i++) {
      if (S[i] == 'u') {
        prev = 0;
      } else {
        int mask = 1 << (S[i] - '0');
        pat1.insert(make_pair(prev, mask));
        prev |= mask;
      }
    }
    vector<PP> tmp;
    bool flg = true;
    int bsize;
    do {
      bsize = pat1.size();
      tmp.clear();
      for (auto v1 : pat1) {
        for (auto v2 : pat1) {
          if ((v1.first & v2.first) == 0 && (v1.first & v2.second) != 0) {
            PP np = make_pair(v1.first | v2.first, v1.second);
            if ((np.first & np.second) != 0) {
              flg = false;
              goto LOOP;
            }
            tmp.push_back(np);
          }
        }
      }
      for (auto v : tmp) {
        pat1.insert(v);
      }
    } while (bsize != pat1.size());

  LOOP:

    if (flg) {
      printf("SAFE\\n");
    } else {
      printf("UNSAFE\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> PP;

int N;
char S[10010];

int main() {
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    scanf("%s", S);
    set<PP> pat1, pat2;

    int prev = 0;
    ;
    for (int i = 0; i < N; i++) {
      if (S[i] == 'u') {
        prev = 0;
      } else {
        int mask = 1 << (S[i] - '0');
        pat1.insert(make_pair(prev, mask));
        prev |= mask;
      }
    }
    vector<PP> tmp;
    int bsize;
    do {
      bsize = pat1.size();
      tmp.clear();
      for (auto v1 : pat1) {
        for (auto v2 : pat1) {
          if ((v1.first & v2.first) == 0 && (v1.first & v2.second) != 0) {
            tmp.push_back(make_pair(v1.first | v2.first, v1.second));
          }
        }
      }
      for (auto v : tmp) {
        pat1.insert(v);
      }
    } while (bsize != pat1.size());

    bool flg = true;
    for (auto v : pat1) {
      if ((v.first & v.second) != 0) {
        flg = false;
        break;
      }
    }
    if (flg) {
      printf("SAFE\\n");
    } else {
      printf("UNSAFE\\n");
    }
  }
}
Problem p01089 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

char s[10010];
bool comb[1 << 10][10];

int main(void) {
  int n;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%s", s);

    memset(comb, false, sizeof(comb));
    bool safe = true;

    int mask = 0;
    rep(i, n) {
      if (s[i] == 'u')
        mask = 0;
      else {
        int get = s[i] - '0';
        if ((mask & (1 << get)) == 0)
          comb[mask][get] = true;
        else
          safe = false;
        mask |= (1 << get);
      }
    }

    if (safe == false) {
      puts("UNSAFE");
      continue;
    }

    rep(mask1, 1 << 10) rep(get1, 10) rep(mask2, 1 << 10) rep(get2, 10) {
      if (comb[mask1][get1] == false)
        continue;
      if (comb[mask2][get2] == false)
        continue;
      if (mask1 & mask2)
        continue;
      if ((mask2 & (1 << get1)) != 0 && (mask1 & (1 << get2)) == 0)
        comb[mask1 | mask2][get2] = true;
    }

    rep(mask1, 1 << 10) rep(get1, 10) rep(mask2, 1 << 10) rep(get2, 10) {
      if (comb[mask1][get1] == false)
        continue;
      if (comb[mask2][get2] == false)
        continue;
      if (mask1 & mask2)
        continue;
      if ((mask2 & (1 << get1)) != 0 && (mask1 & (1 << get2)) != 0) {
        safe = false;
        mask1 = 1 << 10, get1 = 10, mask2 = 1 << 10, get2 = 10;
      }
    }

    if (safe)
      puts("SAFE");
    else
      puts("UNSAFE");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

bool comb[1 << 10][10];
int main(void) {
  int n;
  while (cin >> n, n) {
    string s;
    cin >> s;
    memset(comb, false, sizeof(comb));
    bool safe = true;

    int mask = 0;
    rep(i, n) {
      if (s[i] == 'u')
        mask = 0;
      else {
        int get = s[i] - '0';
        if ((mask & (1 << get)) == 0)
          comb[mask][get] = true;
        else
          safe = false;
        mask |= (1 << get);
      }
    }

    rep(mask1, 1 << 10) rep(get1, 10) rep(mask2, 1 << 10) rep(get2, 10) {
      if (comb[mask1][get1] == false)
        continue;
      if (comb[mask2][get2] == false)
        continue;
      if (mask1 & mask2)
        continue;
      if ((mask2 & (1 << get1)) != 0 && (mask1 & (1 << get2)) == 0)
        comb[mask1 | mask2][get2] = true;
    }

    rep(mask1, 1 << 10) rep(get1, 10) rep(mask2, 1 << 10) rep(get2, 10) {
      if (comb[mask1][get1] == false)
        continue;
      if (comb[mask2][get2] == false)
        continue;
      if (mask1 & mask2)
        continue;
      if ((mask2 & (1 << get1)) != 0 && (mask1 & (1 << get2)) != 0)
        safe = false;
    }

    if (safe)
      puts("SAFE");
    else
      puts("UNSAFE");
  }
  return 0;
}
Problem p01093 (C++) — buggy submission, verdict: Runtime Error

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

int m, nmin, nmax, ans, gap;
int a[1001];
int main() {
  while (1) {
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> a[i];
    }
    sort(a, a + (m));
    gap = 1000001;
    for (int i = 1; i < m; i++) {
      if (a[i] - a[i - 1] <= gap) {
        gap = a[i] - a[i - 1];
      }
    }
    cout << gap << endl;
  }
}

Error: 0

Fixed submission:

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

int m, nmin, nmax, ans, gap;
int a[201];
int main() {
  while (1) {
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> a[i];
    }
    sort(a, a + (m));
    gap = 1000001;
    for (int i = 1; i < m; i++) {
      if (a[i] - a[i - 1] <= gap) {
        gap = a[i] - a[i - 1];
      }
    }
    cout << gap << endl;
  }
}
Problem p01093 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n;
  vector<int> a;

  while (cin >> n, n) {
    a.resize(n);
    rep(i, n) cin >> a[i];

    sort(a.begin(), a.end());
    int ans = 1001001001;

    rep(i, n - 1) { ans = min(ans, a[i + 1] - a[i]); }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n, left, right;
  vector<int> p;

  while (cin >> n >> left >> right, n) {
    p.resize(n);
    rep(i, n) cin >> p[i];

    sort(p.begin(), p.end());
    reverse(p.begin(), p.end());

    int n_passed = 0;
    int max_gap = 0;

    rep(i, n) {
      // cout << "i: " << i << endl;
      if (!(left <= i + 1 && i + 1 <= right))
        continue;
      // cout << "passed " << i+1 << " students (lowest: " << left << ",
      // highest: " << right << ")" << endl; cout << "current gap: " << p[i] -
      // p[i+1] << endl;

      if (max_gap <= p[i] - p[i + 1]) {
        // cout << "new gap: " << p[i] - p[i+1] << endl;
        // cout << left << ", " << i+1 << ", " << right << endl;
        // cout << "update" << endl;
        n_passed = i + 1;
        max_gap = p[i] - p[i + 1];
      }
    }
    cout << n_passed << endl;
  }
  return 0;
}
Problem p01093 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int t, n, k, x, ans, a;

  while (1) {
    vector<int> point;
    cin >> n;

    if (n == 0) {
      break;
    }

    for (int i = 0; i < n; ++i) {
      cin >> x;
      point.push_back(x);
    }

    sort(point.begin(), point.end());
    reverse(point.begin(), point.end());

    t = point[0];

    for (int i = 1; i < n; ++i) {
      k = point[i];
      if (i == 1) {
        ans = t - k;
      } else {
        a = t - k;
        if (ans > a) {
          ans = a;
        }
      }
      t = k;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int main() {
  int t, n, k, x, ans, a;

  while (1) {
    vector<int> point;
    cin >> n;

    for (int i = 0; i < n; ++i) {
      cin >> x;
      point.push_back(x);
    }

    sort(point.begin(), point.end());
    reverse(point.begin(), point.end());

    t = point[0];

    for (int i = 1; i < n; ++i) {
      k = point[i];
      if (i == 1) {
        ans = t - k;
      } else {
        a = t - k;
        if (ans > a) {
          ans = a;
        }
      }
      t = k;
    }

    cout << ans << endl;
  }

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

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

int main() {
  while (1) {
    int n, s[1000], sa, h;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    sa = s[1] - s[0];
    if (sa < 0)
      sa = sa * (-1);
    for (int i = 0; i < n - 1; i++) {
      for (int l = i + 1; l < n; l++) {
        h = s[l] - s[i];
        if (h < 0)
          h = h * (-1);
        if (sa >= h)
          sa = h;
      }
    }
    cout << sa << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  while (1) {
    int n, s[100], sa = 100, h;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < n - 1; i++) {
      for (int l = i + 1; l < n; l++) {
        h = s[l] - s[i];
        if (h < 0)
          h = h * (-1);
        if (sa >= h)
          sa = h;
      }
    }
    cout << sa << endl;
  }
  return 0;
}
Problem p01093 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(all(a));
    int ans = 1000000000;
    for (int i = 0; i < n - 1; i++) {
      ans = min(ans, a[i + 1] - a[i]);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(all(a));
    int ans = 1000000000;
    for (int i = 0; i < n - 1; i++) {
      ans = min(ans, a[i + 1] - a[i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01093 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int abs(int x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int a[n];
    for (size_t i = 0; i < n; i++)
      cin >> a[i];
    int min = 1000000;
    for (size_t i = 0; i < n; i++) {
      int first;
      first = a[i];
      for (size_t j = i + 1; j < n; j++) {
        int diff = abs(first - a[j]);
        if (diff < min)
          min = diff;
      }
    }
    cout << min << endl;
  }
  return 0;
}

Error: 1

Fixed submission:

#include <iostream>

using namespace std;

int abs(int x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int a[n];
    for (size_t i = 0; i < n; i++)
      cin >> a[i];
    int min = 1000000;
    for (size_t i = 0; i < n; i++) {
      int first;
      first = a[i];
      for (size_t j = i + 1; j < n; j++) {
        int diff = abs(first - a[j]);
        if (diff < min)
          min = diff;
      }
    }
    cout << min << endl;
  }
  return true;
}