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

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  //
  int N, sum = 0;
  cin >> N;
  vector<int> s(N);
  for (int i = 0; i < N; ++i) {
    cin >> s[i];
    sum += s[i];
  }

  //
  vector<bool> table(sum + 1, false);
  table[0] = true;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < sum; ++j) {
      if (table[j])
        table[j + s[i]] = true;
    }
  }

  for (int j = sum; j >= 0; --j) {
    if (table[j] && j % 10 != 0) {
      cout << j << endl;
      return 0;
    }
  }

  cout << 0 << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  //
  int N, sum = 0;
  cin >> N;
  vector<int> s(N);
  for (int i = 0; i < N; ++i) {
    cin >> s[i];
    sum += s[i];
  }

  //
  vector<bool> table(sum + 1, false);
  table[0] = true;
  for (int i = 0; i < N; ++i) {
    for (int j = sum - s[i]; j >= 0; --j) {
      if (table[j])
        table[j + s[i]] = true;
    }
  }

  for (int j = sum; j >= 0; --j) {
    if (table[j] && j % 10 != 0) {
      cout << j << endl;
      return 0;
    }
  }

  cout << 0 << endl;

  return 0;
}
Problem p03699 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, s[100], ans, ok;
int backtrack(int sol, int i) {
  if (i == -1) {
    if (sol % 10 == 0)
      return 0;
    return sol;
  }
  return max(backtrack(sol + s[i], i - 1), backtrack(sol, i - 1));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (!ok && s[i] % 10 != 0)
      ok = true;
  }
  if (!ok)
    cout << 0;
  else {
    sort(s, s + n);
    ok = 0;
    cout << backtrack(0, n - 1);
    /*for(int i=n-1;i>=0;i--)ans+=s[i];
    int i=0;
    while(ans%10==0&&i<n)
    {
        ans-=s[i];
        i++;
    }*/
    // cout<<ans;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, s[100], ans, ok;
int backtrack(int sol, int i) {
  if (i == -1) {
    if (sol % 10 == 0)
      return 0;
    return sol;
  }
  return max(backtrack(sol + s[i], i - 1), backtrack(sol, i - 1));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (!ok && s[i] % 10 != 0)
      ok = true;
  }
  if (!ok)
    cout << 0;
  else {
    sort(s, s + n);
    ok = 0;
    // cout<<backtrack(0,n-1);
    for (int i = n - 1; i >= 0; i--)
      ans += s[i];
    if (ans % 10 == 0) {
      int i;
      for (i = 0; i < n; i++) {
        if ((ans - s[i]) % 10 != 0)
          break;
      }
      cout << ans - s[i];
    } else
      cout << ans;
    // cout<<ans;
  }
}
Problem p03699 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int n;
  cin >> n;
  int sum = 0;
  vector<int> s;
  rep(i, n) {
    int tmp_s;
    cin >> tmp_s;
    sum += tmp_s;
    if (tmp_s % 10 != 0) {
      s.push_back(tmp_s);
    }
  }
  sort(s.begin(), s.end());
  if (sum % 10 != 0) {
    cout << sum << endl;
    return 0;
  }
  rep(i, n) {
    sum -= s[i];
    if (sum % 10 != 0) {
      cout << sum << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int n;
  cin >> n;
  int sum = 0;
  vector<int> s;
  rep(i, n) {
    int tmp_s;
    cin >> tmp_s;
    sum += tmp_s;
    if (tmp_s % 10 != 0) {
      s.push_back(tmp_s);
    }
  }
  sort(s.begin(), s.end());
  if (sum % 10 != 0) {
    cout << sum << endl;
    return 0;
  }
  rep(i, s.size()) {
    sum -= s[i];
    if (sum % 10 != 0) {
      cout << sum << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p03699 (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 main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> s(n);
  REP(i, n) cin >> s[i];

  vector<vector<bool>> dp(n + 1, vector<bool>(10001, false));

  dp[0][0] = true;

  REP(i, n) REP(j, 100001) {
    if (dp[i][j]) {
      dp[i + 1][j] = true;
      dp[i + 1][j + s[i]] = true;
    }
  }

  int ans = 0;
  REP(i, 10001)
  if (i % 10 && dp[n][i])
    ans = max(ans, i);

  cout << ans << '\n';
  return 0;
}


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

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

  int n;
  cin >> n;

  vector<int> s(n);
  REP(i, n) cin >> s[i];

  vector<vector<bool>> dp(n + 1, vector<bool>(10001, false));

  dp[0][0] = true;

  REP(i, n) REP(j, 10001) {
    if (dp[i][j]) {
      dp[i + 1][j] = true;
      dp[i + 1][j + s[i]] = true;
    }
  }

  int ans = 0;
  REP(i, 10001)
  if (i % 10 && dp[n][i])
    ans = max(ans, i);

  cout << ans << '\n';
  return 0;
}
Problem p03699 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <iostream>
#include <stack>
// #include <conio.h>

using namespace std;

int n = 0;
int input[100] = {};
bool done[102][102] = {};
int nowMax = 0;

void Search(int num, int score);
void Record(int num);

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input[i];
  }
  Search(0, 0);
  cout << nowMax << endl;
  // getch();
  return 0;
}

void Search(int num, int score) {
  if (num == n) {
    Record(score);
    return;
  }
  if (done[num][score] == true) {
    return;
  }
  done[num][score] = true;
  Search(num + 1, score + input[num]);
  Search(num + 1, score);
}

void Record(int num) {
  if (num % 10 == 0) {
    return;
  }
  if (num > nowMax) {
    nowMax = num;
  }
}

Fixed submission:
#include <climits>
#include <iostream>
#include <stack>
// #include <conio.h>

using namespace std;

int n = 0;
int input[100] = {};
bool done[102][10002] = {};
int nowMax = 0;

void Search(int num, int score);
void Record(int num);

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input[i];
  }
  Search(0, 0);
  cout << nowMax << endl;
  // getch();
  return 0;
}

void Search(int num, int score) {
  if (num == n) {
    Record(score);
    return;
  }
  if (done[num][score] == true) {
    return;
  }
  done[num][score] = true;
  Search(num + 1, score + input[num]);
  Search(num + 1, score);
}

void Record(int num) {
  if (num % 10 == 0) {
    return;
  }
  if (num > nowMax) {
    nowMax = num;
  }
}
Problem p03699 (C++)

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

using namespace std;

#define ll long long
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define rep0(i, n) for (int i = 0; i < (n); i++)

const ll INF = 1e9 + 7;
int n, k = 1, g, a[101];
int main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    g += a[i];
  }
  if (g % 10 != 0) {
    cout << g;
    return 0;
  }
  sort(a + 1, a + n + 1);
  while (a[k] % 10 == 0) {
    k++;
  }
  if (k > n) {
    cout << 0;
  } else {
    cout << g - a[k];
  }
  return 0;
}

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

using namespace std;

#define ll long long
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define rep0(i, n) for (int i = 0; i < (n); i++)

const ll INF = 1e9 + 7;
int n, k = 1, g, a[101];
int main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    g += a[i];
  }
  if (g % 10 != 0) {
    cout << g;
    return 0;
  }
  sort(a + 1, a + n + 1);
  while (a[k] % 10 == 0 && k <= n) {
    k++;
  }
  if (k > n) {
    cout << 0;
  } else {
    cout << g - a[k];
  }
  return 0;
}
Problem p03699 (C++)

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

using namespace std;

vector<int> v;
int N;

int main() {
  int sum = 0;
  cin >> N;
  v.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
    sum += v[i];
  }
  if (sum % 10 != 0) {
    cout << sum << endl;
    return sum;
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < N; i++) {
    if (v[i] % 10 != 0) {
      cout << sum - v[i] << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

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

using namespace std;

vector<int> v;
int N;

int main() {
  int sum = 0;
  cin >> N;
  v.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
    sum += v[i];
  }
  if (sum % 10 != 0) {
    cout << sum << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < N; i++) {
    if (v[i] % 10 != 0) {
      cout << sum - v[i] << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p03699 (C++)

Buggy submission (status: Time Limit Exceeded):
// C - Bugged: 8Min;
// Assunto: DP;
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define loopBack(i, a, b) for (int i = a; i >= b; i--)
#define INF INT_MAX
#define LINF LLONG_MAX
#define DINF DBL_MAX
#define MAX 100050
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

int n, ans = 0;
bool dp[105][10005];
void solve(int idx, int soma, vector<int> &points) {
  if (idx >= n) {
    if (soma % 10 != 0)
      ans = max(ans, soma);
    return;
  }
  if (dp[idx][soma])
    return;
  solve(idx + 1, soma, points);
  solve(idx + 1, soma + points[idx], points);
  dp[idx][soma] = false;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> n;
  vector<int> points(n);
  loop(i, 0, n) cin >> points[i];
  solve(0, 0, points);
  cout << ans << "\n";
}

Fixed submission:
// C - Bugged: 8Min;
// Assunto: DP;
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define loopBack(i, a, b) for (int i = a; i >= b; i--)
#define INF INT_MAX
#define LINF LLONG_MAX
#define DINF DBL_MAX
#define MAX 100050
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

int n, ans = 0;
bool dp[105][10005];
void solve(int idx, int soma, vector<int> &points) {
  if (idx >= n) {
    if (soma % 10 != 0)
      ans = max(ans, soma);
    return;
  }
  if (dp[idx][soma])
    return;
  solve(idx + 1, soma, points);
  solve(idx + 1, soma + points[idx], points);
  dp[idx][soma] = true;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> n;
  vector<int> points(n);
  loop(i, 0, n) cin >> points[i];
  solve(0, 0, points);
  cout << ans << "\n";
}
Problem p03699 (C++)

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

bool pt[10005];

int main() {
  int N;
  scanf("%d", &N);
  int S[N], sm = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &S[i]);
    sm += S[i];
  }
  sort(S, S + N);
  pt[0] = true;
  vector<int> P;
  P.push_back(0);
  for (int i = 0; i < N; ++i) {
    int sz = P.size();
    for (int j = 0; j < sz; ++j) {
      int k = P[j];
      pt[k + S[i]] = true;
      P.push_back(k + S[i]);
    }
  }
  for (int i = sm; i >= 0; --i)
    if (pt[i] && i % 10) {
      printf("%d\n", i);
      return 0;
    }
  printf("0\n");
  return 0;
}

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

bool pt[10005];

int main() {
  int N;
  scanf("%d", &N);
  int S[N], sm = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &S[i]);
    sm += S[i];
  }
  sort(S, S + N);
  pt[0] = true;
  vector<int> P;
  P.push_back(0);
  for (int i = 0; i < N; ++i) {
    int sz = P.size();
    for (int j = 0; j < sz; ++j) {
      int k = P[j] + S[i];
      if (pt[k])
        continue;
      pt[k] = true;
      P.push_back(k);
    }
  }
  for (int i = sm; i >= 0; --i)
    if (pt[i] && i % 10) {
      printf("%d\n", i);
      return 0;
    }
  printf("0\n");
  return 0;
}
Problem p03699 (C++)

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

using namespace std;

int main() {
  vector<int> p;
  int dp[101][1001];
  for (int i = 0; i < 1001; i++)
    dp[0][i] = 0;
  int n;
  cin >> n;
  int maxp = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    maxp += t;
    p.push_back(t);
  }

  // dp
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= maxp; j++) {
      if (j - p[i - 1] >= 0) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i - 1]] + p[i - 1]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

  int res = 0;
  for (int i = 0; i <= maxp; i++) {
    if (dp[n][i] % 10 == 0)
      continue;

    if (dp[n][i] > res)
      res = dp[n][i];
  }

  cout << res << endl;
  return 0;
}

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

using namespace std;

int main() {
  vector<int> p;
  int dp[101][10001];
  for (int i = 0; i < 10001; i++)
    dp[0][i] = 0;
  int n;
  cin >> n;
  int maxp = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    maxp += t;
    p.push_back(t);
  }

  // dp
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= maxp; j++) {
      if (j - p[i - 1] >= 0) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i - 1]] + p[i - 1]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

  int res = 0;
  for (int i = 0; i <= maxp; i++) {
    if (dp[n][i] % 10 == 0)
      continue;

    if (dp[n][i] > res)
      res = dp[n][i];
  }

  cout << res << endl;
  return 0;
}
Problem p03699 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef long long LL;

const LL INF = 1 << 30;
const LL MOD = 1000000007;

LL gcd(LL a, LL b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  int sowa = 0;
  vector<int> trlis;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    sowa += s[i];
    if (s[i] % 10 != 0)
      trlis.push_back(trlis[i]);
  }

  if (trlis.size() == 0)
    cout << 0 << endl;
  else {

    sort(trlis.begin(), trlis.end());
    int i = 0;
    while (sowa % 10 == 0) {
      sowa -= trlis[i];
      i++;
    }
    cout << sowa << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef long long LL;

const LL INF = 1 << 30;
const LL MOD = 1000000007;

LL gcd(LL a, LL b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  int sowa = 0;
  vector<int> trlis;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    sowa += s[i];
    if (s[i] % 10 != 0)
      trlis.push_back(s[i]);
  }

  if (trlis.size() == 0)
    cout << 0 << endl;
  else {

    sort(trlis.begin(), trlis.end());
    int i = 0;
    while (sowa % 10 == 0) {
      sowa -= trlis[i];
      i++;
    }
    cout << sowa << endl;
  }
}
Problem p03699 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = [int(input()) for _ in range(n)]


# total:累計, i:index
def dfs(total, i):
    if i == n:
        if total % 10 != 0:
            return total
        else:
            return 0
    return max(dfs(total, i + 1), dfs(total + s[i], i + 1))


print(dfs(0, 0))


Fixed submission:
n = int(input())
s = [int(input()) for _ in range(n)]

total = sum(s)
s.sort()
if total % 10 != 0:
    print(total)
else:
    for i in range(n):
        if (total - s[i]) % 10 != 0:
            print(total - s[i])
            exit()
    print(0)
Problem p03699 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
S = sorted([int(input()) for _ in range(N)])
s = sum(S)
if s % 10 != 0:
    print(s)
    exit()

for si in range(S):
    if si % 10 != 0:
        print(s - si)
        exit()

print(0)


Fixed submission:
N = int(input())
S = sorted([int(input()) for _ in range(N)])
s = sum(S)
if s % 10 != 0:
    print(s)
    exit()

for si in S:
    if si % 10 != 0:
        print(s - si)
        exit()

print(0)
Problem p03699 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int ans = 0;
  priority_queue<int, vector<int>, greater<int>> pq;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    ans += tmp;
    if (tmp % 10 != 0) {
      pq.push(tmp);
    }
  }
  while (ans % 10 == 0) {
    ans -= pq.top();
    pq.pop();
    if (ans == 0)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int ans = 0;
  priority_queue<int, vector<int>, greater<int>> pq;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    ans += tmp;
    if (tmp % 10 != 0) {
      pq.push(tmp);
    }
  }
  if (ans % 10 == 0) {
    if (!pq.empty()) {
      ans -= pq.top();
      pq.pop();
    } else {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p03700 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
using namespace std;

const int N_MAX = int(2e5);
int N;
long A, B;
long h[N_MAX];

// T回で、全て倒すことが可能かどうか
bool enough(long T) {
  long s = 0;
  REP(i, 0, N) {
    long r = h[i] - B * T;
    // もし r <= 0 ならば、これだけで倒せる
    if (r > 0) {
      double t = double(r) / (A - B);
      // t を切り上げる
      s += long(ceil(t));
    }
  }
  // s が T 以下ならば可能
  return (s <= T);
}

// enough(T) を満たす最小の T を探索する
// そのとき、
// enough(T) かつ !enough(T-1) が成り立つ
long bsearch(long a, long b) {
  assert(a < b);

  long c = (a + b) / 2;
  if (enough(c)) {
    // c が最小と分かる
    if (!enough(c - 1))
      return c;
    // 十分過ぎるケース
    // もっと左を探す
    return bsearch(a, c);
  }
  // 右を探す
  return bsearch(c + 1, b);
}

int main() {
  cin >> N >> A >> B;
  REP(i, 0, N) cin >> h[i];

  // h[i] の最大値
  long hmax = -1;
  REP(i, 0, N) {
    if (h[i] > hmax)
      hmax = h[i];
  }

  // B だけで hmax を倒すのに十分な回数
  // を b とすれば、答えはこれ以下になるはず。
  long b = long(ceil(double(hmax) / B));

  long T = bsearch(1, b);
  cout << T << endl;

  return 0;
}


Fixed submission:
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
using namespace std;

const int N_MAX = int(2e5);
int N;
long A, B;
long h[N_MAX];

// T回で、全て倒すことが可能かどうか
bool enough(long T) {
  long s = 0;
  REP(i, 0, N) {
    long r = h[i] - B * T;
    // もし r <= 0 ならば、これだけで倒せる
    if (r > 0) {
      double t = double(r) / (A - B);
      // t を切り上げる
      s += long(ceil(t));
    }
  }
  // s が T 以下ならば可能
  return (s <= T);
}

// enough(T) を満たす最小の T を探索する
// そのとき、
// enough(T) かつ !enough(T-1) が成り立つ
long bsearch(long a, long b) {
  assert(a <= b);

  long c = (a + b) / 2;
  if (enough(c)) {
    // c が最小と分かる
    if (!enough(c - 1))
      return c;
    // 十分過ぎるケース
    // もっと左を探す
    return bsearch(a, c);
  }
  // 右を探す
  return bsearch(c + 1, b);
}

int main() {
  cin >> N >> A >> B;
  REP(i, 0, N) cin >> h[i];

  // h[i] の最大値
  long hmax = -1;
  REP(i, 0, N) {
    if (h[i] > hmax)
      hmax = h[i];
  }

  // B だけで hmax を倒すのに十分な回数
  // を b とすれば、答えはこれ以下になるはず。
  long b = long(ceil(double(hmax) / B));

  long T = bsearch(1, b);
  cout << T << endl;

  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
typedef long long ll;

ll h[N];
ll n, a, b;
bool check(ll x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    if (h[i] - x * b <= 0)
      continue;
    sum += (h[i] - x * b) / (a - b);
    if ((h[i] - x * b) % (a - b) != 0)
      sum++;
  }
  if (sum > x)
    return false;
  return true;
}
int main() {

  cin >> n >> a >> b;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ll ans = INT_MAX;
  ll l = 0, r = 1e9;
  while (r >= l) {
    ll mid = l + r >> 1;
    if (check(mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
typedef long long ll;

ll h[N];
ll n, a, b;
bool check(ll x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    if (h[i] - x * b <= 0)
      continue;
    sum += (h[i] - x * b) / (a - b);
    if ((h[i] - x * b) % (a - b) != 0)
      sum++;
  }
  if (sum > x)
    return false;
  return true;
}
int main() {

  cin >> n >> a >> b;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ll ans = INT_MAX;
  ll l = 0, r = 1e9;
  while (r >= l) {
    ll mid = l + r >> 1;
    if (check(mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03700 (Python)

Buggy submission (status: Time Limit Exceeded):
n, a, b = map(int, input().split())
h = [int(input()) for i in range(n)]

left = 0
right = 10**9
mid = (left + right) // 2
while right - left > 1:
    mid = (left + right) // 2
    count = 0
    for i in range(n):
        if h[i] - mid * b > 0:
            if (h[i] - mid * b) % (a - b) == 0:
                count += (h[i] - mid * b) // (a - b)
            else:
                count += (h[i] - mid * b) // (a - b) + 1
    if mid - count >= 0:
        right = mid
    else:
        left = mid
print(right)


Fixed submission:
n, a, b = map(int, input().split())
h = [int(input()) for i in range(n)]

left = 0
right = 10**10
mid = (left + right) // 2
while right - left > 1:
    mid = (left + right) // 2
    count = 0
    for i in range(n):
        if h[i] - mid * b > 0:
            if (h[i] - mid * b) % (a - b) == 0:
                count += (h[i] - mid * b) // (a - b)
            else:
                count += (h[i] - mid * b) // (a - b) + 1
    if mid - count >= 0:
        right = mid
    else:
        left = mid
print(right)
Problem p03700 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pint;

ll h[10001], n, a, b;
bool test(ll m) {
  ll res = 0;
  rep(i, n) {
    if (h[i] - b * m <= 0)
      continue;
    int u = 0;
    if ((h[i] - b * m) % (a - b) != 0)
      u = 1;
    res += (h[i] - b * m) / (a - b) + u;
  }
  if (res > m)
    return true;
  else
    return false;
}
int main() {
  int hi;
  cin >> n >> a >> b;
  rep(i, n) { cin >> h[i]; }
  ll lb = 0, ub = 1000100010;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (test(mid))
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pint;

ll h[100001], n, a, b;
bool test(ll m) {
  ll res = 0;
  rep(i, n) {
    if (h[i] - b * m <= 0)
      continue;
    int u = 0;
    if ((h[i] - b * m) % (a - b) != 0)
      u = 1;
    res += (h[i] - b * m) / (a - b) + u;
  }
  if (res > m)
    return true;
  else
    return false;
}
int main() {
  int hi;
  cin >> n >> a >> b;
  rep(i, n) { cin >> h[i]; }
  ll lb = 0, ub = 1000100010;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (test(mid))
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;
  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
#define eps 1e-7
#define maxi 50005
long long h[maxi];
long long hi[maxi];
long long n, a, b;
long long in = 10000000000;
bool C(long long x) {
  long long t = x;
  for (long long i = 0; i < n; i++)
    hi[i] = h[i];
  for (long long i = 0; i < n; i++) {
    hi[i] -= x * b;
    if (hi[i] <= 0)
      continue;
    long long y;
    if (hi[i] % (a - b) == 0)
      y = hi[i] / (a - b);
    else
      y = hi[i] / (a - b) + 1;
    t -= y;
  }
  if (t >= 0)
    return true;
  return false;
}
void solve() {
  sort(h, h + n);
  long long lb = 0, ub = in;
  while (ub - lb > 1) {
    long long mid = (lb + ub) / 2;
    // cout<<mid<<" "<<C(mid)<<" "<<ub<<endl;
    if (C(mid))
      ub = mid;
    else
      lb = mid;
    // cout<<lb<<" "<<ub<<endl;
  }
  cout << ub << endl;
}
int main() {
  scanf("%d%d%d", &n, &a, &b);
  for (int i = 0; i < n; i++)
    scanf("%d", &h[i]);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
#define eps 1e-7
#define maxi 100005
long long h[maxi];
long long hi[maxi];
long long n, a, b;
long long in = 10000000000;
bool C(long long x) {
  long long t = x;
  for (long long i = 0; i < n; i++)
    hi[i] = h[i];
  for (long long i = 0; i < n; i++) {
    hi[i] -= x * b;
    if (hi[i] <= 0)
      continue;
    long long y;
    if (hi[i] % (a - b) == 0)
      y = hi[i] / (a - b);
    else
      y = hi[i] / (a - b) + 1;
    t -= y;
  }
  if (t >= 0)
    return true;
  return false;
}
void solve() {
  sort(h, h + n);
  long long lb = 0, ub = in;
  while (ub - lb > 1) {
    long long mid = (lb + ub) / 2;
    // cout<<mid<<" "<<C(mid)<<" "<<ub<<endl;
    if (C(mid))
      ub = mid;
    else
      lb = mid;
    // cout<<lb<<" "<<ub<<endl;
  }
  cout << ub << endl;
}
int main() {
  scanf("%d%d%d", &n, &a, &b);
  for (int i = 0; i < n; i++)
    scanf("%d", &h[i]);
  solve();
  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
const int INF = 100000000, mod = 1000000007;
using namespace std;

long long n, a, b, h[100000];

bool check(long long pos) {
  long long count = 0;
  for (int i = 0; i < n; i++) {
    long long tmp = h[i] - b * pos, damage = a - b;
    while (tmp > 0) {
      tmp -= damage;
      count++;
    }
  }
  return count <= pos;
}

int main() {
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  long long left = 0, right = 1000000000;
  while (right - left > 1) { // h[left]~h[right]
    long long pos = (left + right) / 2;
    if (check(pos))
      right = pos;
    else
      left = pos;
  }
  cout << right << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
const int INF = 100000000, mod = 1000000007;
using namespace std;

long long n, a, b, h[100000];

bool check(long long pos) {
  long long count = 0;
  for (int i = 0; i < n; i++) {
    long long tmp = h[i] - b * pos, damage = a - b;
    if (tmp % damage == 0 && tmp > 0)
      count += tmp / damage;
    else if (tmp % damage != 0 && tmp > 0)
      count += tmp / damage + 1;
  }
  return count <= pos;
}

int main() {
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  long long left = 0, right = 1000000000;
  while (right - left > 1) { // h[left]~h[right]
    long long pos = (left + right) / 2;
    if (check(pos))
      right = pos;
    else
      left = pos;
  }
  cout << right << endl;
  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#define REP(i, n) for (long(i) = 0; (i) < (n); (i)++)
#define REPI(i, a, b) for (long(i) = (a); (i) < (b); (i)++)
#define INF numeric_limits<double>::infinity()
constexpr long MOD = 1e9 + 7;
using namespace std;
using P = pair<long, long>;
using VI = vector<long>;

long n, a, b;
VI hs;

bool ok(long k) {
  VI s;
  for (auto h : hs) {
    if (h > b * k) {
      s.push_back(h - b * k);
    }
  }
  long cnt = 0;
  for (auto t : s) {
    cnt += long(ceil(double(t) / (a - b)));
  }
  return cnt <= k;
}

int main() {
  cin >> n >> a >> b;
  REP(i, n) {
    long h;
    cin >> h;
    hs.push_back(h);
  }
  sort(hs.begin(), hs.end());
  long k = long(ceil(double(hs.back()) / b));
  long l = 1;

  long hoge = 0;

  while (k != l) {
    hoge++;
    if (hoge > 1e6) {
      cout << k << endl;
      return 0;
    }
    if (ok((k + l) / 2)) {
      k = (k + l) / 2;
    } else {
      l = (k + l) / 2;
    }
  }
  cout << k << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#define REP(i, n) for (long(i) = 0; (i) < (n); (i)++)
#define REPI(i, a, b) for (long(i) = (a); (i) < (b); (i)++)
#define INF numeric_limits<double>::infinity()
constexpr long MOD = 1e9 + 7;
using namespace std;
using P = pair<long, long>;
using VI = vector<long>;

long n, a, b;
VI hs;

bool ok(long k) {
  VI s;
  for (auto h : hs) {
    if (h > b * k) {
      s.push_back(h - b * k);
    }
  }
  long cnt = 0;
  for (auto t : s) {
    cnt += long(ceil(double(t) / (a - b)));
  }
  return cnt <= k;
}

int main() {
  cin >> n >> a >> b;
  REP(i, n) {
    long h;
    cin >> h;
    hs.push_back(h);
  }
  sort(hs.begin(), hs.end());
  long k = long(ceil(double(hs.back()) / b));
  long l = 1;

  long hoge = 0;

  while (k != l) {
    hoge++;
    if (hoge > 36) {
      cout << k << endl;
      return 0;
    }
    if (ok((k + l) / 2)) {
      k = (k + l) / 2;
    } else {
      l = (k + l) / 2;
    }
  }
  cout << k << endl;
}
Problem p03700 (C++)

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

using namespace std;
int main() {
  int n, a, b, i;
  int h[12000];
  cin >> n >> a >> b;
  int diff = a - b;
  int left, right, current;
  int maxh = -1;
  long count = 0;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (maxh < h[i])
      maxh = h[i];
  };
  int maxt = maxh / b + 1;
  left = 1;
  right = maxt;
  while (left < right) {
    current = (left + right) / 2;
    count = 0;
    for (i = 0; i < n; i++) {
      count += max(0, (h[i] - current * b + diff - 1) / diff);
    };
    if (count > current) {
      left = current;
      if (right - left == 1) {
        cout << right;
        return 0;
      };
    } else if (count < current) {
      right = current;
    } else {
      cout << current;
      return 0;
    };
  };
  cout << left;
  return 0;
}

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

using namespace std;
int main() {
  int n, a, b, i;
  int h[120000];
  cin >> n >> a >> b;
  int diff = a - b;
  int left, right, current;
  int maxh = -1;
  long count = 0;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (maxh < h[i])
      maxh = h[i];
  };
  int maxt = maxh / b + 1;
  left = 1;
  right = maxt;
  while (left < right) {
    current = (left + right) / 2;
    count = 0;
    for (i = 0; i < n; i++) {
      count += max(0, (h[i] - current * b + diff - 1) / diff);
    };
    if (count > current) {
      left = current;
      if (right - left == 1) {
        cout << right;
        return 0;
      };
    } else if (count < current) {
      right = current;
    } else {
      cout << current;
      return 0;
    };
  };
  cout << left;
  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  // Here your code !
  long long int N, A, B, C, hiC, loC, sum;
  cin >> N >> A >> B;
  vector<long long int> H(N);
  vector<long long int> Ht(N);
  for (int i = 0; i < N; i++) {

    cin >> H[i];
  }

  sort(H.begin(), H.end());

  C = H[N - 1] / B;
  hiC = C;
  loC = 0;
  while (1) {
    sum = 0;
    for (int i = 0; i < N; i++) {

      Ht[i] = H[i] - C * B;
      if (Ht[i] > 0) {
        sum += (Ht[i] + A - B - 1) / (A - B);
      }
    }
    if (sum == C) {
      cout << C << "\n";
      return 0;
    }
    if (sum > C) {
      loC = C;
      C += (hiC - loC) / 2;
    } else {
      hiC = C;
      C -= (hiC - loC) / 2;
    }
    if (hiC - loC == 1) {

      cout << hiC << "\n";
      return 0;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  // Here your code !
  long long int N, A, B, C, hiC, loC, sum;
  cin >> N >> A >> B;
  vector<long long int> H(N);
  vector<long long int> Ht(N);
  for (int i = 0; i < N; i++) {

    cin >> H[i];
  }

  sort(H.begin(), H.end());

  C = H[N - 1] / B;
  hiC = C;
  loC = 0;
  while (1) {
    sum = 0;
    for (int i = 0; i < N; i++) {

      Ht[i] = H[i] - C * B;
      if (Ht[i] > 0) {
        sum += (Ht[i] + A - B - 1) / (A - B);
      }
    }
    if (sum == C) {
      cout << C << "\n";
      return 0;
    }
    if (sum > C) {
      loC = C;
      C += (hiC - loC) / 2;
    } else {
      hiC = C;
      C -= (hiC - loC) / 2;
    }
    if (hiC - loC == 1) {

      cout << hiC << "\n";
      return 0;
    }
    if (hiC - loC == 0) {

      cout << hiC + 1 << "\n";
      return 0;
    }
  }

  return 0;
}
Problem p03700 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int N;
int A, B;
vector<long> H(10000);

bool fi(long n) {
  long ans = 0;
  rep(i, N) {
    long rem = H.at(i) - n * B;
    if (rem > 0) {
      ans += rem / (A - B);
      if (rem % (A - B) > 0) {
        ans++;
      }
    }
  }
  // cout << "n: " << n << " ans: " << ans << endl;
  return (n >= ans);
}

int binary_search(int from, int to) {
  int pos = (from + to) / 2;
  if (from == to) {
    return from;
  }
  if (fi(pos)) {
    return binary_search(from, pos);
  } else {
    return binary_search(pos + 1, to);
  }
}

int main() {
  cin >> N >> A >> B;
  long ma = 0;
  rep(i, N) {
    cin >> H.at(i);
    ma = max(ma, H.at(i));
  }
  long inf = ma / B + 1;
  int ans = binary_search(0, inf);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int N;
int A, B;
vector<long> H(100005);

bool fi(long n) {
  long ans = 0;
  rep(i, N) {
    long rem = H.at(i) - n * B;
    if (rem > 0) {
      ans += rem / (A - B);
      if (rem % (A - B) > 0) {
        ans++;
      }
    }
  }
  // cout << "n: " << n << " ans: " << ans << endl;
  return (n >= ans);
}

int binary_search(int from, int to) {
  int pos = (from + to) / 2;
  if (from == to) {
    return from;
  }
  if (fi(pos)) {
    return binary_search(from, pos);
  } else {
    return binary_search(pos + 1, to);
  }
}

int main() {
  cin >> N >> A >> B;
  long ma = 0;
  rep(i, N) {
    cin >> H.at(i);
    ma = max(ma, H.at(i));
  }
  long inf = ma / B + 1;
  int ans = binary_search(0, inf);
  cout << ans << endl;
}
Problem p03700 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define int long long

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a) for (int i = (a); i >= 0; --i)
#define CLR(a) memset((a), 0, sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF (3e15)

using namespace std;

int N, A, B;
int H[100005];

int solve(int t) {
  int ans = 0;
  REP(i, N) {
    int hp = H[i] - B * t;
    if (hp <= 0)
      continue;
    ans += hp / (A - B);
    if (hp % (A - B) != 0)
      ans++;
  }
  return ans <= t;
}

signed main() {
  cin >> N >> A >> B;
  REP(i, N) cin >> H[i];
  int left = 0;
  int right = 200000000;
  while (right - left >= 1) {
    int mid = (right + left) / 2;
    if (solve(mid))
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define int long long

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a) for (int i = (a); i >= 0; --i)
#define CLR(a) memset((a), 0, sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF (3e15)

using namespace std;

int N, A, B;
int H[100005];

int solve(int t) {
  int ans = 0;
  REP(i, N) {
    int hp = H[i] - B * t;
    if (hp <= 0)
      continue;
    ans += hp / (A - B);
    if (hp % (A - B) != 0)
      ans++;
  }
  return ans <= t;
}

signed main() {
  cin >> N >> A >> B;
  REP(i, N) cin >> H[i];
  int left = 0;
  int right = 2000000000;
  while (right - left > 1) {
    int mid = (right + left) / 2;
    if (solve(mid))
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}
Problem p03700 (C++)

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

#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL

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

using namespace std;
using ll = long long;

#define NMAX 10000

int h[NMAX];
int N, A, B;

bool check(int T) {
  ll count = 0;
  rep(i, N) {
    int h_ex = h[i] - B * T;
    if (h_ex <= 0) {
      continue;
    }
    count += (h_ex + A - B - 1) / (A - B);
  }
  return count <= T;
}

int main() {
  cin >> N >> A >> B;
  int h_max = 0;
  rep(i, N) {
    cin >> h[i];
    if (h[i] > h_max) {
      h_max = h[i];
    }
  }

  int Tmin = 1;
  int Tmax = h_max / B + 1;
  while (Tmax > Tmin) {
    int Tmid = (Tmin + Tmax) / 2;

    if (check(Tmid)) {
      Tmax = Tmid;
    } else {
      Tmin = Tmid + 1;
    }
  }

  cout << Tmin << endl;
}

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

#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL

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

using namespace std;
using ll = long long;

#define NMAX 100000

int h[NMAX];
int N, A, B;

bool check(int T) {
  ll count = 0;
  rep(i, N) {
    int h_ex = h[i] - B * T;
    if (h_ex <= 0) {
      continue;
    }
    count += (h_ex + A - B - 1) / (A - B);
  }
  return count <= T;
}

int main() {
  cin >> N >> A >> B;
  int h_max = 0;
  rep(i, N) {
    cin >> h[i];
    if (h[i] > h_max) {
      h_max = h[i];
    }
  }

  int Tmin = 1;
  int Tmax = h_max / B + 1;
  while (Tmax > Tmin) {
    int Tmid = (Tmin + Tmax) / 2;

    if (check(Tmid)) {
      Tmax = Tmid;
    } else {
      Tmin = Tmid + 1;
    }
  }

  cout << Tmin << endl;
}
Problem p03701 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define INT long long int
#define MOD 1000000007
#define oo 978654321
#define OO 9999999987654321ll
using namespace std;
int n, s[101], dt[101][10010];
int dp(int x, int y) {
  if (x == n)
    return y % 10 ? y : 0;
  if (~dt[x][y])
    return dt[x][y];
  int &ret = dt[x][y];
  return max(dp(x + 1, y), dp(x + 1, y + s[x]));
}
int main() {
  ios::sync_with_stdio(false);
  memset(dt, -1, sizeof(dt));
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cout << dp(0, 0) << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define INT long long int
#define MOD 1000000007
#define oo 978654321
#define OO 9999999987654321ll
using namespace std;
int n, s[101], dt[101][10010];
int dp(int x, int y) {
  if (x == n)
    return y % 10 ? y : 0;
  if (~dt[x][y])
    return dt[x][y];
  int &ret = dt[x][y];
  return dt[x][y] = max(dp(x + 1, y), dp(x + 1, y + s[x]));
}
int main() {
  ios::sync_with_stdio(false);
  memset(dt, -1, sizeof(dt));
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cout << dp(0, 0) << endl;
}
Problem p03701 (C++)

Buggy submission (status: Runtime Error):
/*
    coder:xxjAc
    time:2017/10/28
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n;
  int sum = 0;
  int x, mn = 107;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", x);
    sum += x;
    if (x % 10 != 0) {
      mn = min(mn, x);
    }
  }
  if (sum % 10 != 0) {
    printf("%d\n", sum);
    return 0;
  }
  if (mn == 107) {
    printf("0\n");
  } else {
    printf("%d\n", sum - mn);
  }
  return 0;
}

Fixed submission:
/*
    coder:xxjAc
    time:2017/10/28
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n;
  int sum = 0;
  int x, mn = 107;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    sum += x;
    if (x % 10 != 0) {
      mn = min(mn, x);
    }
  }
  if (sum % 10 != 0) {
    printf("%d\n", sum);
    return 0;
  }
  if (mn == 107) {
    printf("0\n");
  } else {
    printf("%d\n", sum - mn);
  }
  return 0;
}
Problem p03701 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sort(v.begin(), v.end());
  if (sum % 10 != 0) {
    cout << sum << endl;
    return 0;
  } else {
    for (int i = 0; i < n; i++) {
      if ((sum - v[i]) % 10 != 0) {
        cout << sum - v[i] << "\n" << endl;
        return 0;
      }
    }
  }
  cout << "0" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sort(v.begin(), v.end());
  if (sum % 10 != 0) {
    cout << sum << endl;
    return 0;
  } else {
    for (int i = 0; i < n; i++) {
      if ((sum - v[i]) % 10 != 0) {
        cout << sum - v[i] << "\n" << endl;
        return 0;
      }
    }
  }
  cout << "0" << endl;
}
Problem p03701 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

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

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

int main(void) {
  int n;
  cin >> n;
  VI s(n);
  VI t, u;
  REP(i, 0, n) {
    cin >> s[i];
    if (s[i] % 10 == 0) {
      t.push_back(s[i]);
    } else {
      u.push_back(s[i]);
    }
  }
  if (u.size() == 0) {
    cout << 0 << endl;
  }
  int tot = 0;
  sort(u.begin(), u.end());
  REP(i, 0, u.size()) { tot += u[i]; }
  REP(i, 0, t.size()) { tot += t[i]; }
  if (tot % 10 == 0) {
    tot -= u[0];
  }
  cout << tot << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

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

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

int main(void) {
  int n;
  cin >> n;
  VI s(n);
  VI t, u;
  REP(i, 0, n) {
    cin >> s[i];
    if (s[i] % 10 == 0) {
      t.push_back(s[i]);
    } else {
      u.push_back(s[i]);
    }
  }
  if (u.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int tot = 0;
  sort(u.begin(), u.end());
  REP(i, 0, u.size()) { tot += u[i]; }
  REP(i, 0, t.size()) { tot += t[i]; }
  if (tot % 10 == 0) {
    tot -= u[0];
  }
  cout << tot << endl;
}