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

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

int a[2001];
int minimize(int a[], int z, int w, int k, int n);

int abs(int z, int w) {
  if (z > w) {
    return z - w;
  } else {
    return w - z;
  }
}

int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int min(int a, int b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int maximize(int a[], int z, int w, int k, int n) {
  if (k == n) {
    return abs(a[n], w);
  } else {
    int m = abs(a[n], w);
    for (int i = k; i < n; i++) {
      m = max(m, minimize(a, a[i], w, i + 1, n));
    }
    return m;
  }
}

int minimize(int a[], int z, int w, int k, int n) {
  if (k == n) {
    return abs(z, a[n]);
  } else {
    int m = abs(z, a[n]);
    for (int i = k; i < n; i++) {
      m = min(m, maximize(a, z, a[i], i + 1, n));
    }
    return m;
  }
}

int main() {
  int n, z, w, i, j;
  scanf("%d %d %d", &n, &z, &w);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  j = maximize(a, z, w, 1, n);
  printf("%d", j);
  return 0;
}

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

int a[2001];
int minimize(int a[], int z, int w, int k, int n);

int abs(int z, int w) {
  if (z > w) {
    return z - w;
  } else {
    return w - z;
  }
}

int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int min(int a, int b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int maximize(int a[], int z, int w, int k, int n) {
  if (k == n) {
    return abs(a[n], w);
  } else {
    int m = abs(a[n], w);
    for (int i = k; i < n; i++) {
      m = max(m, minimize(a, a[i], w, i + 1, n));
    }
    return m;
  }
}

int minimize(int a[], int z, int w, int k, int n) {
  if (k == n) {
    return abs(z, a[n]);
  } else {
    int m = abs(z, a[n]);
    for (int i = k; i < n; i++) {
      m = min(m, maximize(a, z, a[i], i + 1, n));
    }
    return m;
  }
}

int main() {
  int n, z, w, i, j;
  scanf("%d %d %d", &n, &z, &w);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  j = maximize(a, z, w, max(1, n - 2), n);
  printf("%d", j);
  return 0;
}
Problem p03550 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Z, W;
  cin >> N >> Z >> W;
  vl A(N);
  rep(i, N) { cin >> A[i]; }
  cout << max({abs(A[N - 1] - W), abs(A[N - 1] - A[N - 2])});
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Z, W;
  cin >> N >> Z >> W;
  vl A(N);
  rep(i, N) { cin >> A[i]; }
  if (N == 1) {
    cout << abs(A[0] - W);
    return 0;
  }
  cout << max({abs(A[N - 1] - W), abs(A[N - 1] - A[N - 2])});
}
Problem p03550 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

int main() {
  int n, z, w;
  cin >> n >> z >> w;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  cout << max(abs(a[n - 1] - w), abs(a[n - 2] - a[n - 1])) << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

int main() {
  int n, z, w;
  cin >> n >> z >> w;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  if (n >= 2)
    cout << max(abs(a[n - 1] - w), abs(a[n - 2] - a[n - 1])) << endl;
  else
    cout << abs(a[n - 1] - w) << endl;
}
Problem p03550 (C++)

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

using namespace std;

int v[1024];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, a, b;
  cin >> n >> a >> b;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  if (n == 1)
    cout << abs(v[0] - b) << "\n";
  else
    cout << max(abs(v[n - 2] - v[n - 1]), abs(v[n - 1] - b)) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int v[2048];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, a, b;
  cin >> n >> a >> b;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  if (n == 1)
    cout << abs(v[0] - b) << "\n";
  else
    cout << max(abs(v[n - 2] - v[n - 1]), abs(v[n - 1] - b)) << "\n";
  return 0;
}
Problem p03550 (C++)

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

using namespace std;

typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = (ll)1e9 + 7;

int rec(int ix, int turn, int me, int other, vector<int> &A,
        vector<vector<int>> &memo) {
  int n = A.size();
  if (ix == n) {
    return abs(me - other);
  }
  int ret = (turn == 0) ? -INF : INF;
  for (int i = ix; i < n; ++i) {
    int score = rec(i + 1, 1 - turn, other, A[i], A, memo);
    ret = (turn == 0) ? max(ret, score) : min(ret, score);
  }
  return memo[ix][turn] = ret;
}

int main() {
  int N, Z, W;
  scanf("%d %d %d", &N, &Z, &W);
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
  }

  vector<vector<int>> memo(2005, vector<int>(2, -1));
  int ans = rec(0, 0, Z, W, A, memo);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = (ll)1e9 + 7;

int rec(int ix, int turn, int me, int other, vector<int> &A,
        vector<vector<int>> &memo) {
  int n = A.size();
  if (ix == n) {
    return abs(me - other);
  }
  if (memo[ix][turn] != -1) {
    return memo[ix][turn];
  }
  int ret = (turn == 0) ? -INF : INF;
  for (int i = ix; i < n; ++i) {
    int score = rec(i + 1, 1 - turn, other, A[i], A, memo);
    ret = (turn == 0) ? max(ret, score) : min(ret, score);
  }
  return memo[ix][turn] = ret;
}

int main() {
  int N, Z, W;
  scanf("%d %d %d", &N, &Z, &W);
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
  }

  vector<vector<int>> memo(2005, vector<int>(2, -1));
  int ans = rec(0, 0, Z, W, A, memo);
  printf("%d\n", ans);
  return 0;
}
Problem p03550 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr ll INF = 1e16;

#define rep(i, N) for (ll i = 0; i < N; ++i)

ll max(ll a, ll b) { return a < b ? b : a; }
ll min(ll a, ll b) { return a > b ? b : a; }

int main() {
  int N;
  ll Z, W;
  cin >> N >> Z >> W;
  vector<ll> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }

  cout << max(abs(W - a.back()), abs(a.back() - a[a.size() - 2])) << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr ll INF = 1e16;

#define rep(i, N) for (ll i = 0; i < N; ++i)

ll max(ll a, ll b) { return a < b ? b : a; }
ll min(ll a, ll b) { return a > b ? b : a; }

int main() {
  int N;
  ll Z, W;
  cin >> N >> Z >> W;
  vector<ll> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }

  if (N == 1) {
    cout << abs(W - a.back()) << endl;
  } else {
    cout << max(abs(W - a.back()), abs(a.back() - a[a.size() - 2])) << endl;
  }
}
Problem p03550 (C++)

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

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

using std::vector;

using std::abs;
using std::max;
using std::min;

int main() {
  int n, z, w;
  cin >> n >> z >> w;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(z);
  a.push_back(w);

  vector<vector<vector<int>>> dp(2,
                                 vector<vector<int>>(n, vector<int>(n + 2, 0)));

  for (int t = 0; t <= 1; t++) {
    for (int j = 0; j < n + 2; j++) {
      dp[t][n - 1][j] = abs(a[n - 1] - a[j]);
    }
  }

  for (int i = n - 2; i >= 0; i--) {
    for (int t = 0; t <= 1; t++) {
      int tmp_max = dp[1][i + 1][i];
      for (int k = i; k < n - 1; k++) {
        tmp_max = max(tmp_max, dp[1][k + 1][k]);
      }
      int tmp_min = dp[0][i + 1][i];
      for (int k = i; k < n - 1; k++) {
        tmp_min = min(tmp_min, dp[0][k + 1][k]);
      }
      for (int j = 0; j <= n + 2; j++) {
        int m;
        if (t == 0) {
          m = max(tmp_max, abs(a[n - 1] - a[j]));
        } else {
          m = min(tmp_min, abs(a[n - 1] - a[j]));
        }
        dp[t][i][j] = m;
      }
    }
  }

  cout << dp[0][0][n + 1] << endl;
  return 0;
}


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

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

using std::vector;

using std::abs;
using std::max;
using std::min;

int main() {
  int n, z, w;
  cin >> n >> z >> w;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(z);
  a.push_back(w);

  vector<vector<vector<int>>> dp(2,
                                 vector<vector<int>>(n, vector<int>(n + 2, 0)));

  for (int t = 0; t <= 1; t++) {
    for (int j = 0; j < n + 2; j++) {
      dp[t][n - 1][j] = abs(a[n - 1] - a[j]);
    }
  }

  for (int i = n - 2; i >= 0; i--) {
    for (int t = 0; t <= 1; t++) {
      int tmp_max = dp[1][i + 1][i];
      for (int k = i; k < n - 1; k++) {
        tmp_max = max(tmp_max, dp[1][k + 1][k]);
      }
      int tmp_min = dp[0][i + 1][i];
      for (int k = i; k < n - 1; k++) {
        tmp_min = min(tmp_min, dp[0][k + 1][k]);
      }
      for (int j = 0; j < n + 2; j++) {
        int m;
        if (t == 0) {
          m = max(tmp_max, abs(a[n - 1] - a[j]));
        } else {
          m = min(tmp_min, abs(a[n - 1] - a[j]));
        }
        dp[t][i][j] = m;
      }
    }
  }

  cout << dp[0][0][n + 1] << endl;
  return 0;
}
Problem p03550 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<ll, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  int n;
  ll z, w;
  cin >> n >> z >> w;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<P> b(n), s(n);
  b[n - 2].first = abs(a[n - 1] - a[n - 2]);
  b[n - 2].second = n - 2;
  s[n - 2] = b[n - 2];
  for (int i = n - 3; i >= 0; --i) {
    if (abs(a[i] - a[n - 1]) > b[i + 1].first) {
      b[i] = P(abs(a[i] - a[n - 1]), i);
    } else {
      b[i] = b[i + 1];
    }
    if (abs(a[i] - a[n - 1]) < s[i + 1].first) {
      s[i] = P(abs(a[i] - a[n - 1]), i);
    } else {
      s[i] = s[i + 1];
    }
  }
  b[n - 1].first = -1;
  s[n - 1].first = LINF;

  int i = 0;
  int f = 0;
  ll ans = abs(w - a[n - 1]);
  while (f != n) {
    if (!(i & 1)) {
      if (abs(w - a[n - 1]) < b[f].first) {
        z = a[b[f].second];
        f = b[f].second + 1;
      } else {
        f = n;
        z = a[n - 1];
      }
    } else {
      if (abs(z - a[n - 1]) > s[f].first) {
        w = a[s[f].second];
        f = s[f].second + 1;
      } else {
        f = n;
        w = a[n - 1];
      }
    }
    ++i;
  }

  ans = max(ans, abs(z - w));
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<ll, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  int n;
  ll z, w;
  cin >> n >> z >> w;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<P> b(n), s(n);
  if (n != 1) {
    b[n - 2].first = abs(a[n - 1] - a[n - 2]);
    b[n - 2].second = n - 2;
    s[n - 2] = b[n - 2];
  }
  for (int i = n - 3; i >= 0; --i) {
    if (abs(a[i] - a[n - 1]) > b[i + 1].first) {
      b[i] = P(abs(a[i] - a[n - 1]), i);
    } else {
      b[i] = b[i + 1];
    }
    if (abs(a[i] - a[n - 1]) < s[i + 1].first) {
      s[i] = P(abs(a[i] - a[n - 1]), i);
    } else {
      s[i] = s[i + 1];
    }
  }
  b[n - 1].first = -1;
  s[n - 1].first = LINF;

  int i = 0;
  int f = 0;
  ll ans = abs(w - a[n - 1]);
  while (f != n) {
    if (!(i & 1)) {
      if (abs(w - a[n - 1]) < b[f].first) {
        z = a[b[f].second];
        f = b[f].second + 1;
      } else {
        f = n;
        z = a[n - 1];
      }
    } else {
      if (abs(z - a[n - 1]) > s[f].first) {
        w = a[s[f].second];
        f = s[f].second + 1;
      } else {
        f = n;
        w = a[n - 1];
      }
    }
    ++i;
  }

  ans = max(ans, abs(z - w));
  cout << ans << endl;
}
Problem p03550 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

ll N, Z, W;
vector<ll> a;

ll dfs(ll x, ll y, ll i, ll c) {
  if (i == N - 1) {
    if (c % 2 == 0)
      return abs(y - a[N - 1]);
    else
      return abs(x - a[N - 1]);
  }
  if (c % 2 == 0) {
    ll ret1 = dfs(a[i], y, i + 1, c + 1);
    ll ret2 = dfs(x, y, i + 1, c);
    return max(ret1, ret2);
  } else {
    ll ret1 = dfs(x, a[i], i + 1, c + 1);
    ll ret2 = dfs(x, y, i + 1, c);
    return min(ret1, ret2);
  }
}

int main() {
  cin >> N >> Z >> W;
  a.resize(N);
  rep(i, 0, N) cin >> a[i];

  std::cout << dfs(Z, W, 0, 0) << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

ll N, Z, W;
vector<ll> a;

ll dfs(ll x, ll y, ll i, ll c) {
  if (i == N - 1) {
    if (c % 2 == 0)
      return abs(y - a[N - 1]);
    else
      return abs(x - a[N - 1]);
  }
  if (c % 2 == 0) {
    ll ret1 = dfs(a[i], y, i + 1, c + 1);
    ll ret2 = dfs(x, y, i + 1, c);
    return max(ret1, ret2);
  } else {
    ll ret1 = dfs(x, a[i], i + 1, c + 1);
    ll ret2 = dfs(x, y, i + 1, c);
    return min(ret1, ret2);
  }
}

int main() {
  cin >> N >> Z >> W;
  a.resize(N);
  rep(i, 0, N) cin >> a[i];

  if (N == 1)
    cout << abs(W - a[0]) << endl;
  else
    cout << max(abs(W - a[N - 1]), abs(a[N - 2] - a[N - 1]));
  return 0;
}
Problem p03550 (C++)

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

using namespace std;

typedef long long ll;

int cmp(int a, int b) { return a <= b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, z, w;
  int tmp, size = 0;
  cin >> n >> z >> w;
  vector<int> v;
  while (n--) {
    cin >> tmp;
    if (size == 0 || v[size - 1] != tmp)
      v.emplace_back(tmp);
    else
      size--;
    size++;
  }
  vector<int>::iterator t = v.begin();
  while (true) {
    t = max_element(t + 1, v.end(), cmp);
    if (t == v.end() - 1 && *t <= z)
      break;
    if (*t > z)
      z = *t;
    t = min_element(t, v.end(), cmp);
    if (t == v.end() - 1 && *t >= w)
      break;
    if (*t < w)
      w = *t;
  }
  if (z < w)
    swap(z, w);
  cout << z - w << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int cmp(int a, int b) { return a <= b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, z, w;
  int tmp, size = 0;
  cin >> n >> z >> w;
  vector<int> v(n);
  for (auto &i : v)
    cin >> i;

  int s = v.size();
  if (s == 1)
    cout << abs(v[0] - w) << endl;
  else
    cout << max(abs(v[s - 1] - w), abs(v[s - 1] - v[s - 2])) << endl;

  return 0;
}
Problem p03550 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int x, z, w;
  cin >> n >> z >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1)
    cout << abs(a[0] - w) << endl;
  else
    cout << max(abs(a[n - 1] - w), abs(a[n - 1] - a[n - 2])) << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int z, w;
  cin >> z >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1)
    cout << abs(a[0] - w) << endl;
  else
    cout << max(abs(a[n - 1] - w), abs(a[n - 1] - a[n - 2])) << endl;
}
Problem p03551 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int powint(int x, int y) {
  int res = 1;
  while (x > 0) {
    if (y & 1 > 0) {
      res *= x;
    }
    x *= x;
    y >>= 1;
  }
  return res;
}

int main() {
  int N, M;
  cin >> N >> M;
  int A = 1900 * M + 100 * (N - M);
  cout << A * powint(2, M) << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int powint(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1 > 0) {
      res *= x;
    }
    x *= x;
    y >>= 1;
  }
  return res;
}

int main() {
  int N, M;
  cin >> N >> M;
  int A = 1900 * M + 100 * (N - M);
  cout << A * powint(2, M) << endl;
  return 0;
}
Problem p03551 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "cmath"
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  double n, m, x;
  cin >> n >> m;
  x = n - m;
  double sum = 0;
  double tmp = 1900 * m + x * 100;
  for (double i = 1; i < 10000000; i++) {
    sum += i * pow((pow(2.0, m) - 1) / pow(2.0, m), i - 1);
  }
  cout << tmp * sum / (pow(2.0, m)) << endl;
  return 0;
}


Fixed submission:
#include "cmath"
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  double n, m, x;
  cin >> n >> m;
  x = n - m;
  double sum = 0;
  double tmp = 1900 * m + x * 100;
  double hhh = (pow(2.0, m) - 1) / pow(2.0, m);
  for (double i = 1; i < 10000; i++) {
    sum += i * pow(hhh, i - 1);
  }
  cout << tmp * sum / (pow(2.0, m)) << endl;
  return 0;
}
Problem p03552 (C++)

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

using namespace std;

int main() {
  int N, Z, W;
  cin >> N >> Z >> W;
  vector<int> a(N);
  for (int i = 1; i <= N; i++)
    cin >> a[i];

  int score = -1;

  if (N == 1)
    score = abs(a[N - 1] - W);
  else
    score = max(abs(a[N - 1] - W), abs(a[N - 2] - a[N - 1]));

  cout << score << endl;
}

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

using namespace std;

int main() {
  int N, Z, W;
  cin >> N >> Z >> W;
  vector<int> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];

  int score = -1;

  if (N == 1)
    score = abs(a[N - 1] - W);
  else
    score = max(abs(a[N - 1] - W), abs(a[N - 2] - a[N - 1]));

  cout << score << endl;
}
Problem p03552 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int dp[2017][2];
int a[2017];
int n, inf = 1001001001;
int cal(int x, int y) {
  if (dp[x][y] >= 0)
    return dp[x][y];
  int ret;
  if (y == 0)
    ret = 0;
  else
    ret = inf;
  REP(i, x + 1, n + 2) {
    int now;
    if (i == n + 1)
      now = abs(a[x] - a[i]);
    else
      now = cal(i, 1 - y);
    if (y == 0)
      ret = max(ret, now);
    else
      ret = min(ret, now);
  }
  return ret;
}
int main() {
  cin >> n >> a[0] >> a[1];
  rep(i, n) cin >> a[2 + i];
  memset(dp, -1, sizeof(dp));
  cout << cal(1, 0) << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int dp[2017][2];
int a[2017];
int n, inf = 1001001001;
int cal(int x, int y) {
  if (dp[x][y] >= 0)
    return dp[x][y];
  int ret;
  if (y == 0)
    ret = 0;
  else
    ret = inf;
  REP(i, x + 1, n + 2) {
    int now;
    if (i == n + 1)
      now = abs(a[x] - a[i]);
    else
      now = cal(i, 1 - y);
    if (y == 0)
      ret = max(ret, now);
    else
      ret = min(ret, now);
  }
  return dp[x][y] = ret;
}
int main() {
  cin >> n >> a[0] >> a[1];
  rep(i, n) cin >> a[2 + i];
  memset(dp, -1, sizeof(dp));
  cout << cal(1, 0) << endl;
}
Problem p03552 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
int n, a[100], w;
int main() {
  scanf("%d%*d%d", &n, &w);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  if (n == 1)
    printf("%d\n", std::abs(w - a[n]));
  else
    printf("%d\n", std::max(std::abs(w - a[n]), std::abs(a[n - 1] - a[n])));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
int n, a[2010], w;
int main() {
  scanf("%d%*d%d", &n, &w);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  if (n == 1)
    printf("%d\n", std::abs(w - a[n]));
  else
    printf("%d\n", std::max(std::abs(w - a[n]), std::abs(a[n - 1] - a[n])));
  return 0;
}
Problem p03552 (C++)

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

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>
#define dead not_bad

#define left not_right
#define y1 what

using namespace std;

const int N = (int)2e3 + 10;
const int M = (int)3e6 + 10;
const ll LINF = (ll)1e18;
const double PI = 3.14159265359;
const int INF = (int)1e9 + 7;
const int mod = 1e9 + 7;
const double EPS = (double)1e-9;

int n, z, w;
int a[N];
int dp[N][N];

int calc(int id, int turn, int x, int y) {
  if (id == n + 1)
    return abs(a[x] - a[y]);
  if (dp[id][turn] != -1)
    return dp[id][turn];
  int ans = 0;
  if (turn == 0) {
    for (int i = id; i <= n; i++) {
      ans = max(ans, calc(i + 1, turn ^ 1, i, y));
    }
  } else {
    ans = INF;
    for (int i = id; i <= n; i++) {
      ans = min(ans, calc(i + 1, turn ^ 1, x, i));
    }
  }
  return dp[x][y] = ans;
}

int main() {
#define fn "billboard"
#ifdef witch
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
//    freopen(fn".in", "r", stdin);
//    freopen(fn".out", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> z >> w;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[n + 1] = z;
  a[n + 2] = w;
  memset(dp, -1, sizeof(dp));
  cout << calc(1, 0, n + 1, n + 2);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>
#define dead not_bad

#define left not_right
#define y1 what

using namespace std;

const int N = (int)2e3 + 10;
const int M = (int)3e6 + 10;
const ll LINF = (ll)1e18;
const double PI = 3.14159265359;
const int INF = (int)1e9 + 7;
const int mod = 1e9 + 7;
const double EPS = (double)1e-9;

int n, z, w;
int a[N];
int dp[N][N];

int calc(int id, int turn, int x, int y) {
  if (id == n + 1)
    return abs(a[x] - a[y]);
  if (dp[id][turn] != -1)
    return dp[id][turn];
  int ans = 0;
  if (turn == 0) {
    for (int i = id; i <= n; i++) {
      ans = max(ans, calc(i + 1, turn ^ 1, i, y));
    }
  } else {
    ans = INF;
    for (int i = id; i <= n; i++) {
      ans = min(ans, calc(i + 1, turn ^ 1, x, i));
    }
  }
  return dp[id][turn] = ans;
}

int main() {
#define fn "billboard"
#ifdef witch
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
//    freopen(fn".in", "r", stdin);
//    freopen(fn".out", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> z >> w;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[n + 1] = z;
  a[n + 2] = w;
  memset(dp, -1, sizeof(dp));
  cout << calc(1, 0, n + 1, n + 2);
  return 0;
}
Problem p03552 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define mod 1000000007
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
// #define INF 931931114518101
using namespace std;
typedef pair<ll, ll> P;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define yo 100001
//------------------変数-----------------------//
ll n, z, w, c[2002];
ll dp[2][2001];
//-------------------関数----------------------//

ll sa(P a) { return abs(a.first - a.second); }

ll DP(ll num, ll pl, ll oth) {
  if (num == n - 1) {
    return sa(P(c[n - 1], oth));
  }
  if (dp[pl][num] >= 0) {
    return dp[pl][num];
  }
  if (pl == 0) {
    ll now = 0;
    FOR(i, num, n - 1) { now = max(now, DP(i + 1, 1, c[num])); }
    return DP(n - 1, 0, c[num]);
  }
  if (pl == 1) { // y
    ll now = INF;
    FOR(i, num, n - 1) { now = min(DP(i + 1, 0, c[num]), now); }
    dp[pl][num] = now;
    return dp[pl][num] = now;
  }
}

int main() {
  fill_all(dp, (ll)-1);
  cin >> n >> z >> w;
  REP(i, n) { cin >> c[i]; }
  ll cnt = 0;
  REP(i, n) { cnt = max(DP(i, 1, w), cnt); }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define mod 1000000007
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
// #define INF 931931114518101
using namespace std;
typedef pair<ll, ll> P;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define yo 100001
//------------------変数-----------------------//
ll n, z, w, c[2002];
ll dp[2][2001];
//-------------------関数----------------------//

ll sa(P a) { return abs(a.first - a.second); }

ll DP(ll num, ll pl, ll oth) {
  if (num == n - 1) {
    return sa(P(c[n - 1], oth));
  }
  if (dp[pl][num] >= 0) {
    return dp[pl][num];
  }
  if (pl == 0) {
    ll now = 0;
    FOR(i, num, n - 1) { now = max(now, DP(i + 1, 1, c[num])); }
    return dp[pl][num] = now;
  }
  if (pl == 1) { // y
    ll now = INF;
    FOR(i, num, n - 1) { now = min(DP(i + 1, 0, c[num]), now); }
    dp[pl][num] = now;
    return dp[pl][num] = now;
  }
}

int main() {
  fill_all(dp, (ll)-1);
  cin >> n >> z >> w;
  REP(i, n) { cin >> c[i]; }
  ll cnt = 0;
  REP(i, n) { cnt = max(DP(i, 1, w), cnt); }
  cout << cnt << endl;
}
Problem p03552 (C++)

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

int main() {
  long n, z, w, k;
  cin >> n >> z >> w;
  vector<long> v(n);
  vector<long> e(n);
  for (int i = 0; i < n; i++)
    cin >> v.at(i);
  for (int i = 0; i < n; i++) {
    e.at(i) = max(v.at(i) - v.at(n - 1), v.at(n - 1) - v.at(i));
  }
  k = max(w - v.at(n - 1), v.at(n - 1) - w);
  cout << max(k, e.at(n - 2)) << endl;
  return 0;
}

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

int main() {
  long n, z, w, k;
  cin >> n >> z >> w;
  vector<long> v(n);
  vector<long> e(n);
  for (int i = 0; i < n; i++)
    cin >> v.at(i);
  for (int i = 0; i < n; i++) {
    e.at(i) = max(v.at(i) - v.at(n - 1), v.at(n - 1) - v.at(i));
  }
  k = max(w - v.at(n - 1), v.at(n - 1) - w);
  if (n >= 2) {

    cout << max(k, e.at(n - 2)) << endl;
  } else {
    cout << k << endl;
  }
  return 0;
}
Problem p03552 (C++)

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

using namespace std;

int a[2002], n, d[2][2002];

int f(int t, int k) {
  int res = abs(a[k] - a[n - 1]);
  for (int i = k + 1; i < n - 1; i++) {
    if (t == 0)
      res = max(res, f(1, i));
    else
      res = min(res, f(0, i));
  }
  return d[t][k] = res;
}

int main() {
  int z, w;
  scanf("%d %d %d", &n, &z, &w);
  a[0] = w;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  n++;
  memset(d, -1, sizeof(d));
  printf("%d\n", f(0, 0));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

int a[2002], n, d[2][2002];

int f(int t, int k) {
  int res = abs(a[k] - a[n - 1]);
  if (d[t][k] >= 0)
    return d[t][k];
  for (int i = k + 1; i < n - 1; i++) {
    if (t == 0)
      res = max(res, f(1, i));
    else
      res = min(res, f(0, i));
  }
  return d[t][k] = res;
}

int main() {
  int z, w;
  scanf("%d %d %d", &n, &z, &w);
  a[0] = w;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  n++;
  memset(d, -1, sizeof(d));
  printf("%d\n", f(0, 0));
  return 0;
}
Problem p03552 (C++)

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

using namespace std;

int main() {
  ///{}
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, z, w;
  cin >> n >> z >> w;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> bdis(n);
  for (int i = 0; i < n - 2; i++) {
    int mn = INT_MAX;
    mn = min(mn, abs(v[i + 1] - v[n - 1]));
    bdis[i] = mn;
  }
  bdis[n - 2] = INT_MAX;
  int bst = INT_MAX;
  for (int i = n - 3; i >= 0; i--) {
    bst = min(bst, bdis[i]);
    bdis[i] = bst;
  }
  for (int i = n - 2; i >= 0; i--) {
    bdis[i] = min(bdis[i], abs(v[i] - v[n - 1]));
  }
  int ans = abs(v[n - 1] - w);
  for (int i = 0; i < n - 1; i++)
    ans = max(ans, bdis[i]);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ///{}
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, z, w;
  cin >> n >> z >> w;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (n == 1) {
    cout << abs(v[0] - w);
    return 0;
  }
  vector<int> bdis(n);
  for (int i = 0; i < n - 2; i++) {
    int mn = INT_MAX;
    mn = min(mn, abs(v[i + 1] - v[n - 1]));
    bdis[i] = mn;
  }
  bdis[n - 2] = INT_MAX;
  int bst = INT_MAX;
  for (int i = n - 3; i >= 0; i--) {
    bst = min(bst, bdis[i]);
    bdis[i] = bst;
  }
  for (int i = n - 2; i >= 0; i--) {
    bdis[i] = min(bdis[i], abs(v[i] - v[n - 1]));
  }
  int ans = abs(v[n - 1] - w);
  for (int i = 0; i < n - 1; i++)
    ans = max(ans, bdis[i]);
  cout << ans;
  return 0;
}
Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n;
ll a[200];
ll Max = INT_MIN;
bool b[105];

void dfs(int p, ll c) {
  if (p == n + 1) {
    Max = max(Max, c);
    return;
  }
  if (b[p] == 1)
    dfs(p + 1, c);
  else {
    map<int, int> d;
    for (int j = p; j <= n; j += p) {
      d[j] = b[j];
      b[j] = 1;
    }
    dfs(p + 1, c);
    for (int j = p; j <= n; j += p) {
      b[j] = d[j];
    }
    dfs(p + 1, c + a[p]);
  }
}
int main() {
  /*for(int i=1;i<=100;i++){
          int cnt=0;
          for(int j=1;j<=100;j++){
                  if(i%j==0)cnt++;
          }
          cout<<i<<' '<<cnt<<endl;
  }*/
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1, 0);
  cout << Max << endl;
}

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

int n;
ll a[200];
ll Max = INT_MIN;
bool b[105];

void dfs(int p, ll c) {
  if (p == n + 1) {
    Max = max(Max, c);
    return;
  }
  ll cnt = 0;
  for (int i = p; i <= n; i++) {
    if (!b[i] && a[i] > 0)
      cnt += a[i];
  }
  if (c + cnt < Max)
    return;
  if (b[p] == 1)
    dfs(p + 1, c);
  else {
    map<int, int> d;
    for (int j = p; j <= n; j += p) {
      d[j] = b[j];
      b[j] = 1;
    }
    dfs(p + 1, c);
    for (int j = p; j <= n; j += p) {
      b[j] = d[j];
    }
    dfs(p + 1, c + a[p]);
  }
}
int main() {
  /*for(int i=1;i<=100;i++){
          int cnt=0;
          for(int j=1;j<=100;j++){
                  if(i%j==0)cnt++;
          }
          cout<<i<<' '<<cnt<<endl;
  }*/
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1, 0);
  cout << Max << endl;
}
Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD1 = 1000000007;
const ll MOD2 = 1000000021;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int N;
ll A[101];
bool dead[101];
ll ans;

void dfs(int pos, ll now) {
  if (pos == N + 1) {
    ans = max(ans, now);
    return;
  }
  if (dead[pos]) {
    dfs(pos + 1, now);
    return;
  }
  dfs(pos + 1, now + A[pos]);
  if (A[pos] >= 0)
    return;
  bool tmp[100];
  for (int i = pos; i <= N; i++)
    tmp[i] = dead[i];
  for (int i = pos; i <= N; i += pos) {
    dead[i] = true;
  }
  dfs(pos + 1, now);
  for (int i = pos; i <= N; i++)
    dead[i] = tmp[i];
}

int main() {
  cin >> N;
  rep(i, N) cin >> A[i + 1];
  dfs(1, 0);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD1 = 1000000007;
const ll MOD2 = 1000000021;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int N;
ll A[101];
bool dead[101];
ll ans;

void dfs(int pos, ll now) {
  if (pos == N + 1) {
    ans = max(ans, now);
    return;
  }
  if (dead[pos]) {
    dfs(pos + 1, now);
    return;
  }
  dfs(pos + 1, now + A[pos]);
  if (A[pos] >= 0)
    return;
  bool tmp[100];
  for (int i = pos; i <= N; i++)
    tmp[i] = dead[i];
  for (int i = pos; i <= N; i += pos) {
    dead[i] = true;
  }
  ll cnt = 0;
  for (int i = pos; i <= N; i++) {
    if (!dead[i] && A[i] > 0)
      cnt += A[i];
  }
  if (now + cnt > ans)
    dfs(pos + 1, now);
  for (int i = pos; i <= N; i++)
    dead[i] = tmp[i];
}

int main() {
  cin >> N;
  rep(i, N) cin >> A[i + 1];
  dfs(1, 0);
  cout << ans << endl;
}
Problem p03554 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct tree {
  int minv[500000], n;
  tree(int size) : n(size) { memset(minv, 0x3f, sizeof minv); }
  void update(int p, int x, int o, int l, int r) {
    if (l == r) {
      minv[o] = min(minv[o], x);
      return;
    }
    int m = (l + r) >> 1;
    if (p <= m)
      update(p, x, o * 2, l, m);
    else
      update(p, x, o * 2 + 1, m + 1, r);
    minv[o] = min(minv[o * 2], minv[o * 2 + 1]);
  }
  int query(int x, int y, int o, int l, int r) {
    if (x <= l && r <= y)
      return minv[o];
    int m = (l + r) >> 1, ans = 1 << 28;
    if (x <= m)
      ans = min(ans, query(x, y, o * 2, l, m));
    if (y > m)
      ans = min(ans, query(x, y, o * 2 + 1, m + 1, r));
    return ans;
  }
  void set(int p, int x) { update(p, x, 1, 0, n); }
  int get(int l, int r) { return query(l, r, 1, 0, n); }
};
vector<int> v[300000];
int f[300000], b[300000], n, q;
int main() {
  scanf("%d", &n);
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", b + i), cnt += !b[i], b[i] = b[i] ? 1 : -1;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    v[l].push_back(r);
  }
  tree t(n);
  t.set(0, 0);
  for (int i = 1; i <= n; i++) {
    for (int j : v[i])
      t.set(j, t.get(i - 1, j));
    t.set(i, min(t.get(i, i), t.get(i - 1, i - 1) + b[i]));
  }
  printf("%d\n", cnt + t.get(n, n));
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct tree {
  int minv[600000], n;
  tree(int size) : n(size) { memset(minv, 0x3f, sizeof minv); }
  void update(int p, int x, int o, int l, int r) {
    if (l == r) {
      minv[o] = min(minv[o], x);
      return;
    }
    int m = (l + r) >> 1;
    if (p <= m)
      update(p, x, o * 2, l, m);
    else
      update(p, x, o * 2 + 1, m + 1, r);
    minv[o] = min(minv[o * 2], minv[o * 2 + 1]);
  }
  int query(int x, int y, int o, int l, int r) {
    if (x <= l && r <= y)
      return minv[o];
    int m = (l + r) >> 1, ans = 1 << 28;
    if (x <= m)
      ans = min(ans, query(x, y, o * 2, l, m));
    if (y > m)
      ans = min(ans, query(x, y, o * 2 + 1, m + 1, r));
    return ans;
  }
  void set(int p, int x) { update(p, x, 1, 0, n); }
  int get(int l, int r) { return query(l, r, 1, 0, n); }
};
vector<int> v[300000];
int f[300000], b[300000], n, q;
int main() {
  scanf("%d", &n);
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", b + i), cnt += !b[i], b[i] = b[i] ? 1 : -1;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    v[l].push_back(r);
  }
  tree t(n);
  t.set(0, 0);
  for (int i = 1; i <= n; i++) {
    for (int j : v[i])
      t.set(j, t.get(i - 1, j));
    t.set(i, min(t.get(i, i), t.get(i - 1, i - 1) + b[i]));
  }
  printf("%d\n", cnt + t.get(n, n));
  return 0;
}
Problem p03555 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

typedef pair<int, int> s;
typedef long long int ll;

int abs(int x) { return x > 0 ? x : -x; }

int length(char c[]) {
  int cnt = 0;
  for (cnt = 0; c[cnt] != '\0'; cnt++) {
  }
  return cnt;
}

char c[2][4];

int main() {
  scanf("%s", c[0]);
  scanf("%s", c[1]);
  REP(i, 3) {
    if (c[0][i] == c[1][2 - i])
      continue;
    printf("NO\n");
    return 1;
  }
  printf("YES\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

typedef pair<int, int> s;
typedef long long int ll;

int abs(int x) { return x > 0 ? x : -x; }

int length(char c[]) {
  int cnt = 0;
  for (cnt = 0; c[cnt] != '\0'; cnt++) {
  }
  return cnt;
}

char c[2][4];

int main() {
  scanf("%s", c[0]);
  scanf("%s", c[1]);
  REP(i, 3) {
    if (c[0][i] == c[1][2 - i])
      continue;
    printf("NO\n");
    return 0;
  }
  printf("YES\n");
  return 0;
}
Problem p03555 (C++)

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

int main() {
  vector<vector<char>> C(2, vector<char>(3));
  rep(i, 2) { rep(j, 3) cin >> C.at(i).at(j); }
  if (C.at(1).at(1) == C.at(2).at(3) && C.at(1).at(2) == C.at(2).at(2) &&
      C.at(1).at(3) == C.at(2).at(1))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

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

int main() {
  vector<vector<char>> C(2, vector<char>(3));
  rep(i, 2) { rep(j, 3) cin >> C.at(i).at(j); }
  if (C.at(0).at(0) == C.at(1).at(2) && C.at(0).at(1) == C.at(1).at(1) &&
      C.at(0).at(2) == C.at(1).at(0))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03555 (C++)

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

int main() {
  string a;
  string b;
  if (a.at(2) == b.at(0) && a.at(0) == b.at(2) && a.at(1) == b.at(1))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


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

int main() {
  string a;
  string b;
  cin >> a >> b;
  if (a.at(2) == b.at(0) && a.at(0) == b.at(2) && a.at(1) == b.at(1))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03555 (C++)

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

using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    if (a.at(i) != b.at(5 - i))
      ans = 1;
  }
  if (ans)
    cout << "NO";
  else
    cout << "YES";
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    if (a.at(i) != b.at(2 - i))
      ans = 1;
  }
  if (ans)
    cout << "NO";
  else
    cout << "YES";
}
Problem p03555 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 30;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9)
// memset(a, 0, sizeof(a));
//--------------------------

string s1, s2;

int main() {
  cin >> s1 >> s2;
  reverse(s1.begin(), s2.begin());
  if (s1 == s2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 30;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9)
// memset(a, 0, sizeof(a));
//--------------------------

string s1, s2;

int main() {
  cin >> s1 >> s2;

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

  if (s1 == s2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}