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

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

constexpr int MAXA = 300;
constexpr int MAXB = 600;
constexpr int OFFSET = 300;

void chmax(int &a, int b) { a = (a < b ? b : a); }

int main() {
  int n, x, y;
  std::cin >> n >> x >> y;

  std::vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));
  dp[x][y + OFFSET] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAXA; j++) {
      for (int k = 0; k < MAXB; k++) {
        if (dp[j][k] == -1) {
          continue;
        }

        if (j - a[i] >= 0 && k + b[i] < MAXB) {
          chmax(dp[j - a[i]][k + b[i]], dp[j][k]);
        }

        if (k - c[i] >= 0) {
          chmax(dp[j][k - c[i]], dp[j][k] + d[i]);
        }
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < MAXA; i++) {
    for (int j = OFFSET; j < MAXB; j++) {
      chmax(ret, dp[i][j]);
    }
  }

  std::cout << ret << std::endl;

  return 0;
}

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

constexpr int MAXA = 350;
constexpr int MAXB = 700;
constexpr int OFFSET = 350;

void chmax(int &a, int b) { a = (a < b ? b : a); }

int main() {
  int n, x, y;
  std::cin >> n >> x >> y;

  std::vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));
  dp[x][y + OFFSET] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAXA; j++) {
      for (int k = 0; k < MAXB; k++) {
        if (dp[j][k] == -1) {
          continue;
        }

        if (j - a[i] >= 0 && k + b[i] < MAXB) {
          chmax(dp[j - a[i]][k + b[i]], dp[j][k]);
        }

        if (k - c[i] >= 0) {
          chmax(dp[j][k - c[i]], dp[j][k] + d[i]);
        }
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < MAXA; i++) {
    for (int j = OFFSET; j < MAXB; j++) {
      chmax(ret, dp[i][j]);
    }
  }

  std::cout << ret << std::endl;

  return 0;
}
Problem p01065 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n, x, y;
int a[102], b[102], c[102], d[102];

// idx, umaka, fugashi
int dp[102][302][700];
const int offset = 301;

#define at(i, j, k) dp[i][j][k + offset]

int dfs(int i, int j, int k) {
  if (i == n)
    return 0;
  int ret = dfs(i + 1, j, k);
  if (j >= a[i])
    ret = max(ret, dfs(i + 1, j - a[i], k + b[i]));
  ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]);
  return ret;
}

int main() {
  cin >> n >> x >> y;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  rep(i, 102) rep(j, 303) rep(k, 302 * 2) dp[i][j][k] = -1e8;
  at(0, x, y) = 0;

  rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) {
    // cout<<i<<", "<<j<<", "<<k<<endl;
    chmax(at(i + 1, j, k), at(i, j, k));
    if (j - a[i] >= 0 && k + b[i] < 303 * 2)
      chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k));
    if (k - c[i] + offset >= 0)
      chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]);
  }
  int ans = -1e8;
  rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k));
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n, x, y;
int a[102], b[102], c[102], d[102];

// idx, umaka, fugashi
int dp[102][302][700];
const int offset = 301;

#define at(i, j, k) dp[i][j][k + offset]

int dfs(int i, int j, int k) {
  if (i == n)
    return 0;
  int ret = dfs(i + 1, j, k);
  if (j >= a[i])
    ret = max(ret, dfs(i + 1, j - a[i], k + b[i]));
  ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]);
  return ret;
}

int main() {
  cin >> n >> x >> y;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  rep(i, 102) rep(j, 302) rep(k, 302 * 2) dp[i][j][k] = -1e8;
  at(0, x, y) = 0;

  rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) {
    // cout<<i<<", "<<j<<", "<<k<<endl;
    chmax(at(i + 1, j, k), at(i, j, k));
    if (j - a[i] >= 0 && k + b[i] < 303 * 2)
      chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k));
    if (k - c[i] + offset >= 0)
      chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]);
  }
  int ans = -1e8;
  rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k));
  cout << ans << endl;

  return 0;
}
Problem p01075 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

struct edge {
  int to, cost;
};

#define MAX_V 100000
vector<edge> G[MAX_V];

void dijkstra(int s, vector<int> &d) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > e.cost && d[v] <= e.cost) {
        d[e.to] = e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].pb(edge{b, c});
  }

  vector<int> d(n);
  dijkstra(0, d);

  int ans = -1;

  rep(i, n) {
    for (auto &e : G[i]) {
      if (e.to == n - 1) {
        if (d[i] > e.cost)
          continue;
        ans = max(e.cost, ans);
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

struct edge {
  int to, cost;
};

#define MAX_V 100000
vector<edge> G[MAX_V];

void dijkstra(int s, vector<int> &d) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > e.cost && d[v] <= e.cost) {
        d[e.to] = e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].pb(edge{b, c});
  }

  vector<int> d(n);
  dijkstra(0, d);

  int ans = -1;

  rep(i, n) {
    for (auto &e : G[i]) {
      if (e.to == n - 1) {
        if (d[i] > e.cost)
          continue;
        ans = max(e.cost, ans);
      }
    }
  }

  cout << ans << endl;
}
Problem p01075 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edges(N);
  vector<int> memo(N - 1, 1e9 + 1e7);
  memo[0] = 0;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(make_pair(b, c));
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.second;
    const int now_time = atop.first;
    for (auto e : edges[now]) {
      if (e.first == N - 1)
        continue;
      if (e.second >= now_time) {
        if (memo[e.first] > e.second) {
          que.push(make_pair(e.second, e.first));
          memo[e.first] = e.second;
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N - 1; ++i) {
    for (auto e : edges[i]) {
      if (e.first == N - 1 && memo[i] <= e.second) {
        ans = max(ans, e.second);
      }
    }
  }
  { cout << ans << endl; }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edges(N);
  vector<int> memo(N - 1, 1e9 + 1e7);
  memo[0] = 0;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(make_pair(b, c));
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.second;
    const int now_time = atop.first;
    if (memo[now] < now_time)
      continue;
    for (auto e : edges[now]) {
      if (e.first == N - 1)
        continue;
      if (e.second >= now_time) {
        if (memo[e.first] > e.second) {
          que.push(make_pair(e.second, e.first));
          memo[e.first] = e.second;
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N - 1; ++i) {
    for (auto e : edges[i]) {
      if (e.first == N - 1 && memo[i] <= e.second) {
        ans = max(ans, e.second);
      }
    }
  }
  { cout << ans << endl; }
  return 0;
}
Problem p01081 (C++)

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

const int N = 200000;

int n;
char string[N + 1];
std::deque<int> letters[26];

int count[N];
bool present[N];

void modify(int k, int v) {
  for (; k < n; k += ~k & k + 1) {
    count[k] += v;
  }
}

int query(int k) {
  int ret = 0;
  for (; k >= 0; k -= ~k & k + 1) {
    ret += count[k];
  }
  return ret;
}

void remove(int i) {
  present[i] = false;
  modify(i, -1);
}

int main() {
  scanf("%s", string);
  n = strlen(string);
  for (int i = 0; i < n; ++i) {
    letters[(int)(string[i] -= 'a')].push_back(i);
  }

  int odd_count = 0;
  for (int i = 0; i < 26; ++i) {
    odd_count += letters[i].size() & 1;
  }
  if (odd_count != (n & 1)) {
    puts("-1");
    return 0;
  }
  memset(present, true, sizeof(present));
  for (int i = 0; i < n; ++i) {
    modify(i, 1);
  }
  long long answer = 0;
  for (int l = 0, r = n - 1; l < r;) {
    if (!present[l]) {
      l++;
    } else if (!present[r]) {
      r--;
    } else {
      int left_cost = query(n - 1) - query(letters[(int)string[l]].back());
      int right_cost = query(letters[(int)string[r]].front()) - 1;
      answer += std::min(left_cost, right_cost);
      int token = left_cost < right_cost ? string[l] : string[r];
      remove(letters[token].front());
      remove(letters[token].back());
      letters[token].pop_front();
      letters[token].pop_back();
    }
  }
  std::cout << answer << std::endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>

const int N = 1000000;

int n;
char string[N + 1];
std::deque<int> letters[26];

int count[N];
bool present[N];

void modify(int k, int v) {
  for (; k < n; k += ~k & k + 1) {
    count[k] += v;
  }
}

int query(int k) {
  int ret = 0;
  for (; k >= 0; k -= ~k & k + 1) {
    ret += count[k];
  }
  return ret;
}

void remove(int i) {
  present[i] = false;
  modify(i, -1);
}

int main() {
  scanf("%s", string);
  n = strlen(string);
  for (int i = 0; i < n; ++i) {
    letters[(int)(string[i] -= 'a')].push_back(i);
  }

  int odd_count = 0;
  for (int i = 0; i < 26; ++i) {
    odd_count += letters[i].size() & 1;
  }
  if (odd_count != (n & 1)) {
    puts("-1");
    return 0;
  }
  memset(present, true, sizeof(present));
  for (int i = 0; i < n; ++i) {
    modify(i, 1);
  }
  long long answer = 0;
  for (int l = 0, r = n - 1; l < r;) {
    if (!present[l]) {
      l++;
    } else if (!present[r]) {
      r--;
    } else {
      int left_cost = query(n - 1) - query(letters[(int)string[l]].back());
      int right_cost = query(letters[(int)string[r]].front()) - 1;
      answer += std::min(left_cost, right_cost);
      int token = left_cost < right_cost ? string[l] : string[r];
      remove(letters[token].front());
      remove(letters[token].back());
      letters[token].pop_front();
      letters[token].pop_back();
    }
  }
  std::cout << answer << std::endl;
  return 0;
}
Problem p01085 (C++)

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

int main() {
  while (1) {
    int m, nmin, nmax, p[201], maxg = 0, n;
    scanf("%d %d %d", &m, &nmin, &nmax);
    for (int i = 1; i < m; i++) {
      scanf("%d", &p[i]);
    }
    for (int j = nmin; j <= nmax; j++) {
      if (p[j] - p[j + 1] >= maxg) {
        maxg = p[j] - p[j + 1];
        n = j;
      }
    }
    if (m == 0 && nmin == 0 && nmax == 0) {
      return 0;
    } else {
      printf("%d\n", n);
    }
  }
}


Fixed submission:
#include <cstdio>

int main() {
  while (1) {
    int m, nmin, nmax, p[201], maxg = 0, n;
    scanf("%d %d %d", &m, &nmin, &nmax);
    for (int i = 1; i <= m; i++) {
      scanf("%d", &p[i]);
    }
    for (int j = nmin; j <= nmax; j++) {
      if (p[j] - p[j + 1] >= maxg) {
        maxg = p[j] - p[j + 1];
        n = j;
      }
    }
    if (m == 0 && nmin == 0 && nmax == 0) {
      return 0;
    } else {
      printf("%d\n", n);
    }
  }
}
Problem p01085 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <vector>
#define ll long long
#define all(a) (a).begin(), (a).end()

int main(int argc, char const *argv[]) {
  while (1) {
    int m = 0, nmin = 0, nmax = 0;
    scanf("%d%d%d", &m, &nmin, &nmax);
    if (m == 0 && nmin == 0 && nmax == 0) {
      break;
    }
    std::vector<int> p(m);
    for (int i = 1; i <= m; i++) {
      scanf("%d", &p[i]);
    }
    int temp = 0, memo = 0, memoid = 0;
    for (int i = nmin; i <= nmax; i++) {
      temp = p[i] - p[i + 1];
      memo = std::max(memo, temp);
      if (memo == temp)
        memoid = i;
    }
    p.erase(all(p));
    printf("%d\n", memoid);
  }
  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <vector>
#define ll long long
#define all(a) (a).begin(), (a).end()

int main(int argc, char const *argv[]) {
  while (1) {
    int m = 0, nmin = 0, nmax = 0;
    scanf("%d%d%d", &m, &nmin, &nmax);
    if (m == 0 && nmin == 0 && nmax == 0) {
      break;
    }
    std::vector<int> p(m + 1);
    for (int i = 1; i <= m; i++) {
      scanf("%d", &p[i]);
    }
    int temp = 0, memo = 0, memoid = 0;
    for (int i = nmin; i <= nmax; i++) {
      temp = p[i] - p[i + 1];
      memo = std::max(memo, temp);
      if (memo == temp)
        memoid = i;
    }
    p.erase(all(p));
    printf("%d\n", memoid);
  }
  return 0;
}
Problem p01086 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {

  while (true) {

    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }
    string str;
    int nums[41];

    for (int i = 0; i < n; i++) {
      cin >> str;
      nums[i] = str.size();
    }

    int start = 0;
    int tail = 0;
    int sum = 0;
    while (true) {
      while (tail < n && sum < (5 + 7 + 5 + 7 + 7)) {
        tail++;

        sum = 0;
        for (int i = start; i < tail; i++) {
          sum += nums[i];
        }
      }
      if (sum < (5 + 7 + 5 + 7 + 7)) {
        break;
      } else if (sum == (5 + 7 + 5 + 7 + 7)) {
        int ns[5] = {5, 7, 5, 7, 7};
        int s = start;
        int i = 0;
        while (i < 5) {
          if (nums[s] == ns[i]) {
            i++;
            s++;
          } else if (nums[s] > ns[i]) {
            break;
          } else {
            ns[i] = ns[i] - nums[s];
            s++;
          }
        }
        if (i == 5) {
          cout << start + 1 << endl;
          break;
        }
      }

      while (start < tail && sum > (5 + 7 + 5 + 7 + 7)) {
        start++;

        sum = 0;
        for (int i = start; i < tail; i++) {
          sum += nums[i];
        }
      }
    }
  }

  return 0;
}

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {

  while (true) {

    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }
    string str;
    int nums[41];

    for (int i = 0; i < n; i++) {
      cin >> str;
      nums[i] = str.size();
    }

    int start = 0;
    int tail = 0;
    int sum = 0;
    while (true) {
      while (tail < n && sum < (5 + 7 + 5 + 7 + 7)) {
        tail++;

        sum = 0;
        for (int i = start; i < tail; i++) {
          sum += nums[i];
        }
      }
      if (sum < (5 + 7 + 5 + 7 + 7)) {
        break;
      } else if (sum == (5 + 7 + 5 + 7 + 7)) {
        int ns[5] = {5, 7, 5, 7, 7};
        int s = start;
        int i = 0;
        while (i < 5) {
          if (nums[s] == ns[i]) {
            i++;
            s++;
          } else if (nums[s] > ns[i]) {
            break;
          } else {
            ns[i] = ns[i] - nums[s];
            s++;
          }
        }
        if (i == 5) {
          cout << start + 1 << endl;
          break;
        }
      }

      start++;
      sum = 0;
      for (int i = start; i < tail; i++) {
        sum += nums[i];
      }
    }
  }

  return 0;
}
Problem p01086 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

int main() {
  int num_word;
  while (~scanf("%d", &num_word)) {
    if (num_word == 0)
      break;

    vector<string> words;
    for (int word_i = 0; word_i < num_word; word_i++) {
      string word;
      cin >> word;
      words.push_back(word);
    }
    int res = 0;
    for (int start = 0; start < num_word; start++) {
      bool has_end[50];
      memset(has_end, 0, sizeof(has_end));
      int pos = 0;
      for (int word_i = start; word_i < num_word; word_i++) {
        pos += words[word_i].size();
        has_end[pos] = true;
      }

      if (has_end[5] && has_end[5 + 7] && has_end[5 + 7 + 5] &&
          has_end[5 + 7 + 5 + 7] && has_end[5 + 7 + 5 + 7 + 7]) {
        res = start + 1;
        break;
      }
    }
    printf("%d\n", res);
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

int main() {
  int num_word;
  while (~scanf("%d", &num_word)) {
    if (num_word == 0)
      break;

    vector<string> words;
    for (int word_i = 0; word_i < num_word; word_i++) {
      string word;
      cin >> word;
      words.push_back(word);
    }
    int res = 0;
    for (int start = 0; start < num_word; start++) {
      bool has_end[256];
      memset(has_end, 0, sizeof(has_end));
      int pos = 0;
      for (int word_i = start; word_i < num_word; word_i++) {
        pos += words[word_i].size();
        has_end[pos] = true;
      }

      if (has_end[5] && has_end[5 + 7] && has_end[5 + 7 + 5] &&
          has_end[5 + 7 + 5 + 7] && has_end[5 + 7 + 5 + 7 + 7]) {
        res = start + 1;
        break;
      }
    }
    printf("%d\n", res);
  }
}
Problem p01086 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  Short Phrase
//
//  Created by x15071xx on 2017/06/18.
//  Copyright ?? 2017??´ AIT. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main(int argc, const char *argv[]) {

  while (1) {
    int n, i, j;
    vector<string> str;
    cin >> n;

    if (n == 0) {
      break;
    }

    string tmp;
    for (i = 0; i < n; i++) {

      cin >> tmp;
      str.push_back(tmp);
    }

    for (i = 0; i < n; i++) {
      int flag = 0;
      j = i;
      int numsum = 0;
      while (1) {
        if (flag == -1) {
          break;
        }

        if (flag == 5) {
          cout << i + 1 << endl;
          break;
        }

        numsum += str[i + j].length();

        //                if (i == 5) {
        //                    cout << numsum << str[i+j] <<endl;
        //                }
        if (flag == 0 || flag == 2) {

          if (numsum > 5) {
            flag = -1;
          } else if (numsum < 5) {
            j++;
          } else if (numsum == 5) {
            flag++;
            numsum = 0;
            j++;
          }
        } else {
          if (numsum > 7) {
            flag = -1;
          } else if (numsum < 7) {
            j++;
          } else if (numsum == 7) {
            flag++;
            numsum = 0;
            j++;
          }
        }
      }

      if (flag == 5) {
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
//
//  main.cpp
//  Short Phrase
//
//  Created by x15071xx on 2017/06/18.
//  Copyright ?? 2017??´ AIT. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main(int argc, const char *argv[]) {

  while (1) {
    int n, i, j;
    vector<string> str;
    cin >> n;

    if (n == 0) {
      break;
    }

    string tmp;
    for (i = 0; i < n; i++) {

      cin >> tmp;
      str.push_back(tmp);
    }

    for (i = 0; i < n; i++) {
      int flag = 0;
      j = 0;
      int numsum = 0;
      while (1) {
        if (flag == -1) {
          break;
        }

        if (flag == 5) {
          cout << i + 1 << endl;
          break;
        }

        numsum += str[i + j].length();

        //                if (i == 5) {
        //                    cout << numsum << str[i+j] <<endl;
        //                }
        if (flag == 0 || flag == 2) {

          if (numsum > 5) {
            flag = -1;
          } else if (numsum < 5) {
            j++;
          } else if (numsum == 5) {
            flag++;
            numsum = 0;
            j++;
          }
        } else {
          if (numsum > 7) {
            flag = -1;
          } else if (numsum < 7) {
            j++;
          } else if (numsum == 7) {
            flag++;
            numsum = 0;
            j++;
          }
        }
      }

      if (flag == 5) {
        break;
      }
    }
  }
  return 0;
}
Problem p01086 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>

int n, len[40];
char word[11];

int solve(int pos) {
  int rest;
  rest = 5;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 5;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  return 1;
}

int main() {
  while (1) {
    scanf("%d\n", &n);
    for (int i = 0; i < n; i++) {
      scanf("%s\n", word);
      len[i] = strlen(word);
    }
    for (int j = 0; j < n; j++) {
      if (solve(j)) {
        printf("%d\n", j + 1);
        break;
      }
    }
  }
}


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

int n, len[40];
char word[11];

int solve(int pos) {
  int rest;
  rest = 5;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 5;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  rest = 7;
  while (rest > 0) {
    rest -= len[pos++];
    if (rest < 0)
      return 0;
  }
  return 1;
}

int main() {
  while (1) {
    scanf("%d\n", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%s\n", word);
      len[i] = strlen(word);
    }
    for (int j = 0; j < n; j++) {
      if (solve(j)) {
        printf("%d\n", j + 1);
        break;
      }
    }
  }
}
Problem p01086 (C++)

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

int main() {
  int n;
  int d_words[5] = {5, 7, 5, 7, 7};
  while (cin >> n && n != 0) {
    int *n_words = new int[n];
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      n_words[i] = str.size();
    }
    int start = 0;
    int success = 0;
    while (!success) {
      int t_words = 0;
      int tmp = start;
      int tmp2 = 0;
      while (1) {
        tmp2 += n_words[tmp];
        if (tmp2 == d_words[t_words]) {
          t_words++;
          if (t_words == 5) {
            success = start + 1;
            break;
          }
        } else if (tmp2 > d_words[t_words]) {
          break;
        }

        tmp++;
      }
      start++;
    }
    cout << success << endl;
  }
}


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

int main() {
  int n;
  int d_words[5] = {5, 7, 5, 7, 7};
  while (cin >> n && n != 0) {
    int *n_words = new int[n];
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      n_words[i] = str.size();
    }
    int start = 0;
    int success = 0;
    while (!success) {
      int t_words = 0;
      int tmp = start;
      int tmp2 = 0;
      while (1) {
        tmp2 += n_words[tmp];
        if (tmp2 == d_words[t_words]) {
          t_words++;
          tmp2 = 0;
          if (t_words == 5) {
            success = start + 1;
            break;
          }
        } else if (tmp2 > d_words[t_words]) {
          break;
        }

        tmp++;
      }
      start++;
    }
    cout << success << endl;
  }
}
Problem p01087 (C++)

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int calc(vector<string> s, int &i) {
  int depth = s[i].length(), n = s.size();
  char c = s[i][depth - 1];
  if (c == '+') {
    int res = 0;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res += calc(s, i);
    }
    return res;
  } else if (c == '*') {
    int res = 1;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res *= calc(s, i);
    }
    return res;
  } else {
    i++;
    return c - '0';
  }
}

int main() {
  int n;
  ifstream ifs("in.txt");
  while (cin >> n && n) {
    vector<string> s(n);
    REP(i, n) cin >> s[i];
    int i = 0;
    cout << calc(s, i) << endl;
  }

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int calc(vector<string> s, int &i) {
  int depth = s[i].length(), n = s.size();
  char c = s[i][depth - 1];
  if (c == '+') {
    int res = 0;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res += calc(s, i);
    }
    return res;
  } else if (c == '*') {
    int res = 1;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res *= calc(s, i);
    }
    return res;
  } else {
    i++;
    return c - '0';
  }
}

int main() {
  int n;
  ifstream ifs("in.txt");
  while (cin >> n && n) {
    vector<string> s(n + 1);
    REP(i, n) cin >> s[i];
    int i = 0;
    cout << calc(s, i) << endl;
  }

  return 0;
}
Problem p01087 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<int, string>> vpis;
int parse(const vpis &v, int l, int r) {
  if (v[l].second != "+" && v[l].second != "*")
    return strtol(v[l].second.c_str(), NULL, 10);
  int unit = v[l].second == "+" ? 0 : 1;
  for (int i = l + 1; i < r;) {
    int j = i + 1;
    if (v[i].second == "+" || v[i].second == "*") {
      for (; v[j].first > v[i].first; j++)
        ;
    }
    int n = parse(v, i, j);
    i = j;
    if (v[l].second == "+")
      unit += n;
    else
      unit *= n;
  }
  return unit;
}
int main() {
  int N;
  string s;
  for (; cin >> N, N;) {
    vpis v(N);
    for (int i = 0; i < N; i++) {
      cin >> s;
      int idx = 0;
      for (; s[idx] == '.'; idx++)
        ;
      v[i] = make_pair(idx, s.substr(idx));
    }
    printf("%d\n", parse(v, 0, N));
  }
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<int, string>> vpis;
int parse(const vpis &v, int l, int r) {
  if (v[l].second != "+" && v[l].second != "*")
    return strtol(v[l].second.c_str(), NULL, 10);
  int unit = v[l].second == "+" ? 0 : 1;
  for (int i = l + 1; i < r;) {
    int j = i + 1;
    if (v[i].second == "+" || v[i].second == "*") {
      for (; j < v.size() && v[j].first > v[i].first; j++)
        ;
    }
    int n = parse(v, i, j);
    i = j;
    if (v[l].second == "+")
      unit += n;
    else
      unit *= n;
  }
  return unit;
}
int main() {
  int N;
  string s;
  for (; cin >> N, N;) {
    vpis v(N);
    for (int i = 0; i < N; i++) {
      cin >> s;
      int idx = 0;
      for (; s[idx] == '.'; idx++)
        ;
      v[i] = make_pair(idx, s.substr(idx));
    }
    printf("%d\n", parse(v, 0, N));
  }
}
Problem p01087 (C++)

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

int n;
string s[10];
int calc(int a, int b, bool m) {
  int res = m;
  for (int i = b; i < n; i++) {
    if (s[i].size() <= a)
      break;
    if ('0' <= s[i][a] && s[i][a] <= '9') {
      if (m)
        res *= s[i][a] - '0';
      else
        res += s[i][a] - '0';
    }
    if (s[i][a] == '*') {
      if (m)
        res *= calc(a + 1, i + 1, 1);
      else
        res += calc(a + 1, i + 1, 1);
    }
    if (s[i][a] == '+') {
      if (m)
        res *= calc(a + 1, i + 1, 0);
      else
        res += calc(a + 1, i + 1, 0);
    }
  }
  return res;
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    printf("%d\n", calc(0, 0, 0));
  }
}

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

int n;
string s[50];
int calc(int a, int b, bool m) {
  int res = m;
  for (int i = b; i < n; i++) {
    if (s[i].size() <= a)
      break;
    if ('0' <= s[i][a] && s[i][a] <= '9') {
      if (m)
        res *= s[i][a] - '0';
      else
        res += s[i][a] - '0';
    }
    if (s[i][a] == '*') {
      if (m)
        res *= calc(a + 1, i + 1, 1);
      else
        res += calc(a + 1, i + 1, 1);
    }
    if (s[i][a] == '+') {
      if (m)
        res *= calc(a + 1, i + 1, 0);
      else
        res += calc(a + 1, i + 1, 0);
    }
  }
  return res;
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    printf("%d\n", calc(0, 0, 0));
  }
}
Problem p01087 (C++)

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

int main() {
  static auto add = [](int a, int b) { return a + b; };
  static auto mul = [](int a, int b) { return a * b; };
  int n;
  while (cin >> n, n) {
    vector<string> a(n);
    for (auto &e : a) {
      cin >> e;
    }
    a.push_back("0");
    vector<pair<function<int(int, int)>, int>> v{make_pair(add, 0)};
    for (auto e : a) {
      while (e.size() - 1 < v.size() - 1) {
        (v.rbegin() - 1)->second =
            (v.rbegin() - 1)
                ->first((v.rbegin() - 1)->second, v.rbegin()->second);
        v.pop_back();
      }
      char c = *e.rbegin();
      if (c == '+') {
        v.push_back(make_pair(add, 0));
      } else if (c == '*') {
        v.push_back(make_pair(mul, 1));
      } else {
        v.rbegin()->second = v.rbegin()->first(v.rbegin()->second, c - '0');
      }
    }
    cout << v.back().second << endl;
  }
  return 0;
}


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

int main() {
  static auto add = [](int a, int b) { return a + b; };
  static auto mul = [](int a, int b) { return a * b; };
  int n;
  while (cin >> n, n) {
    vector<string> a(n);
    for (auto &e : a) {
      cin >> e;
    }
    a.push_back("0");
    vector<pair<function<int(int, int)>, int>> v{make_pair(add, 0)};
    for (auto e : a) {
      while (e.size() - 1 < v.size() - 1) {
        (v.rbegin() + 1)->second =
            (v.rbegin() + 1)
                ->first((v.rbegin() + 1)->second, v.rbegin()->second);
        v.pop_back();
      }
      char c = *e.rbegin();
      if (c == '+') {
        v.push_back(make_pair(add, 0));
      } else if (c == '*') {
        v.push_back(make_pair(mul, 1));
      } else {
        v.rbegin()->second = v.rbegin()->first(v.rbegin()->second, c - '0');
      }
    }
    cout << v.back().second << endl;
  }
  return 0;
}
Problem p01088 (C++)

Buggy submission (status: 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 = 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);
  }
}


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 = 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);
  }
}
Problem p01088 (C++)

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

#define M 25000

int dp[2][101][M];

int main() {
  int n;
  while (cin >> n, n) {
    int p[100], pp[100];
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]), pp[i] = (1000 - p[i] % 1000) % 1000;

    for (int i = 0; i < 2; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k < M; k++)
          dp[i][j][k] = 1e9;

    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++) {
      int ni = (i + 1) % 2, I = i % 2, mm = min(M, 500 * i + 1);

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++)
          dp[ni][j][k] = 1e9;

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++) {
          int np = k + pp[i];
          int nj = j;
          if (np >= 500)
            nj++, np -= 500;
          np = min(M - 1, np);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
          dp[ni][j][k] = min(dp[ni][j][k], dp[I][j][k]);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
        }
    }

    int c = 0, s = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < M; j++)
        if (dp[n % 2][i][j] != 1e9 && i >= c) {
          if (c == i && dp[n % 2][i][j] < s)
            s = dp[n % 2][i][j];
          else if (c < i)
            c = i, s = dp[n % 2][i][j];
        }
    printf("%d %d\n", c, s);
  }
  return 0;
}

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

#define M 25000

int dp[2][101][M];

int main() {
  int n;
  while (cin >> n, n) {
    int p[100], pp[100];
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]), pp[i] = (1000 - p[i] % 1000) % 1000;

    for (int i = 0; i < 2; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k < M; k++)
          dp[i][j][k] = 1e9;

    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++) {
      int ni = (i + 1) % 2, I = i % 2, mm = min(M, 500 * i + 1);

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++)
          dp[ni][j][k] = 1e9;

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++) {
          int np = k + pp[i];
          int nj = j;
          if (np >= 500)
            nj++, np -= 500;
          np = min(M - 1, np);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
          dp[ni][j][k] = min(dp[ni][j][k], dp[I][j][k]);
        }
    }

    int c = 0, s = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < M; j++)
        if (dp[n % 2][i][j] != 1e9 && i >= c) {
          if (c == i && dp[n % 2][i][j] < s)
            s = dp[n % 2][i][j];
          else if (c < i)
            c = i, s = dp[n % 2][i][j];
        }
    printf("%d %d\n", c, s);
  }
  return 0;
}
Problem p01089 (C++)

Buggy submission (status: 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 = 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;
}


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 = 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;
}
Problem p01089 (C++)

Buggy submission (status: 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;
      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;
}


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;
      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 p01093 (C++)

Buggy submission (status: 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 true;
}

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 0;
}
Problem p01093 (C++)

Buggy submission (status: Runtime Error):
#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;
  }
}

Fixed submission:
#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;
  }
}
Problem p01093 (C++)

Buggy submission (status: 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;

    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;
}

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;

    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;
}