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

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

int main() {
  int R, G, B, N, Sum = 0;
  cin >> R >> G >> B >> N;
  rep(i, N / R + 1) {
    if (R * i > N)
      break;
    rep(j, N / G + 1) {
      if (R * i + G * j > N)
        break;
      rep(k, N / B + 1) {
        if (R * i + G * j + B * k > N)
          break;
        else if (R * i + G * j + B * k == N)
          Sum++;
      }
    }
  }
  cout << Sum << endl;
}

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

int main() {
  int R, G, B, N, Sum = 0;
  cin >> R >> G >> B >> N;
  rep(i, N / R + 1) {
    if (R * i > N)
      break;
    rep(j, N / G + 1) {
      if (R * i + G * j > N)
        break;
      if ((N - (R * i + G * j)) % B == 0)
        Sum++;
    }
  }
  cout << Sum << endl;
}
Problem p03048 (C++)

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

using namespace std;

int main() {
  int R, G, B, n;
  int ans = 0;

  cin >> R >> G >> B >> n;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (R * i + G * j + B * k == n)
          ans++;
      }
    }
  }

  cout << ans << endl;
  return (0);
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int R, G, B, n;
  int ans = 0;

  cin >> R >> G >> B >> n;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if ((n - (R * i + G * j)) % B == 0 && R * i + G * j <= n) {
        ans++;
      }
    }
  }

  cout << ans << endl;
  return (0);
}
Problem p03048 (C++)

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

int main() {
  int r, g, b, n, sum = 0;
  scanf("%d %d %d %d", &r, &g, &b, &n);
  for (int i = 0; i <= n; i += r) {
    for (int j = 0; j <= n - i; j += g) {
      for (int z = 0; z <= n - i - j; z += b) {
        if (z + j + i > 0 && z + j + i == n)
          sum++;
      }
    }
  }
  printf("%d", sum);
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int r, g, b, n, sum = 0;
  scanf("%d %d %d %d", &r, &g, &b, &n);
  for (int i = 0; i <= n / r; i++) {
    for (int j = 0; j <= n / g; j++) {
      if ((n - i * r - g * j) % b == 0 && n - i * r - g * j >= 0)
        sum++;
    }
  }
  printf("%d", sum);
  return 0;
}
Problem p03048 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  int R, G, B, N;
  cin >> R >> G >> B >> N;

  int ans = 0;
  for (int ii = 0; ii <= N / R; ++ii) {
    int val1 = ii * R;
    for (int jj = 0; jj <= N / G; ++jj) {
      int val2 = jj * G;
      if (val1 + val2 > N) {
        break;
      }
      for (int kk = 0; kk <= N / B; ++kk) {
        int val3 = kk * B;
        int sum = val1 + val2 + val3;
        if (sum > N) {
          break;
        }
        if (sum == N) {
          ans++;
        }
      }
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  int R, G, B, N;
  cin >> R >> G >> B >> N;

  int ans = 0;
  for (int ii = 0; ii <= N / R; ++ii) {
    int val1 = ii * R;
    for (int jj = 0; jj <= N / G; ++jj) {
      int val2 = jj * G;
      if (val1 + val2 > N) {
        break;
      }

      int rem = N - val1 - val2;
      if (rem % B == 0) {
        ans++;
      }
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p03048 (C++)

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

int main(void) {
  int i, j, k, tmp, ans;
  int R, G, B, N;
  cin >> R >> G >> B >> N;

  for (i = 0, ans = 0; i * R <= N; i++) {
    for (j = 0; (i * R + j * G) <= N; j++) {
      for (k = 0; (i * R + j * G + k * B) <= N; k++) {
        if ((i * R + j * G + k * B) == N) {
          ans++;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main(void) {
  int i, j, k, tmp, ans;
  int R, G, B, N;
  cin >> R >> G >> B >> N;

  for (i = 0, ans = 0; i * R <= N; i++) {
    for (j = 0; (i * R + j * G) <= N; j++) {
      if ((N - (i * R + j * G)) % B == 0)
        ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03049 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  string ss = "";
  vector<bool> sumi(n, false);
  vector<int> topB;
  vector<int> bottomA;
  vector<int> doublle;
  int i, j;
  int tmp = 0;
  int first = -1;
  for (i = 0; i < n; i++) {
    tmp = 0;
    cin >> s[i];
    if (s[i][0] == 'B')
      tmp += 1;
    if (s[i][s[i].length() - 1] == 'A')
      tmp += 2;
    if (tmp == 1 || tmp == 3)
      topB.push_back(i);
    if (tmp == 2 || tmp == 3)
      bottomA.push_back(i);
    if (tmp == 3)
      doublle.push_back(i);
    if (tmp == 2)
      first = i;
  }
  if (first != -1) {
    ss += s[first];
    sumi[first] = true;
  }

  for (i = 0; i < doublle.size(); i++) {
    if (!sumi[doublle[i]]) {
      sumi[doublle[i]] = true;
      ss += s[doublle[i]];
    }
  }
  i = 0;
  j = 0;
  if (ss == "") {
    ss = s[bottomA[0]];
    sumi[bottomA[0]] = true;
    j++;
  }
  while (i < topB.size() && j < bottomA.size()) {
    while (sumi[topB[i]] && i < topB.size() - 1)
      i++;
    while (sumi[bottomA[j]] && j < bottomA.size() - 1)
      j++;

    if (!sumi[topB[i]]) {
      ss += s[topB[i]];
      sumi[topB[i]] = true;
    }
    if (!sumi[bottomA[j]]) {
      sumi[bottomA[j]] = true;
      ss += s[bottomA[j]];
    }
    i++;
    j++;
  }
  for (i = 0; i < n; i++) {
    if (!sumi[i])
      ss += s[i];
  }
  int ans = 0;
  for (i = 0; i < ss.length() - 1; i++) {
    if (ss[i] == 'A' && ss[i + 1] == 'B')
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  string ss = "";
  vector<bool> sumi(n, false);
  vector<int> topB;
  vector<int> bottomA;
  vector<int> doublle;
  int i, j;
  int tmp = 0;
  int first = -1;
  for (i = 0; i < n; i++) {
    tmp = 0;
    cin >> s[i];
    if (s[i][0] == 'B')
      tmp += 1;
    if (s[i][s[i].length() - 1] == 'A')
      tmp += 2;
    if (tmp == 1 || tmp == 3)
      topB.push_back(i);
    if (tmp == 2 || tmp == 3)
      bottomA.push_back(i);
    if (tmp == 3)
      doublle.push_back(i);
    if (tmp == 2)
      first = i;
  }
  if (first != -1) {
    ss += s[first];
    sumi[first] = true;
  }

  for (i = 0; i < doublle.size(); i++) {
    if (!sumi[doublle[i]]) {
      sumi[doublle[i]] = true;
      ss += s[doublle[i]];
    }
  }
  i = 0;
  j = 0;
  if (ss == "" && !bottomA.empty()) {
    ss = s[bottomA[0]];
    sumi[bottomA[0]] = true;
    j++;
  }
  while (i < topB.size() && j < bottomA.size()) {
    while (sumi[topB[i]] && i < topB.size() - 1)
      i++;
    while (sumi[bottomA[j]] && j < bottomA.size() - 1)
      j++;

    if (!sumi[topB[i]]) {
      ss += s[topB[i]];
      sumi[topB[i]] = true;
    }
    if (!sumi[bottomA[j]]) {
      sumi[bottomA[j]] = true;
      ss += s[bottomA[j]];
    }
    i++;
    j++;
  }
  for (i = 0; i < n; i++) {
    if (!sumi[i])
      ss += s[i];
  }
  int ans = 0;
  for (i = 0; i < ss.length() - 1; i++) {
    if (ss[i] == 'A' && ss[i + 1] == 'B')
      ans++;
  }
  cout << ans << endl;
}
Problem p03049 (C++)

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

int main() {
  int N;
  vector<string> S(10010);

  cin >> N;
  int ans = 0;

  int A = 0, B = 0, BA = 0;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    int l = S[i].length();
    for (int j = 0; j < N - 1; j++) {
      if (S[i][j] == 'A' && S[i][j + 1] == 'B')
        ans++;
    }

    if (S[i][0] == 'B' && S[i][l - 1] == 'A') {
      BA++;
    } else if (S[i][0] == 'B') {
      B++;
    } else if (S[i][l - 1] == 'A') {
      A++;
    }
  }

  if (BA >= 1) {
    ans += BA - 1;
    if (A > 0) {
      ans++;
      A--;
    }
    if (B > 0) {
      ans++;
      B--;
    }
  }
  ans += min(A, B);
  cout << ans << endl;
}

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

int main() {
  int N;
  vector<string> S(10010);

  cin >> N;
  int ans = 0;

  int A = 0, B = 0, BA = 0;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    int l = S[i].length();
    for (int j = 0; j < l - 1; j++) {
      if (S[i][j] == 'A' && S[i][j + 1] == 'B')
        ans++;
    }

    if (S[i][0] == 'B' && S[i][l - 1] == 'A') {
      BA++;
    } else if (S[i][0] == 'B') {
      B++;
    } else if (S[i][l - 1] == 'A') {
      A++;
    }
  }

  if (BA >= 1) {
    ans += BA - 1;
    if (A > 0) {
      ans++;
      A--;
    }
    if (B > 0) {
      ans++;
      B--;
    }
  }
  ans += min(A, B);
  cout << ans << endl;
}
Problem p03049 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int main() {
  ll n;
  cin >> n;
  ll ans = 0, x = 0, y = 0, z = 0;
  rep(i, n) {
    string s;
    cin >> s;
    rep(i, n - 1) {
      if (s[i] == 'A' && s[i + 1] == 'B')
        ans++;
    }

    if (s[0] == 'B' && s.back() == 'A')
      x++;
    else if (s[0] == 'B')
      y++;
    else if (s.back() == 'A')
      z++;
  }

  if (y == 0 && z == 0) {
    ans += max(x - 1, 0LL);
  } else if (y == z) {
    ans += x + y;
  } else if (y > z) {
    ans += x + min(y, z);
  } else if (y < z)
    ans += x + min(y, z);

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int main() {
  ll n;
  cin >> n;
  ll ans = 0, x = 0, y = 0, z = 0;
  rep(i, n) {
    string s;
    cin >> s;
    rep(i, s.size()) {
      if (s[i] == 'A' && s[i + 1] == 'B')
        ans++;
    }

    if (s[0] == 'B' && s.back() == 'A')
      x++;
    else if (s[0] == 'B')
      y++;
    else if (s.back() == 'A')
      z++;
  }

  if (y == 0 && z == 0) {
    ans += max(x - 1, 0LL);
  } else if (y == z) {
    ans += x + y;
  } else if (y > z) {
    ans += x + min(y, z);
  } else if (y < z)
    ans += x + min(y, z);

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

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

using namespace std;

int main() {
  int n, cnt = 0;
  int a, b, ba;
  a = 0;
  b = 0;
  ba = 0;
  string str;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", &str);
    int L = str.size();
    for (int j = 0; j + 1 < L; j++) {
      if (str[j] == 'A' && str[j + 1] == 'B') {
        cnt++;
      }
    }
    if (str[0] == 'B')
      b++;
    if (str[L - 1] == 'A')
      a++;
    if (str[0] == 'B' && str[L - 1] == 'A')
      ba++;
  }
  cnt += min(a, b);
  if (a == ba && b == ba && b > 0)
    cnt--;
  printf("%d\n", cnt);
  return 0;
}

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

using namespace std;

int main() {
  int n, cnt = 0;
  int a, b, ba;
  a = 0;
  b = 0;
  ba = 0;
  string str;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> str;
    int L = str.size();
    for (int j = 0; j + 1 < L; j++) {
      if (str[j] == 'A' && str[j + 1] == 'B') {
        cnt++;
      }
    }
    if (str[0] == 'B')
      b++;
    if (str[L - 1] == 'A')
      a++;
    if (str[0] == 'B' && str[L - 1] == 'A')
      ba++;
  }
  cnt += min(a, b);
  if (a == ba && b == ba && b > 0)
    cnt--;
  printf("%d\n", cnt);
  return 0;
}
Problem p03049 (C++)

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

int findAndCount(const string s, const string target) {
  int cnt = 0;
  size_t pos = 0;
  while ((pos = s.find(target, pos)) != -1) {
    ++cnt;
    ++pos;
  }
  return cnt;
}

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

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

  int nAB = 0;
  for (i = 0; i < n; ++i) {
    nAB += findAndCount(s[i], "AB");
  }

  int tailAOnlyCount = 0, headBOnlyCount = 0, tailAHeadBCount = 0;
  bool isTailA, isHeadB;
  for (i = 0; i < n; ++i) {
    isTailA = (s[i].back() == 'A');
    isHeadB = (s[i].front() == 'B');
    if (isTailA && isHeadB) {
      ++tailAHeadBCount;
    } else if (isTailA) {
      ++tailAOnlyCount;
    } else if (isHeadB) {
      ++headBOnlyCount;
    }
  }

  int nPairs =
      ((tailAOnlyCount > headBOnlyCount) ? headBOnlyCount : tailAOnlyCount);
  nAB += nPairs;
  if (tailAHeadBCount >= 1) {
    nAB += (tailAHeadBCount - 1);
    if ((tailAOnlyCount != 0) || (headBOnlyCount != 0)) {
      nAB += 1;
    }
  }

  cout << nAB << endl;

  return 0;
}

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

int findAndCount(const string s, const string target) {
  int cnt = 0;
  size_t pos = 0;
  while ((pos = s.find(target, pos)) != -1) {
    ++cnt;
    ++pos;
  }
  return cnt;
}

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

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

  int nAB = 0;
  for (i = 0; i < n; ++i) {
    nAB += findAndCount(s[i], "AB");
  }

  int tailAOnlyCount = 0, headBOnlyCount = 0, tailAHeadBCount = 0;
  bool isTailA, isHeadB;
  for (i = 0; i < n; ++i) {
    isTailA = (s[i].back() == 'A');
    isHeadB = (s[i].front() == 'B');
    if (isTailA && isHeadB) {
      ++tailAHeadBCount;
    } else if (isTailA) {
      ++tailAOnlyCount;
    } else if (isHeadB) {
      ++headBOnlyCount;
    }
  }

  int nPairs =
      ((tailAOnlyCount > headBOnlyCount) ? headBOnlyCount : tailAOnlyCount);
  nAB += nPairs;
  if (tailAHeadBCount >= 1) {
    nAB += (tailAHeadBCount - 1);
    if ((tailAOnlyCount != 0) || (headBOnlyCount != 0)) {
      nAB += 1;
    }
  }

  cout << nAB << endl;

  return 0;
}
Problem p03049 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
int main(int argc, char *argv[]) {
  int n;
  std::cin >> n;

  std::vector<std::string> str(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> str[i];
  }

  int ret = 0;

  int bx = 0;
  int xa = 0;
  int ba = 0;

  for (int i = 0; i < n; ++i) {
    if (str[i].front() == 'B' && str[i].back() != 'A') {
      bx += 1;
    }
    if (str[i].front() != 'B' && str[i].back() == 'A') {
      xa += 1;
    }
    if (str[i].front() == 'B' && str[i].back() == 'A') {
      ba += 1;
    }
    for (int j = 0; j < str[i].size() - 1; ++j) {
      if (str[i][j] == 'A' && str[i][j + 1] == 'B') {
        ret += 1;
      }
    }
  }
  // std::cout<<ret;

  if (ba >= 2) {
    ret += (ba - 1);
  }

  // std::cout<<ret;
  if (ba) {
    if (bx) {
      ret += 1;
      bx -= 1;
    }
    if (xa) {
      ret += 1;
      xa -= 1;
    }
  }

  // std::cout<<ret;
  if (xa >= bx) {
    ret += bx;
  } else {
    ret += xa;
  }

  // std::cout<<ret;

  return ret;
}

Fixed submission:
#include <iostream>
#include <vector>
int main(int argc, char *argv[]) {
  int n;
  std::cin >> n;

  std::vector<std::string> str(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> str[i];
  }

  int ret = 0;

  int bx = 0;
  int xa = 0;
  int ba = 0;

  for (int i = 0; i < n; ++i) {
    if (str[i].front() == 'B' && str[i].back() != 'A') {
      bx += 1;
    }
    if (str[i].front() != 'B' && str[i].back() == 'A') {
      xa += 1;
    }
    if (str[i].front() == 'B' && str[i].back() == 'A') {
      ba += 1;
    }
    for (int j = 0; j < str[i].size() - 1; ++j) {
      if (str[i][j] == 'A' && str[i][j + 1] == 'B') {
        ret += 1;
      }
    }
  }
  // std::cout<<ret;

  if (ba >= 2) {
    ret += (ba - 1);
  }

  // std::cout<<ret;
  if (ba) {
    if (bx) {
      ret += 1;
      bx -= 1;
    }
    if (xa) {
      ret += 1;
      xa -= 1;
    }
  }

  // std::cout<<ret;
  if (xa >= bx) {
    ret += bx;
  } else {
    ret += xa;
  }

  std::cout << ret;
  return 0;
}
Problem p03049 (C++)

Buggy submission (status: Runtime Error):
/*
[diverta2019] C - AB Substrings
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const int MAX_N = 1000;

int N;
string s[MAX_N];

ll solve() {
  int c1 = 0, c2 = 0, c3 = 0;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)s[i].length() - 1; j++) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B') {
        ans++;
      }
    }
    if (s[i][0] == 'B') {
      if (s[i].back() == 'A') {
        c1++;
      } else {
        c2++;
      }
    } else if (s[i].back() == 'A') {
      c3++;
    }
  }

  if (c1 == 0) {
    ans += min(c2, c3);
  } else if (c2 + c3 > 0) {
    ans += c1 + min(c2, c3);
  } else {
    ans += c1 - 1;
  }

  return ans;
}

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

  cout << solve() << endl;

  return 0;
}


Fixed submission:
/*
[diverta2019] C - AB Substrings
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const int MAX_N = 1e4;

int N;
string s[MAX_N];

ll solve() {
  int c1 = 0, c2 = 0, c3 = 0;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)s[i].length() - 1; j++) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B') {
        ans++;
      }
    }
    if (s[i][0] == 'B') {
      if (s[i].back() == 'A') {
        c1++;
      } else {
        c2++;
      }
    } else if (s[i].back() == 'A') {
      c3++;
    }
  }

  if (c1 == 0) {
    ans += min(c2, c3);
  } else if (c2 + c3 > 0) {
    ans += c1 + min(c2, c3);
  } else {
    ans += c1 - 1;
  }

  return ans;
}

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

  cout << solve() << endl;

  return 0;
}
Problem p03049 (C++)

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

int main() {
  int n, b = 0, a = 0, ba = 0;
  cin >> n;
  int ans = 0;
  rep(i, n) {
    string s;
    cin >> s;
    for (int i = 0; i < n - 1; ++i) {
      if (s[i] == 'A' && s[i + 1] == 'B')
        ans++;
    }
    if (s[0] == 'B' && s[s.size() - 1] == 'A')
      ba++;
    else if (s[s.size() - 1] == 'A')
      a++;
    else if (s[0] == 'B')
      b++;
  }
  ans += min(a + ba, b + ba);
  if (a == 0 && b == 0 && ba > 0)
    ans--;
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, b = 0, a = 0, ba = 0;
  cin >> n;
  int ans = 0;
  rep(i, n) {
    string s;
    cin >> s;
    for (int i = 0; i < s.size() - 1; ++i) {
      if (s[i] == 'A' && s[i + 1] == 'B')
        ans++;
    }
    if (s[0] == 'B' && s[s.size() - 1] == 'A')
      ba++;
    else if (s[s.size() - 1] == 'A')
      a++;
    else if (s[0] == 'B')
      b++;
  }
  ans += min(a + ba, b + ba);
  if (a == 0 && b == 0 && ba > 0)
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p03049 (C++)

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

vector<string> a, b, c, d;
string ans;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    int x = s[0], y = s[s.size() - 1];
    if (x != 'B' && y != 'A')
      a.push_back(s);
    if (x != 'B' && y == 'A')
      b.push_back(s);
    if (x == 'B' && y != 'A')
      c.push_back(s);
    if (x == 'B' && y == 'A')
      d.push_back(s);
  }
  for (auto v : a)
    ans += v;
  if (b.size()) {
    ans += b[b.size() - 1];
    b.pop_back();
  }
  for (auto v : d)
    ans += v;
  if (c.size()) {
    ans += c[c.size() - 1];
    c.pop_back();
  }
  while (b.size() || c.size()) {
    ans += b[b.size() - 1];
    b.pop_back();
    ans += c[c.size() - 1];
    c.pop_back();
  }
  for (auto v : b)
    ans += v;
  for (auto v : c)
    ans += v;
  int tot = 0;
  for (int i = 0; i < ans.size() - 1; ++i)
    if (ans[i] == 'A' && ans[i + 1] == 'B')
      ++tot;
  cout << tot << endl;
  return 0;
}

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

vector<string> a, b, c, d;
string ans;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    int x = s[0], y = s[s.size() - 1];
    if (x != 'B' && y != 'A')
      a.push_back(s);
    if (x != 'B' && y == 'A')
      b.push_back(s);
    if (x == 'B' && y != 'A')
      c.push_back(s);
    if (x == 'B' && y == 'A')
      d.push_back(s);
  }
  for (auto v : a)
    ans += v;
  if (b.size()) {
    ans += b[b.size() - 1];
    b.pop_back();
  }
  for (auto v : d)
    ans += v;
  if (c.size()) {
    ans += c[c.size() - 1];
    c.pop_back();
  }
  while (b.size() && c.size()) {
    ans += b[b.size() - 1];
    b.pop_back();
    ans += c[c.size() - 1];
    c.pop_back();
  }
  for (auto v : b)
    ans += v;
  for (auto v : c)
    ans += v;
  int tot = 0;
  for (int i = 0; i < ans.size() - 1; ++i)
    if (ans[i] == 'A' && ans[i + 1] == 'B')
      ++tot;
  cout << tot << endl;
  return 0;
}
Problem p03049 (C++)

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

using namespace std;

typedef long long ll;

constexpr int N{1000}, M{1000000000};
constexpr int di[]{1, -1, 0, 0}, dj[]{0, 0, 1, -1};

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

  int n;
  array<string, N> s;
  // string s, t;
  // bool can;

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

  int c_btw = 0;
  int fb = 0, ba = 0, fbba = 0;

  for (int i = 0; i < n; i++) {
    int c_now = 0;
    int pos = s[i].find("AB");
    while (pos != string::npos) {
      pos = s[i].find("AB", pos + 2);
      c_now++;
    }
    c_btw += c_now;
    bool front_is_b = s[i].front() == 'B';
    bool back_is_a = s[i].back() == 'A';
    fb += front_is_b;
    ba += back_is_a;
    fbba += front_is_b & back_is_a;
  }

  int c_connect = min(fb, ba);
  if (fbba == fb && fbba == ba)
    c_connect--;
  if (c_connect < 0)
    c_connect = 0;

  cout << c_btw + c_connect << endl;
  // if (can)
  //     cout << "Yes" << endl;
  // else
  //     cout << "No" << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;

constexpr int N{50000}, M{1000000000};
constexpr int di[]{1, -1, 0, 0}, dj[]{0, 0, 1, -1};

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

  int n;
  array<string, N> s;
  // string s, t;
  // bool can;

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

  int c_btw = 0;
  int fb = 0, ba = 0, fbba = 0;

  for (int i = 0; i < n; i++) {
    int c_now = 0;
    int pos = s[i].find("AB");
    while (pos != string::npos) {
      pos = s[i].find("AB", pos + 2);
      c_now++;
    }
    c_btw += c_now;
    bool front_is_b = s[i].front() == 'B';
    bool back_is_a = s[i].back() == 'A';
    fb += front_is_b;
    ba += back_is_a;
    fbba += front_is_b & back_is_a;
  }

  int c_connect = min(fb, ba);
  if (fbba == fb && fbba == ba)
    c_connect--;
  if (c_connect < 0)
    c_connect = 0;

  cout << c_btw + c_connect << endl;
  // if (can)
  //     cout << "Yes" << endl;
  // else
  //     cout << "No" << endl;

  return 0;
}
Problem p03049 (C++)

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

template <class T, class U>
bool contain(const std::basic_string<T> &s, const U &v) {
  return s.find(v) != std::basic_string<T>::npos;
}
// int ans = contain(s, "AB");

int main() {

  int n;
  cin >> n;
  int cnt = 0;
  string s[n];
  queue<string> aq;
  queue<string> bq;
  string abq;
  string ab;
  string str;
  int k = 0, b = 0, z = 0;
  rep(i, n) {
    cin >> s[i];
    if (s[i].front() == 'B' && s[i].back() == 'A') {
      abq += s[i];
      k++;
    } else if (s[i].front() == 'B') {
      bq.push(s[i]);
      b++;
    } else if (s[i].back() == 'A') {
      aq.push(s[i]);
      z++;
    } else
      str += s[i];
  }
  if (b == z) {
    abq = aq.front() += abq;
    abq += bq.front();
    aq.pop();
    bq.pop();
  }
  while (!aq.empty() && !bq.empty()) {
    ab += (aq.front() + bq.front());
    aq.pop();
    bq.pop();
  }
  if (!aq.empty()) {
    while (!aq.empty()) {
      abq = aq.front() += abq;
      aq.pop();
    }
  }
  if (!bq.empty()) {
    while (!bq.empty()) {
      abq += bq.front();
      bq.pop();
    }
  }
  string ans = abq + ab + str;
  rep(i, ans.size()) {
    if (ans.substr(i, 2) == "AB")
      cnt++;
  }
  // cout << k << endl;
  // cout << b << endl;
  // cout << z << endl;
  cout << cnt << endl;
  return (0);
}


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

template <class T, class U>
bool contain(const std::basic_string<T> &s, const U &v) {
  return s.find(v) != std::basic_string<T>::npos;
}
// int ans = contain(s, "AB");

int main() {

  int n;
  cin >> n;
  int cnt = 0;
  string s[n];
  queue<string> aq;
  queue<string> bq;
  string abq;
  string ab;
  string str;
  int k = 0, b = 0, z = 0;
  rep(i, n) {
    cin >> s[i];
    if (s[i].front() == 'B' && s[i].back() == 'A') {
      abq += s[i];
      k++;
    } else if (s[i].front() == 'B') {
      bq.push(s[i]);
      b++;
    } else if (s[i].back() == 'A') {
      aq.push(s[i]);
      z++;
    } else
      str += s[i];
  }
  if (b == z) {
    if (!bq.empty() && !aq.empty()) {
      abq = aq.front() += abq;
      abq += bq.front();
      aq.pop();
      bq.pop();
    }
  }
  while (!aq.empty() && !bq.empty()) {
    ab += (aq.front() + bq.front());
    aq.pop();
    bq.pop();
  }
  if (!aq.empty()) {
    while (!aq.empty()) {
      abq = aq.front() += abq;
      aq.pop();
    }
  }
  if (!bq.empty()) {
    while (!bq.empty()) {
      abq += bq.front();
      bq.pop();
    }
  }
  string ans = abq + ab + str;
  rep(i, ans.size()) {
    if (ans.substr(i, 2) == "AB")
      cnt++;
  }
  // cout << k << endl;
  // cout << b << endl;
  // cout << z << endl;
  cout << cnt << endl;
  return (0);
}
Problem p03049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

int n, ans, l, r, p;
string s[1010];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].length() - 1; j++) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B') {
        ans++;
      }
    }
    if (s[i][0] == 'B' && s[i][s[i].length() - 1] == 'A') {
      p++;
    } else if (s[i][0] == 'B') {
      l++;
    } else if (s[i][s[i].length() - 1] == 'A') {
      r++;
    }
  }
  ans += max(p - 1, 0);
  if (p == 0) {
    ans += min(r, l);
  } else {
    if (r > 0) {
      r--;
      ans++;
    }
    if (l > 0) {
      l--;
      ans++;
    }
    ans += max(min(r, l), 0);
  }
  cout << ans << endl;
  cin >> n;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

int n, ans, l, r, p;
string s[10010];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].length() - 1; j++) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B') {
        ans++;
      }
    }
    if (s[i][0] == 'B' && s[i][s[i].length() - 1] == 'A') {
      p++;
    } else if (s[i][0] == 'B') {
      l++;
    } else if (s[i][s[i].length() - 1] == 'A') {
      r++;
    }
  }
  ans += max(p - 1, 0);
  if (p == 0) {
    ans += min(r, l);
  } else {
    if (r > 0) {
      r--;
      ans++;
    }
    if (l > 0) {
      l--;
      ans++;
    }
    ans += max(min(r, l), 0);
  }
  cout << ans << endl;
  cin >> n;
}
Problem p03049 (C++)

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

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

  int n;
  cin >> n;
  int a = 0; // ooooA
  int b = 0; // Boooo
  int c = 0; // BoooA

  int ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int ss = s.size() - 1;
    if (s[0] == 'B' and s[ss] == 'A')
      c++;
    else if (s[0] == 'B')
      b++;
    else if (s[ss] == 'A')
      a++;
    for (int j = 0; j < n - 1; j++) {
      if (s[j] == 'A' and s[j + 1] == 'B') {
        ans++;
      }
    }
  }

  if (c == 0)
    ans += min(a, b);
  else if (a == 0 and b == 0 and c > 0)
    ans += c - 1;
  else
    ans += c + min(a, b);

  cout << ans << endl;

  return 0;
}

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

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

  int n;
  cin >> n;
  int a = 0; // ooooA
  int b = 0; // Boooo
  int c = 0; // BoooA

  int ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int ss = s.size() - 1;
    if (s[0] == 'B' and s[ss] == 'A')
      c++;
    else if (s[0] == 'B')
      b++;
    else if (s[ss] == 'A')
      a++;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'A' and s[j + 1] == 'B') {
        ans++;
      }
    }
  }

  if (c == 0)
    ans += min(a, b);
  else if (a == 0 and b == 0 and c > 0)
    ans += c - 1;
  else
    ans += c + min(a, b);

  cout << ans << endl;

  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int64_t N;
  cin >> N;
  int64_t sum = 0;
  for (int64_t i = 1; i * i <= N; i++) {
    int64_t r = N / i;
    if (N % (r - 1) == N / (r - 1)) {
      sum += r - 1;
    }
    if (i - 1 > 0 && N % (i - 1) == N / (i - 1)) {
      sum += i - 1;
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int64_t N;
  cin >> N;
  int64_t sum = 0;
  for (int64_t i = 1; i * i <= N; i++) {
    int64_t r = N / i;
    if (r - 1 > 0 && N % (r - 1) == N / (r - 1)) {
      sum += r - 1;
    }
    if (i - 1 > 0 && N % (i - 1) == N / (i - 1)) {
      sum += i - 1;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
  // freopen("input.txt","r",stdin);
  scanf("%lld", &n);
  long long to = sqrt(n);
  while ((to + 1) * (to + 1) <= n)
    to++;
  for (int i = 1; i <= to; i++)
    if ((n - i) % i == 0 && n % ((n - i) / i) == i)
      ans += (n - i) / i;
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
  // freopen("input.txt","r",stdin);
  scanf("%lld", &n);
  long long to = sqrt(n);
  while ((to + 1) * (to + 1) <= n)
    to++;
  for (int i = 1; i <= to; i++)
    if ((n - i) / i > 0 && (n - i) % i == 0 && n % ((n - i) / i) == i)
      ans += (n - i) / i;
  printf("%lld\n", ans);
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, r, b;
ll guessm(ll m) {
  if (n % m != n / m)
    return 0;
  return m;
}
int main() {
  cin >> n;
  r = 0;
  b = (ll)sqrt(n);
  for (ll i = 2; i <= b; i++) {
    if (n % i == 0) {
      r += guessm(i - 1);
      r += guessm(n / i - 1);
    }
  }
  r += guessm(n - 1);
  if (b * b == n)
    r -= guessm(b - 1);
  cout << r << endl;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, r, b;
ll guessm(ll m) {
  if (m == 0)
    return 0;
  if (n % m != n / m)
    return 0;
  return m;
}
int main() {
  cin >> n;
  r = 0;
  b = (ll)sqrt(n);
  for (ll i = 2; i <= b; i++) {
    if (n % i == 0) {
      r += guessm(i - 1);
      r += guessm(n / i - 1);
    }
  }
  r += guessm(n - 1);
  if (b * b == n)
    r -= guessm(b - 1);
  cout << r << endl;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, h, i, j, k, l, m, n, x, y;
  ll ans = 0;
  string s;
  cin >> n;
  for (i = 1; i <= (ll)sqrt(n); i++) {
    if (n % i != 0)
      continue;
    m = n / i - 1;
    if (n / m == n % m)
      ans += m;
    // if (i<n/3) break;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, h, i, j, k, l, m, n, x, y;
  ll ans = 0;
  string s;
  cin >> n;
  for (i = 1; i <= (ll)sqrt(n); i++) {
    if (n % i != 0)
      continue;
    m = n / i - 1;
    if (m == 0)
      continue;
    if (n / m == n % m)
      ans += m;
    // if (i<n/3) break;
  }

  cout << ans << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define MEM(x, y) memset(x, y, sizeof(x))
#define MOD(x) ((x) % mod)
#define mod 1000000007
#define pb push_back
#define STREAM_FAST ios::sync_with_stdio(false)
using namespace std;

int main() {
  LL n;
  scanf("%lld", &n);
  LL ans = 0;
  for (LL x = 1; x <= sqrt(n + 0.5); x++) {
    LL m = n / x - 1;
    if ((n / m) == (n % m))
      ans += m;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define MEM(x, y) memset(x, y, sizeof(x))
#define MOD(x) ((x) % mod)
#define mod 1000000007
#define pb push_back
#define STREAM_FAST ios::sync_with_stdio(false)
using namespace std;

int main() {
  LL n;
  scanf("%lld", &n);
  LL ans = 0;
  for (LL x = 1; x <= sqrt(n + 0.5); x++) {
    LL m = n / x - 1;
    if (m == 0)
      continue;
    if ((n / m) == (n % m))
      ans += m;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;

int main() {
  long long int ans = 0;
  long long int n;
  cin >> n;
  for (long long int i = 0; n >= (i * i); i++) {
    if (n % (i + 1) == 0) {
      long long int t = (n / (i + 1)) - 1;
      if (n / t == (n % t))
        ans = ans + t;
      if (i != 0 && n / i == (n % i))
        ans = ans + i;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;

int main() {
  long long int ans = 0;
  long long int n;
  cin >> n;
  if (n == 1 || n == 2) {
    cout << 0 << endl;
    return 0;
  }
  for (long long int i = 0; n >= (i * i); i++) {
    if (n % (i + 1) == 0) {
      long long int t = (n / (i + 1)) - 1;
      if (n / t == (n % t))
        ans = ans + t;
      if (i != 0 && n / i == (n % i))
        ans = ans + i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <limits>
using namespace std;
int main(void) {

  long N;
  cin >> N;

  double n = sqrt(N);

  // long nup = n;
  long ans = 0;
  long k = N;
  long i = 1;

  while (k > n && i < N) {
    if ((N - i) % i == 0) {
      k = (N - i) / i;
      if (k > n) {
        ans += (N - i) / i;
        // cout << "ans = " << ans << endl;
      }
    }
    i++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <limits>
using namespace std;
int main(void) {

  long N;
  cin >> N;

  double n = sqrt(N);

  // long nup = n;
  long ans = 0;
  long k = N;
  long i = 1;

  while (k > n && i < N) {
    k = (N - i) / i;
    if ((N - i) % i == 0 && k > n) {
      ans += k;
    }
    i++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << _ << ", ";                                                       \
    cerr << "]" << endl;                                                       \
  }

int main(void) {
  ll n;
  cin >> n;
  ll r = sqrt(n);
  ll sum = 0;
  for (ll y = 1; y <= r; y++) {
    if (n % y != 0)
      continue;
    ll x = (n / y) - 1;
    if (n % x == y) {
      sum += x;
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << _ << ", ";                                                       \
    cerr << "]" << endl;                                                       \
  }

int main(void) {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll r = sqrt(n);
  ll sum = 0;
  for (ll y = 1; y <= r; y++) {
    if (n % y != 0)
      continue;
    ll x = (n / y) - 1;
    if (n % x == y) {
      sum += x;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p03050 (C++)

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

long long N;
long long ans = 0;

int main() {
  cin >> N;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      long long cand = (N - i) / i;
      if (N % cand == N / cand)
        ans += cand;
    }
  }
  cout << ans << endl;
  return 0;
}

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

long long N;
long long ans = 0;

int main() {
  cin >> N;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      long long cand = (N - i) / i;
      if (cand == 0)
        continue;
      if (N % cand == N / cand)
        ans += cand;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03050 (C++)

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

int main() {
  LL n;
  cin >> n;

  LL ans = 0;
  for (int i = 1; i * i * i <= n; i++) {
    if (n % i == 0) {
      LL c = n / i - 1;
      if (n / c == n % c)
        ans += c;
    }
  }

  cout << ans << endl;
}

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

int main() {
  LL n;
  cin >> n;

  LL ans = 0;
  for (LL i = 1; i * i <= n; i++) {
    if ((n - i) % i == 0 && (n - i) / i > i)
      ans += (n - i) / i;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  ll N;
  cin >> N;
  ll ans = 0;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i > 0)
      continue;
    ll x = N / i - 1;
    // cout<<x<<" ";
    if (x > N / x) {
      ans += x;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  ll N;
  cin >> N;
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i > 0)
      continue;
    ll x = N / i - 1;
    // cout<<x<<" ";
    if (x > N / x) {
      ans += x;
    }
  }
  cout << ans << endl;
}
Problem p03050 (C++)

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

int main() {
  int64_t n, sum;
  sum = 0;
  cin >> n;
  for (int64_t i = 1; i <= sqrt(n); i++) {
    if ((n % i == 0) && (n % (n / i - 1) == n / (n / i - 1))) {
      sum += n / i - 1;
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int64_t n, sum;
  sum = 0;
  cin >> n;
  for (int64_t i = 1; i <= sqrt(n); i++) {
    if ((n % i == 0) && (n / i > 1)) {
      if (n % (n / i - 1) == n / (n / i - 1)) {
        sum += n / i - 1;
      }
    }
  }
  cout << sum << endl;
}
Problem p03050 (C++)

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

typedef long long ll;

ll N;
ll ans;

int main() {
  cin >> N;

  for (ll i = 1; i * i <= N; i++)
    if ((N - i) % i == 0 && N / ((N - i) / i) == N % ((N - i) / i))
      ans += (N - i) / i;
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;

ll N;
ll ans;

int main() {
  cin >> N;

  if (N == 1)
    cout << 0 << endl;
  else {
    for (ll i = 1; i * i <= N; i++)
      if ((N - i) % i == 0 && N / ((N - i) / i) == N % ((N - i) / i))
        ans += (N - i) / i;
    cout << ans << endl;
  }
  return 0;
}
Problem p03050 (C++)

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

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

  ll n;
  cin >> n;

  ll res = 0;
  for (ll i = 1; i * i <= n; ++i) {
    if ((n - i) % i == 0 && (n / ((n - i) / i) == (n % ((n - i) / i)))) {
      res += (n - i) / i;
    }
  }

  cout << res << endl;

  return 0;
}

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

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

  ll n;
  cin >> n;

  ll res = 0;
  for (ll i = 1; i * i <= n; ++i) {
    if ((n - i) % i == 0 &&
        ((n - i) / i != 0 && (n / ((n - i) / i) == (n % ((n - i) / i))))) {
      res += (n - i) / i;
    }
  }

  cout << res << endl;

  return 0;
}
Problem p03050 (C++)

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

int main() {
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i * i <= n; i++) {
    if ((n - i) % i != 0) {
      continue;
    }
    ll x = (n - i) / i;
    if (n / x == n % x) {
      ans += x;
    }
  }
  cout << ans << endl;
}

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

int main() {
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i * i <= n; i++) {
    if ((n - i) % i != 0) {
      continue;
    }
    ll x = (n - i) / i;
    if (x == 0) {
      continue;
    }
    if (n / x == n % x) {
      ans += x;
    }
  }
  cout << ans << endl;
}