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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
int main(void) {
  short i, j, n, sum = 0, ans = 0;
  scanf("%hd", &n);
  std::vector<short> A(n);
  for (i = 0; i < n; i++) {
    scanf("\n%hd", &A[i]);
    sum += A[i];
  }
  std::vector<bool> KingCrimson(sum + 1, false);
  KingCrimson[0] = true;
  sum = 0;
  for (i = 0; i < n; i++) {
    for (j = sum; j >= 0; j++) {
      if (KingCrimson[j])
        KingCrimson[j + A[i]] = true;
    }
    sum += A[i];
  }
  for (short i = 0; i <= sum; i++)
    if (KingCrimson[i] and i % 10 != 0)
      ans = i;
  printf("%hd\n", ans);
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
int main(void) {
  short i, j, n, sum = 0, ans = 0;
  scanf("%hd", &n);
  std::vector<short> A(n);
  for (i = 0; i < n; i++) {
    scanf("\n%hd", &A[i]);
    sum += A[i];
  }
  std::vector<bool> KingCrimson(sum + 1, false);
  KingCrimson[0] = true;
  sum = 0;
  for (i = 0; i < n; i++) {
    for (j = sum; j >= 0; j--) {
      if (KingCrimson[j])
        KingCrimson[j + A[i]] = true;
    }
    sum += A[i];
  }
  for (short i = 0; i <= sum; i++)
    if (KingCrimson[i] and i % 10 != 0)
      ans = i;
  printf("%hd\n", ans);
  return 0;
}
Problem p03701 (C++)

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

int dp[100][20000];
int main() {

  int n;
  cin >> n;

  dp[0][0] = 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] |= dp[i][j];
      dp[i + 1][j + a] |= dp[i][j];
    }
  }

  for (int i = 0; i < N; i++)
    if (dp[n][i] && i % 10)
      ans = i;
  cout << ans << endl;

  return 0;
}


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

int dp[110][20000];
int main() {

  int n;
  cin >> n;

  dp[0][0] = 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] |= dp[i][j];
      dp[i + 1][j + a] |= dp[i][j];
    }
  }

  for (int i = 0; i < N; i++)
    if (dp[n][i] && i % 10)
      ans = i;
  cout << ans << endl;

  return 0;
}
Problem p03701 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const ll LLINF = 1LL << 60;
bool dp[51][10001]; // dp[i + 1][j] : i番目まで見たときjが作れるか否か
int main() {
  int N;
  cin >> N;
  int s[N];
  rep(i, N) cin >> s[i];
  rep(i, N + 1) dp[i][0] = true;
  rep(i, N) {
    rep(j, 10001) {
      dp[i + 1][j] = dp[i][j];
      if (j >= s[i]) {
        dp[i + 1][j] |= dp[i][j - s[i]];
      }
    }
  }
  rep(i, 10001) {
    if (i % 10 != 0 && dp[N][10000 - i]) {
      cout << 10000 - i << endl;
      return 0;
    }
  }
  cout << 0 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const ll LLINF = 1LL << 60;
bool dp[101][10001]; // dp[i + 1][j] : i番目まで見たときjが作れるか否か
int main() {
  int N;
  cin >> N;
  int s[N];
  rep(i, N) cin >> s[i];
  rep(i, N + 1) dp[i][0] = true;
  rep(i, N) {
    rep(j, 10001) {
      dp[i + 1][j] = dp[i][j];
      if (j >= s[i]) {
        dp[i + 1][j] |= dp[i][j - s[i]];
      }
    }
  }
  rep(i, 10001) {
    if (i % 10 != 0 && dp[N][10000 - i]) {
      cout << 10000 - i << endl;
      return 0;
    }
  }
  cout << 0 << endl;
}
Problem p03701 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define f first
#define s second

using namespace std;

const int maxn = (int)(1e5) + 2;
const int inf = (int)(1e9) + 7;

int n, s, a[111];

int main() {
  _
#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], s += a[i];

  sort(a + 1, a + 1 + n);
  if (s % 10 == 0) {
    for (int i = 1; i <= n; i++)
      if (a[i] % 10 != 0) {
        s -= a[i];
        break;
      }
  }
  if (s % 10 == 0)
    s = 0;
  cout << s;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define f first
#define s second

using namespace std;

const int maxn = (int)(1e5) + 2;
const int inf = (int)(1e9) + 7;

int n, s, a[111];

int main() {
  _

          cin >>
      n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], s += a[i];

  sort(a + 1, a + 1 + n);
  if (s % 10 == 0) {
    for (int i = 1; i <= n; i++)
      if (a[i] % 10 != 0) {
        s -= a[i];
        break;
      }
  }
  if (s % 10 == 0)
    s = 0;
  cout << s;
  return 0;
}
Problem p03701 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  priority_queue<int, vector<int>, greater<int>> pq;
  int n;
  cin >> n;
  int s = 0;
  for (int i = 0; n > i; i++) {
    int x;
    cin >> x;
    pq.push(x);
    s += x;
  }
  if (s % 10 != 0)
    cout << s << endl;
  else {
    while (!pq.empty()) {
      if (pq.top() % 10 != 0) {
        s -= pq.top();
        break;
      }
    }
    if (s % 10 == 0)
      cout << 0 << endl;
    else
      cout << s << endl;
  }
}


Fixed submission:
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  priority_queue<int, vector<int>, greater<int>> pq;
  int n;
  cin >> n;
  int s = 0;
  for (int i = 0; n > i; i++) {
    int x;
    cin >> x;
    pq.push(x);
    s += x;
  }
  if (s % 10 != 0)
    cout << s << endl;
  else {
    while (!pq.empty()) {
      if (pq.top() % 10 != 0) {
        s -= pq.top();
        break;
      }
      pq.pop();
    }
    if (s % 10 == 0)
      cout << 0 << endl;
    else
      cout << s << endl;
  }
}
Problem p03702 (C++)

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

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define pb push_back
#define fst first
#define snd second
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

ll n, a, b;
vector<ll> h;

bool calc(ll m) {
  ll c = 0;
  rep(i, n) {
    if (h[i] - b * m > 0)
      c += (h[i] - b * m + a - b - 1) / (a - b);
  }
  if (c <= m)
    return true;
  else
    return false;
}

int main() {
  cin >> n >> a >> b;
  h.resize(n);
  rep(i, n) cin >> h[i];

  ll l = 0, r = 1e9 / b, m;

  while (l + 1 < r) {
    m = (l + r) / 2;
    if (calc(m))
      r = m;
    else
      l = m;
  }

  while (1) {
    if (calc(r)) {
      cout << r << endl;
      break;
    }
  }
}

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

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define pb push_back
#define fst first
#define snd second
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

ll n, a, b;
vector<ll> h;

bool calc(ll m) {
  ll c = 0;
  rep(i, n) {
    if (h[i] - b * m > 0)
      c += (h[i] - b * m + a - b - 1) / (a - b);
  }
  if (c <= m)
    return true;
  else
    return false;
}

int main() {
  cin >> n >> a >> b;
  h.resize(n);
  rep(i, n) cin >> h[i];

  ll l = 0, r = 1e9 / b, m;

  while (l + 1 < r) {
    m = (l + r) / 2;
    if (calc(m))
      r = m;
    else
      l = m;
  }

  while (1) {
    if (calc(r)) {
      cout << r << endl;
      break;
    }
    r++;
  }
}
Problem p03702 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define REP(i, a) for (int i = 0; i < a; ++i)
#define REPP(i, a, b) for (int i = a; i < b; ++i)
#define FILL(a, x) memset(a, x, sizeof(a))
#define foreach(gg, itit)                                                      \
  for (typeof(gg.begin()) itit = gg.begin(); itit != gg.end(); itit++)
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define present(c, x) ((c).find(x) != (c).end())
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e4 + 10;
const ll INF = 1e18;

ll power(ll x, ll y) {
  ll t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}

ll h[N];

int main() {
  ll n, a, b;
  scanf("%lld%lld%lld", &n, &a, &b);
  REP(i, n)
  scanf("%lld", &h[i]);
  sort(h, h + n);
  ll l = 1, r = (h[n - 1] + b - 1) / b, mid;
  while (l < r) {
    mid = (l + r) / 2;
    ll spent = 0;
    REP(i, n) {
      if (h[i] > mid * b)
        spent += (h[i] - mid * b + a - b - 1) / (a - b);
    }
    if (spent > mid)
      l = mid + 1;
    else
      r = mid;
  }
  mid = (l + r) / 2;
  printf("%lld\n", mid);
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define REP(i, a) for (int i = 0; i < a; ++i)
#define REPP(i, a, b) for (int i = a; i < b; ++i)
#define FILL(a, x) memset(a, x, sizeof(a))
#define foreach(gg, itit)                                                      \
  for (typeof(gg.begin()) itit = gg.begin(); itit != gg.end(); itit++)
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define present(c, x) ((c).find(x) != (c).end())
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const ll INF = 1e18;

ll power(ll x, ll y) {
  ll t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}

ll h[N];

int main() {
  ll n, a, b;
  scanf("%lld%lld%lld", &n, &a, &b);
  REP(i, n)
  scanf("%lld", &h[i]);
  sort(h, h + n);
  ll l = 1, r = (h[n - 1] + b - 1) / b, mid;
  while (l < r) {
    mid = (l + r) / 2;
    ll spent = 0;
    REP(i, n) {
      if (h[i] > mid * b)
        spent += (h[i] - mid * b + a - b - 1) / (a - b);
    }
    if (spent > mid)
      l = mid + 1;
    else
      r = mid;
  }
  mid = (l + r) / 2;
  printf("%lld\n", mid);
  return 0;
}
Problem p03702 (C++)

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

template <class T> void scan(T &number) {
  bool negative = false;
  register T c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    number = number * 10 + c - 48;
  if (negative)
    number *= -1;
}

int n, A, B;
int h[N], Max;

bool check(int x) {
  int least = x;
  for (int i = 1; i <= n && least >= 0; i++)
    least -= (max(h[i] - x * B, 0) + A - 1) / A;
  return (least >= 0);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);

  scan(n);
  scan(A);
  scan(B);
  A -= B;
  for (int i = 1; i <= n; i++) {
    scan(h[i]);
    Max = max(Max, h[i]);
  }

  int l = 0, r = Max / B + 1;

  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}

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

template <class T> void scan(T &number) {
  bool negative = false;
  register T c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    number = number * 10 + c - 48;
  if (negative)
    number *= -1;
}

int n, A, B;
int h[N], Max;

bool check(int x) {
  int least = x;
  for (int i = 1; i <= n && least >= 0; i++)
    least -= (max(h[i] - x * B, 0) + A - 1) / A;
  return (least >= 0);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  scan(n);
  scan(A);
  scan(B);
  A -= B;
  for (int i = 1; i <= n; i++) {
    scan(h[i]);
    Max = max(Max, h[i]);
  }

  int l = 0, r = Max / B + 1;

  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p03702 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

typedef long long ll;

ll n, a, b;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> a >> b;
  vector<ll> h(n);
  for (int i = 0; i < n; i++)
    cin >> h.at(i);
  ll l = 0, r = 1000000000;
  while (r > l) {
    ll mid = (r + l) / 2, sum = 0;
    for (int i = 0; i < n; i++)
      if (h.at(i) > mid * b)
        sum += (h.at(i) - mid * b + (a - b - 1)) / (a - b);
    if (sum <= mid)
      r = mid;
    else
      l = mid;
  }
  cout << r << '\n';
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

typedef long long ll;

ll n, a, b;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> a >> b;
  vector<ll> h(n);
  for (int i = 0; i < n; i++)
    cin >> h.at(i);
  ll l = 0, r = 1000000000;
  while (r - l > 1) {
    ll mid = (r + l) / 2, sum = 0;
    for (int i = 0; i < n; i++)
      if (h.at(i) > mid * b)
        sum += (h.at(i) - mid * b + (a - b - 1)) / (a - b);
    if (sum <= mid)
      r = mid;
    else
      l = mid;
  }
  cout << r << '\n';
  return 0;
}
Problem p03702 (C++)

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

using namespace std;

typedef long long ll;

const int N = 1e4 + 10;
int n, di, indi, a[N];

bool check(ll use) {
  ll tmp = use;
  for (int i = 1; i <= n; i++)
    tmp -= (max(0LL, a[i] - use * indi) + di - 1) / di;
  return tmp >= 0;
}
int main() {
  scanf("%d%d%d", &n, &di, &indi);
  di -= indi;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  ll L = 0, R = 1e9;
  while (L <= R) {
    ll mid = (L + R) / 2;
    if (check(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  cout << L;
}


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

using namespace std;

typedef long long ll;

const int N = 2e5 + 10;
int n, di, indi, a[N];

bool check(ll use) {
  ll tmp = use;
  for (int i = 1; i <= n; i++)
    tmp -= (max(0LL, a[i] - use * indi) + di - 1) / di;
  return tmp >= 0;
}
int main() {
  scanf("%d%d%d", &n, &di, &indi);
  di -= indi;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  ll L = 0, R = 1e9;
  while (L <= R) {
    ll mid = (L + R) / 2;
    if (check(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  cout << L;
}
Problem p03702 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long a, b, n, s[10010], l, h = 1000000000, ans, mx;
bool check(long long mid) {
  long long sum = 0;
  for (int i = 0; i < n; i++)
    if (s[i] - mid * b > 0)
      sum += (s[i] - mid * b + a - b - 1) / (a - b);
  return sum <= mid;
}
int main() {
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    mx = max(mx, s[i]);
  }
  long long mid;
  while (l <= h) {
    mid = (l + h) / 2;
    if (check(mid) == 1) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  system("pause");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long a, b, n, s[100010], l, h = 1000000000, ans, mx;
bool check(long long mid) {
  long long sum = 0;
  for (int i = 0; i < n; i++)
    if (s[i] - mid * b > 0)
      sum += (s[i] - mid * b + a - b - 1) / (a - b);
  return sum <= mid;
}
int main() {
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    mx = max(mx, s[i]);
  }
  long long mid;
  while (l <= h) {
    mid = (l + h) / 2;
    if (check(mid) == 1) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  system("pause");
  return 0;
}
Problem p03702 (C++)

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

using ll = long long int;

ll arr[100010] = {};

int main(void) {

  int n, a, b;
  cin >> n >> a >> b;

  ll l = 1, r = 10000000010, ans = 10000000010;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }

  while (true) {
    ll mid = l + (r - l) / 2;

    ll res = mid;

    for (int i = 0; i < n; i++) {
      ll t = arr[i] - mid * b;

      if (t > 0) {
        res -= (t + (a - b) - 1) / (a - b);
      }
    }

    // cout << mid << " " << l << " " << r << " res:" << res << endl;
    if (res < 0) {
      l = mid + 1;
    } else if (res > 0) {
      r = mid - 1;
    } else {
      l = mid;
      break;
    }
  }

  cout << l << endl;
}


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

using ll = long long int;

ll arr[100010] = {};

int main(void) {

  int n, a, b;
  cin >> n >> a >> b;

  ll l = 1, r = 10000000010, ans = 10000000010;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < 100; i++) {
    ll mid = l + (r - l) / 2;

    ll res = mid;

    for (int i = 0; i < n; i++) {
      ll t = arr[i] - mid * b;

      if (t > 0) {
        res -= (t + (a - b) - 1) / (a - b);
      }
    }

    // cout << mid << " " << l << " " << r << " res:" << res << endl;
    if (res < 0) {
      l = mid + 1;
    } else if (res > 0) {
      r = mid - 1;
    } else {
      l = mid;
      break;
    }
  }

  cout << l << endl;
}
Problem p03702 (C++)

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

#define N 10010
#define M 500010

typedef long long ll;
const int INF = 1 << 30;
const int mod = 1000000007;

int a[N], x, y;

bool Check(int n, int k) {
  ll t = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] > y * k)
      t += (1ll * a[i] - y * k + x - 1) / x;
  return t <= k;
}

int main() {
  // freopen("in.in", "r", stdin);
  // freopen("out.out", "w", stdout);

  int n, l = 0, r = 0, mid;
  cin >> n >> x >> y;
  x = x - y;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), r = max(r, (a[i] + y - 1) / y);

  while (l < r) {
    mid = (l + r) >> 1;
    if (Check(n, mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

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

#define N 100010
#define M 500010

typedef long long ll;
const int INF = 1 << 30;
const int mod = 1000000007;

int a[N], x, y;

bool Check(int n, int k) {
  ll t = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] > y * k)
      t += (1ll * a[i] - y * k + x - 1) / x;
  return t <= k;
}

int main() {
  // freopen("in.in", "r", stdin);
  // freopen("out.out", "w", stdout);

  int n, l = 0, r = 0, mid;
  cin >> n >> x >> y;
  x = x - y;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), r = max(r, (a[i] + y - 1) / y);

  while (l < r) {
    mid = (l + r) >> 1;
    if (Check(n, mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p03702 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define maxn 150
using namespace std;
int n, a[maxn];
long long A, B;
bool check(long long x) {
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (x >= a[i])
      continue;
    if (x * B >= a[i])
      continue;
    if (x * A < a[i])
      return 0;
    cnt += (a[i] - x * B + A - B - 1) / (A - B);
  }
  return cnt <= x;
}
int main() {
  cin >> n >> A >> B;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  long long l = 0, r = 1e16;
  while (l < r) {
    long long m = (l + r) >> 1;
    if (check(m))
      r = m;
    else
      l = m + 1;
  }
  cout << r << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define maxn 100009
using namespace std;
int n, a[maxn];
long long A, B;
bool check(long long x) {
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (x >= a[i])
      continue;
    if (x * B >= a[i])
      continue;
    if (x * A < a[i])
      return 0;
    cnt += (a[i] - x * B + A - B - 1) / (A - B);
  }
  return cnt <= x;
}
int main() {
  cin >> n >> A >> B;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  long long l = 0, r = 1e16;
  while (l < r) {
    long long m = (l + r) >> 1;
    if (check(m))
      r = m;
    else
      l = m + 1;
  }
  cout << r << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define sz(x) (int)(x.size())
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/////////////////////

int const N = 1e5 + 41;
ll a[N];
int n;
ll k;
vector<pll> v;
int p[N];

int F[N];

void upd(int x, int v) {
  while (x < N) {
    F[x] += v;
    x |= (x + 1);
  }
}

int get(int x) {
  int r = 0;
  while (x >= 0) {
    r += F[x];
    x = (x & (x + 1)) - 1;
  }
  return r;
}

void solve() {
  cin >> n >> k;
  rep(i, 0, n) {
    cin >> a[i];
    a[i] -= k;
  }
  ll cur = 0;
  v.pb(mp(0, -1));
  rep(i, 0, n) {
    cur += a[i];
    v.pb(mp(cur, i));
  }
  sort(v.begin(), v.end());
  rep(i, 0, sz(v)) p[v[i].second] = i;

  ll ans = 0;
  rep(i, 0, sz(v)) if (v[i].second == -1) {
    upd(i, 1);
    break;
  }
  rep(i, 0, n) {
    ans += get(p[i]);
    upd(p[i], 1);
  }
  cout << ans << endl;
}

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define sz(x) (int)(x.size())
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/////////////////////

int const N = 2e5 + 41;
ll a[N];
int n;
ll k;
vector<pll> v;
int p[N];

int F[N];

void upd(int x, int v) {
  while (x < N) {
    F[x] += v;
    x |= (x + 1);
  }
}

int get(int x) {
  int r = 0;
  while (x >= 0) {
    r += F[x];
    x = (x & (x + 1)) - 1;
  }
  return r;
}

void solve() {
  cin >> n >> k;
  rep(i, 0, n) {
    cin >> a[i];
    a[i] -= k;
  }
  ll cur = 0;
  v.pb(mp(0, -1));
  rep(i, 0, n) {
    cur += a[i];
    v.pb(mp(cur, i));
  }
  sort(v.begin(), v.end());
  rep(i, 0, sz(v)) p[v[i].second] = i;

  ll ans = 0;
  rep(i, 0, sz(v)) if (v[i].second == -1) {
    upd(i, 1);
    break;
  }
  rep(i, 0, n) {
    ans += get(p[i]);
    upd(p[i], 1);
  }
  cout << ans << endl;
}

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}
Problem p03703 (C++)

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

using namespace std;

typedef long long ll;
ll n, k, A[100050], fw[100050];
void update(ll p, ll v) {
  for (; p <= n; p += p & (-p))
    fw[p] += v;
}
ll query(ll p) {
  ll ans = 0;
  for (; p; p -= p & (-p))
    ans += fw[p];
  return ans;
}
int main() {
  cin >> n >> k;
  vector<ll> v;
  for (ll i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
    v.push_back(A[i] - k * i);
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll val = lower_bound(v.begin(), v.end(), A[i] - k * i) - v.begin() + 1;
    ans += query(val);
    update(val, 1);
  }
  cout << ans;
  // A[i]-A[j] >= k*(i-(j)) -> A[i]-k*i >= A[j]-k*j for range [j+1,i]
}


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

using namespace std;

typedef long long ll;
ll n, k, A[200050], fw[200050];
void update(ll p, ll v) {
  for (; p <= n; p += p & (-p))
    fw[p] += v;
}
ll query(ll p) {
  ll ans = 0;
  for (; p; p -= p & (-p))
    ans += fw[p];
  return ans;
}
int main() {
  cin >> n >> k;
  vector<ll> v;
  for (ll i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
    v.push_back(A[i] - k * i);
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll val = lower_bound(v.begin(), v.end(), A[i] - k * i) - v.begin() + 1;
    ans += query(val);
    update(val, 1);
  }
  cout << ans;
  // A[i]-A[j] >= k*(i-(j)) -> A[i]-k*i >= A[j]-k*j for range [j+1,i]
}
Problem p03703 (C++)

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

#define fo(i, s, t) for (int i = s; i <= t; ++i)
const int maxn = 200050;
typedef long long ll;
const ll lim = 2000000000000000ll;

int nd[maxn * 40], ls[maxn * 40], rs[maxn * 40], rt[maxn];
int n, k, a[maxn], tot;
ll s[maxn], ans, delta;

void insert(int &now, int pre, ll l, ll r, ll x) {
  now = ++tot;
  nd[now] = nd[pre] + 1;
  if (l == r)
    return;
  ll mid = l + r >> 1;
  if (x <= mid)
    insert(ls[now], ls[pre], l, mid, x), rs[now] = rs[pre];
  else
    insert(rs[now], rs[pre], mid + 1, r, x), ls[now] = ls[pre];
}

int ask(int now, int pre, ll l, ll r, ll al, ll ar) {
  if (!now)
    return 0;
  if (al <= l && r <= ar)
    return nd[now] - nd[pre];
  ll mid = l + r >> 1;
  if (ar <= mid)
    return ask(ls[now], ls[pre], l, mid, al, ar);
  else if (al > mid)
    return ask(rs[now], rs[pre], mid + 1, r, al, ar);
  else
    return ask(ls[now], ls[pre], l, mid, al, mid) +
           ask(rs[now], rs[pre], mid + 1, r, mid + 1, ar);
}

int main() {
  scanf("%d%d", &n, &k);
  fo(i, 1, n) scanf("%d", &a[i]), a[i] -= k;
  fo(i, 1, n) s[i] = s[i - 1] + a[i], insert(rt[i], rt[i - 1], -lim, lim, s[i]);
  fo(i, 1, n) ans += ask(rt[n], rt[i - 1], -lim, lim, s[i - 1], lim);
  printf("%lld\n", ans);
  return 0;
}

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

#define fo(i, s, t) for (int i = s; i <= t; ++i)
const int maxn = 200050;
typedef long long ll;
const ll lim = 2000000000000000ll;

int nd[maxn * 80], ls[maxn * 80], rs[maxn * 80], rt[maxn];
int n, k, a[maxn], tot;
ll s[maxn], ans, delta;

void insert(int &now, int pre, ll l, ll r, ll x) {
  now = ++tot;
  nd[now] = nd[pre] + 1;
  if (l == r)
    return;
  ll mid = l + r >> 1;
  if (x <= mid)
    insert(ls[now], ls[pre], l, mid, x), rs[now] = rs[pre];
  else
    insert(rs[now], rs[pre], mid + 1, r, x), ls[now] = ls[pre];
}

int ask(int now, int pre, ll l, ll r, ll al, ll ar) {
  if (!now)
    return 0;
  if (al <= l && r <= ar)
    return nd[now] - nd[pre];
  ll mid = l + r >> 1;
  if (ar <= mid)
    return ask(ls[now], ls[pre], l, mid, al, ar);
  else if (al > mid)
    return ask(rs[now], rs[pre], mid + 1, r, al, ar);
  else
    return ask(ls[now], ls[pre], l, mid, al, mid) +
           ask(rs[now], rs[pre], mid + 1, r, mid + 1, ar);
}

int main() {
  scanf("%d%d", &n, &k);
  fo(i, 1, n) scanf("%d", &a[i]), a[i] -= k;
  fo(i, 1, n) s[i] = s[i - 1] + a[i], insert(rt[i], rt[i - 1], -lim, lim, s[i]);
  fo(i, 1, n) ans += ask(rt[n], rt[i - 1], -lim, lim, s[i - 1], lim);
  printf("%lld\n", ans);
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod = 1000000007;
const ll INF = 1e15;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair

const ll MaxN = 3 * 1e5;

ll bit[MaxN];
void add(ll a, ll w) {
  for (ll x = a; x <= MaxN; x += (x & -x))
    bit[x] += w;
}

ll sum(ll a) {
  ll ret = 0;
  for (ll x = a; x > 0; x -= (x & -x))
    ret += bit[x];
  return ret;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vec d;
  rep(i, N) {
    ll x;
    cin >> x;
    d.pb(x - K);
  }
  vec r(N + 1, 0), s(N + 1, 0);
  rep(i, N) {
    r.at(i + 1) = r.at(i) + d.at(i);
    s.at(i + 1) = r.at(i + 1);
  }
  sort(s.begin(), s.end());
  vec p(N + 1);

  rep(i, N + 1) { p.at(i) = find(s.begin(), s.end(), r.at(i)) - s.begin(); }

  ll ans = 0;
  rep(i, N + 1) {
    ans += sum(p.at(i) + 10);
    add(p.at(i) + 10, 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod = 1000000007;
const ll INF = 1e15;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair

const ll MaxN = 3 * 1e5;

ll bit[MaxN];
void add(ll a, ll w) {
  for (ll x = a; x <= MaxN; x += (x & -x))
    bit[x] += w;
}

ll sum(ll a) {
  ll ret = 0;
  for (ll x = a; x > 0; x -= (x & -x))
    ret += bit[x];
  return ret;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vec d;
  rep(i, N) {
    ll x;
    cin >> x;
    d.pb(x - K);
  }
  vec r(N + 1, 0), s(N + 1, 0);
  rep(i, N) {
    r.at(i + 1) = r.at(i) + d.at(i);
    s.at(i + 1) = r.at(i + 1);
  }
  sort(s.begin(), s.end());
  vec p(N + 1);

  rep(i, N + 1) {
    p.at(i) = lower_bound(s.begin(), s.end(), r.at(i)) - s.begin();
  }

  ll ans = 0;
  rep(i, N + 1) {
    ans += sum(p.at(i) + 10);
    add(p.at(i) + 10, 1);
  }
  cout << ans << endl;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define N 202
using namespace std;
long long n, k, tree[N], sum[N], f[N], m, ans;
long long lowbit(int x) { return x & -x; }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
long long query(int x) {
  int ans = 0;
  while (x > 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> sum[i];
  for (int i = 1; i <= n; ++i)
    f[i] = sum[i] += sum[i - 1] - k;
  for (int i = 1; i <= n; ++i)
    if (sum[i] >= 0)
      ans++;
  sort(f + 1, f + n + 1);
  m = unique(f + 1, f + n + 1) - f - 1;
  for (int i = 1; i <= n; ++i)
    sum[i] = lower_bound(f + 1, f + m + 1, sum[i]) - f;
  for (int i = n; i >= 1; --i) {
    ans += query(sum[i]);
    add(1, 1);
    add(sum[i] + 1, -1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define N 202000
using namespace std;
long long n, k, tree[N], sum[N], f[N], m, ans;
long long lowbit(int x) { return x & -x; }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
long long query(int x) {
  int ans = 0;
  while (x > 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> sum[i];
  for (int i = 1; i <= n; ++i)
    f[i] = sum[i] += sum[i - 1] - k;
  for (int i = 1; i <= n; ++i)
    if (sum[i] >= 0)
      ans++;
  sort(f + 1, f + n + 1);
  m = unique(f + 1, f + n + 1) - f - 1;
  for (int i = 1; i <= n; ++i)
    sum[i] = lower_bound(f + 1, f + m + 1, sum[i]) - f;
  for (int i = n; i >= 1; --i) {
    ans += query(sum[i]);
    add(1, 1);
    add(sum[i] + 1, -1);
  }
  cout << ans << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 100010;
int a[MAXN], b[MAXN], c[MAXN], sum[MAXN];
map<int, int> mp;
int n, k, nn;
int lowbit(int x) { return x & (-x); }
int getSum(int x) {
  int ans = 0;
  while (x > 0) {
    ans += sum[x];
    x -= lowbit(x);
  }
  return ans;
}
void update(int x, int num) {
  while (x <= nn) {
    sum[x] += num;
    x += lowbit(x);
  }
}
int main() {
  while (scanf("%d%d", &n, &k) != EOF) {
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    c[0] = 0, b[0] = 0;
    for (int i = 1; i <= n; i++) {
      c[i] = c[i - 1] + a[i] - k;
      b[i] = c[i];
    }
    sort(b, b + 1 + n);
    nn = 0;
    mp.clear();
    for (int i = 0; i <= n; i++) {
      if (!mp[b[i]])
        mp[b[i]] = ++nn;
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += getSum(mp[c[i]]);
      update(mp[c[i]], 1);
    }
    printf("%lld\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 200010;
long long a[MAXN], b[MAXN], c[MAXN], sum[MAXN];
map<long long, int> mp;
int n, k, nn;
int lowbit(int x) { return x & (-x); }
int getSum(int x) {
  int ans = 0;
  while (x > 0) {
    ans += sum[x];
    x -= lowbit(x);
  }
  return ans;
}
void update(int x, int num) {
  while (x <= nn) {
    sum[x] += num;
    x += lowbit(x);
  }
}
int main() {
  while (scanf("%d%d", &n, &k) != EOF) {
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    c[0] = 0, b[0] = 0;
    for (int i = 1; i <= n; i++) {
      c[i] = c[i - 1] + a[i] - k;
      b[i] = c[i];
    }
    sort(b, b + 1 + n);
    nn = 0;
    mp.clear();
    for (int i = 0; i <= n; i++) {
      if (!mp[b[i]])
        mp[b[i]] = ++nn;
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += getSum(mp[c[i]]);
      update(mp[c[i]], 1);
    }
    printf("%lld\n", ans);
  }
}
Problem p03703 (C++)

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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

class BIT {
private:
  ll n;
  vector<ll> bit;

public:
  BIT(ll num) {
    n = num;
    bit.resize(n);
  }

  ll sum(ll i) {
    // 0-indexed->1-indexed
    i++;
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  ll add(ll i, ll x) {
    // 0-indexed->1-indexed
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main() {
source:
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, n) { cin >> a[i]; }
  ll b[n + 1];
  b[0] = 0;
  repd(i, 1, n + 1) { b[i] = b[i - 1] + a[i - 1]; }

  rep(i, n + 1) { b[i] -= k * i; }

  map<ll, ll> m;
  rep(i, n + 1) { m[b[i]] = -1; }

  ll index = 0;
  for (auto iter = m.begin(); iter != m.end(); iter++) {
    iter->second = index;
    index++;
  }
  rep(i, n + 1) { b[i] = m[b[i]]; }

  BIT bit(n + 1);
  ll ans = 0;
  rep(i, n + 1) {
    ans += bit.sum(b[i]);
    bit.add(b[i], 1);
  }

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


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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

class BIT {
private:
  ll n;
  vector<ll> bit;

public:
  BIT(ll num) {
    n = num;
    bit.resize(n + 1);
  }

  ll sum(ll i) {
    // 0-indexed->1-indexed
    i++;
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  ll add(ll i, ll x) {
    // 0-indexed->1-indexed
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main() {
source:
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, n) { cin >> a[i]; }
  ll b[n + 1];
  b[0] = 0;
  repd(i, 1, n + 1) { b[i] = b[i - 1] + a[i - 1]; }

  rep(i, n + 1) { b[i] -= k * i; }

  map<ll, ll> m;
  rep(i, n + 1) { m[b[i]] = -1; }

  ll index = 0;
  for (auto iter = m.begin(); iter != m.end(); iter++) {
    iter->second = index;
    index++;
  }
  rep(i, n + 1) { b[i] = m[b[i]]; }

  BIT bit(n + 1);
  ll ans = 0;
  rep(i, n + 1) {
    ans += bit.sum(b[i]);
    bit.add(b[i], 1);
  }

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

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

const int MAX_N = 100005;
int sz, bit[MAX_N + 1];

// iが与えられた時に、a[1]+a[2]+...+a[i]を計算する
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i; // 最後の1ビット
  }
  return s;
}

// iとxが与えられた時に、a[i]にxを足す
void add(int i, int x) {
  while (i <= sz) {
    bit[i] += x;
    i += i & -i;
  }
}

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

  int n;
  ll k;
  cin >> n >> k;

  ll a[n + 1];
  vector<ll> sa(n + 1);
  a[0] = sa[0] = 0;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= k;
    a[i] += a[i - 1];
    sa[i] = a[i];
  }

  sort(sa.begin(), sa.end());
  unique(sa.begin(), sa.end());

  map<ll, int> mp;
  sz = sa.size();
  for (int i = 1; i <= sz; i++) {
    if (mp[sa[i - 1]] > 0)
      break;
    mp[sa[i - 1]] = i;
  }

  for (int i = 0; i <= n; i++) {
    ans += sum(mp[a[i]]);
    add(mp[a[i]], 1);
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const int MAX_N = 200005;
int sz, bit[MAX_N + 1];

// iが与えられた時に、a[1]+a[2]+...+a[i]を計算する
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i; // 最後の1ビット
  }
  return s;
}

// iとxが与えられた時に、a[i]にxを足す
void add(int i, int x) {
  while (i <= sz) {
    bit[i] += x;
    i += i & -i;
  }
}

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

  int n;
  ll k;
  cin >> n >> k;

  ll a[n + 1];
  vector<ll> sa(n + 1);
  a[0] = sa[0] = 0;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= k;
    a[i] += a[i - 1];
    sa[i] = a[i];
  }

  sort(sa.begin(), sa.end());
  unique(sa.begin(), sa.end());

  map<ll, int> mp;
  sz = sa.size();
  for (int i = 1; i <= sz; i++) {
    if (mp[sa[i - 1]] > 0)
      break;
    mp[sa[i - 1]] = i;
  }

  for (int i = 0; i <= n; i++) {
    ans += sum(mp[a[i]]);
    add(mp[a[i]], 1);
  }

  cout << ans << "\n";
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define maxn 100005
using namespace std;
ll arr[maxn];
int SZ, bit[maxn];
inline int lowbit(int x) { return x & -x; }
void add(int pos) {
  while (pos <= SZ) {
    bit[pos]++;
    pos += lowbit(pos);
  }
}
int sum(int pos) {
  int ret = 0;
  while (pos > 0) {
    ret += bit[pos];
    pos -= lowbit(pos);
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> dct = {0};
  for (int i = 1, val; i <= n; i++) {
    cin >> val;
    arr[i] = arr[i - 1] + val - k;
    dct.push_back(arr[i]);
  }
  sort(dct.begin(), dct.end());
  dct.resize(unique(dct.begin(), dct.end()) - dct.begin());
  SZ = (int)dct.size();
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    int idx = lower_bound(dct.begin(), dct.end(), arr[i]) - dct.begin() + 1;
    ans += sum(idx);
    add(idx);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define maxn 200005
using namespace std;
ll arr[maxn];
int SZ, bit[maxn];
inline int lowbit(int x) { return x & -x; }
void add(int pos) {
  while (pos <= SZ) {
    bit[pos]++;
    pos += lowbit(pos);
  }
}
int sum(int pos) {
  int ret = 0;
  while (pos > 0) {
    ret += bit[pos];
    pos -= lowbit(pos);
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> dct = {0};
  for (int i = 1, val; i <= n; i++) {
    cin >> val;
    arr[i] = arr[i - 1] + val - k;
    dct.push_back(arr[i]);
  }
  sort(dct.begin(), dct.end());
  dct.resize(unique(dct.begin(), dct.end()) - dct.begin());
  SZ = (int)dct.size();
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    int idx = lower_bound(dct.begin(), dct.end(), arr[i]) - dct.begin() + 1;
    ans += sum(idx);
    add(idx);
  }
  cout << ans << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct BIT {
  vector<ll> bit;
  ll MAX_N;

  BIT(ll n) : bit(n), MAX_N(n) {
    for (int i = 0; i < MAX_N; i++) {
      bit[i] = 0;
    }
  }

  ll sum(ll i) {
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(ll i, ll x) {
    while (i <= MAX_N) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= k;
  }
  vector<ll> s(n + 1);
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + a[i - 1];
  }

  vector<ll> tmp(n + 1);
  for (int i = 1; i <= n; i++) {
    tmp[i] = s[i];
  }
  sort(tmp.begin(), tmp.end());

  vector<ll> c(n + 1);
  for (int i = 0; i <= n; i++) {
    ll d = distance(tmp.begin(), lower_bound(tmp.begin(), tmp.end(), s[i]));
    c[i] = d;
  }

  BIT bit(n + 1);
  ll ans = 0;
  for (int j = 0; j <= n; j++) {
    ans += bit.sum(c[j] + 1);
    bit.add(c[j] + 1, 1);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct BIT {
  vector<ll> bit;
  ll MAX_N;

  BIT(ll n) : bit(n), MAX_N(n) {
    for (int i = 0; i < MAX_N; i++) {
      bit[i] = 0;
    }
  }

  ll sum(ll i) {
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(ll i, ll x) {
    while (i <= MAX_N) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= k;
  }
  vector<ll> s(n + 1);
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + a[i - 1];
  }

  vector<ll> tmp(n + 1);
  for (int i = 1; i <= n; i++) {
    tmp[i] = s[i];
  }
  sort(tmp.begin(), tmp.end());

  vector<ll> c(n + 1);
  for (int i = 0; i <= n; i++) {
    ll d = distance(tmp.begin(), lower_bound(tmp.begin(), tmp.end(), s[i]));
    c[i] = d;
  }

  BIT bit(n + 10);
  ll ans = 0;
  for (int j = 0; j <= n; j++) {
    ans += bit.sum(c[j] + 1);
    bit.add(c[j] + 1, 1);
  }
  cout << ans << endl;
}
Problem p03703 (C++)

Buggy submission (status: Time Limit Exceeded):
// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#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 <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

using namespace std;

// }}}

int main() {
  long long N, K;
  vector<long long> a;
  cin >> N >> K;

  long long currentsum = 0;

  a.push_back(0);
  FOR(i, 0, N) {
    long long tmp;
    cin >> tmp;
    tmp -= K;
    currentsum += tmp;
    a.push_back(currentsum);
  }

  vector<long long> sorted;
  vector<long long> tmp;

  long long freq = sqrt(N);

  long long ret = 0;

  FOR(i, 0, N + 1) {
    long long t = 0;

    FOR(j, 0, tmp.size()) {
      if (tmp[j] <= a[i]) {
        t++;
      }
    }
    tmp.push_back(a[i]);

    long long upper = sorted.size();
    long long lower = 0;

    while (lower < upper) {
      long long mid = (lower + upper) / 2;

      if (sorted[mid] <= a[i]) {
        lower = mid + 1;
      } else {
        upper = mid;
      }
    }

    ret += t + lower;

    if (i % freq == 0) {
      FOR(j, 0, tmp.size()) { sorted.push_back(tmp[j]); }
      tmp.clear();
      sort(ALL(sorted));
    }
  }

  cout << ret << endl;
}


Fixed submission:
// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#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 <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

using namespace std;

// }}}

int main() {
  long long N, K;
  vector<long long> a;
  cin >> N >> K;

  long long currentsum = 0;

  a.push_back(0);
  FOR(i, 0, N) {
    long long tmp;
    cin >> tmp;
    tmp -= K;
    currentsum += tmp;
    a.push_back(currentsum);
  }

  vector<long long> sorted;
  vector<long long> tmp;

  long long freq = sqrt(N) * 2;

  long long ret = 0;

  FOR(i, 0, N + 1) {
    long long t = 0;

    FOR(j, 0, tmp.size()) {
      if (tmp[j] <= a[i]) {
        t++;
      }
    }
    tmp.push_back(a[i]);

    long long upper = sorted.size();
    long long lower = 0;

    while (lower < upper) {
      long long mid = (lower + upper) / 2;

      if (sorted[mid] <= a[i]) {
        lower = mid + 1;
      } else {
        upper = mid;
      }
    }

    ret += t + lower;

    if (i % freq == 0) {
      FOR(j, 0, tmp.size()) { sorted.push_back(tmp[j]); }
      tmp.clear();
      sort(ALL(sorted));
    }
  }

  cout << ret << endl;
}