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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define erep(i, a, b) for (int i = a; i <= (int)(b); ++i)
#define per(i, a, b) for (int i = (b); i > (a); --i)
#define eper(i, a, b) for (int i = ((int)(a)); i >= b; --i)
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

ll n, c, k, ans;
vector<ll> v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> c >> k;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  rep(i, 0, n) {
    ll now = v[i], cnt = 1, x = v[i];
    while (v[i + 1] <= now + k && cnt < c) {
      cnt++, i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define erep(i, a, b) for (int i = a; i <= (int)(b); ++i)
#define per(i, a, b) for (int i = (b); i > (a); --i)
#define eper(i, a, b) for (int i = ((int)(a)); i >= b; --i)
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

ll n, c, k, ans;
vector<ll> v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> c >> k;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  v[n] = INF;
  rep(i, 0, n) {
    ll now = v[i], cnt = 1, x = v[i];
    while (v[i + 1] <= now + k && cnt < c) {
      cnt++, i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;
typedef long long ll;

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> t(N);
  for (ll i = 0; i < N; i++) {
    cin >> t[i];
  }
  sort(t.begin(), t.end());
  ll i = 0;
  ll res = 0;
  while (i < N) {
    res++;
    ll start = i;
    while (i < N && t[i] - t[start] && i - start < C)
      i++;
  }
  cout << res << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> t(N);
  for (ll i = 0; i < N; i++) {
    cin >> t[i];
  }
  sort(t.begin(), t.end());
  ll i = 0;
  ll res = 0;
  while (i < N) {
    res++;
    ll start = i;
    while (i < N && t[i] - t[start] <= K && i - start < C)
      i++;
  }
  cout << res << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
// #include<queue>
// #define d double
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = 1e5 + 233;
int a[maxn];
int i, j, k, n, m;
int p;

int ra, fh;
char rx;
inline int read() {
  rx = getchar(), ra = 0, fh = 1;
  while (rx < 48)
    rx == '-' && (fh = -1), rx = getchar();
  while (rx > 47)
    ra = ra * 10 + rx - 48, rx = getchar();
  return ra * fh;
}

int main() {
  int n = read(), c = read(), k = read();
  for (i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + 1 + n);

  int l, r, mid, ans = 0;
  for (i = 1; i <= n; i = l + 1) {
    l = i, r = i + c - 1;
    while (l < r)
      if (a[mid = (l + r + 1) >> 1] - a[i] > k)
        r = mid - 1;
      else
        l = mid;
    ans++;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
// #include<queue>
// #define d double
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = 1e5 + 233;
int a[maxn];
int i, j, k, n, m;
int p;

int ra, fh;
char rx;
inline int read() {
  rx = getchar(), ra = 0, fh = 1;
  while (rx < 48)
    rx == '-' && (fh = -1), rx = getchar();
  while (rx > 47)
    ra = ra * 10 + rx - 48, rx = getchar();
  return ra * fh;
}

int main() {
  int n = read(), c = read(), k = read();
  for (i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + 1 + n);

  /*int l,r,mid,ans=0;
  for(i=1;i<=n;i=l+1){
          l=i,r=i+c-1;
          while(l<r)if(a[mid=(l+r+1)>>1]-a[i]>k)r=mid-1;else l=mid;
          ans++;
  }printf("%d\n",ans);*/
  int ans = 0, now = 0;
  for (i = 1; i <= n; i = j + 1) {
    for (j = i, now = 0; j <= n; j++)
      if (a[j] - a[i] > k || now++ == c)
        break;
    j--, ans++;
  }
  printf("%d\n", ans);
}
Problem p03785 (C++)

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

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  vector<int> vec(n);
  for (auto &e : vec) {
    cin >> e;
  }

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

  int bus = 0;
  for (int i = 0; i < n;) {
    //		cout << "bus:" << bus << endl;
    //		cout << "  i:" << i << endl;
    //		cout << "vec:" << vec.at(i) << endl;

    int x = 0;
    for (int j = i + 1; j < i + c; j++) {
      if (j < n) {
        if (vec.at(j) <= vec.at(i) + k) {
          x++;
        }
      }
    }
    if (x == (c - 1)) {
      bus++;
      i += c;
    } else {
      bus++;
      i += x + 1;
    }
  }
  cout << bus << endl;
}


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

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  vector<int> vec(n);
  for (auto &e : vec) {
    cin >> e;
  }

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

  int bus = 0;
  for (int i = 0; i < n;) {
    //		cout << "bus:" << bus << endl;
    //		cout << "  i:" << i << endl;
    //		cout << "vec:" << vec.at(i) << endl;

    int x = 0;
    for (int j = i + 1; j < i + c; j++) {
      if (j < n) {
        if (vec.at(j) <= vec.at(i) + k) {
          x++;
        } else {
          break;
        }
      }
    }
    if (x == (c - 1)) {
      bus++;
      i += c;
    } else {
      bus++;
      i += x + 1;
    }
  }
  cout << bus << endl;
}
Problem p03785 (C++)

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

#ifdef DEBUG
#include "../include/debug.hpp"
#else
#define debug(...) 42
#endif

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

  int N, C, K;
  cin >> N >> C >> K;

  vector<int> T;
  for (int i = 0; i < N; ++i)
    cin >> T[i];
  sort(T.begin(), T.end());

  int bus = 0, people = 0, depart = -1;
  for (int i = 0; i < N; ++i) {
    if (depart < T[i] || C == people) {
      depart = T[i] + K;
      people = 0;
      bus++;
    }
    people++;
  }

  cout << bus << endl;

  return 0;
}

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

#ifdef DEBUG
#include "../include/debug.hpp"
#else
#define debug(...) 42
#endif

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

  int N, C, K;
  cin >> N >> C >> K;

  vector<int> T(N);
  for (int i = 0; i < N; ++i)
    cin >> T[i];
  sort(T.begin(), T.end());

  int bus = 0, people = 0, depart = -1;
  for (int i = 0; i < N; ++i) {
    if (depart < T[i] || C == people) {
      depart = T[i] + K;
      people = 0;
      bus++;
    }
    people++;
  }

  cout << bus << endl;

  return 0;
}
Problem p03785 (C++)

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

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

#define y1 what

using namespace std;

const int N = (int)1e5 + 10;
const int M = (int)101;
const ll big = (1LL << 51);
const ll LINF = (ll)1e18;
const int INF = (int)1e9 + 7;
const int INF1 = (int)1e9 + 3;
const double EPS = (double)1e-6;
const double PI = 3.14159265359;

int n, c, k;
int t[N], dp[N];

int main() {
#define fn "balls"
#ifdef witch
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  //        freopen(fn".in", "r", stdin);
  //        freopen(fn".out", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> c >> k;
  for (int i = 1; i <= n; i++) {
    cin >> t[i];
  }
  sort(t + 1, t + n + 1);
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    for (int j = 0; j < i; j++) {
      if (i - j <= c && t[j + 1] + k >= t[i]) {
        dp[i] = min(dp[i], dp[j] + 1);
      }
    }
  }
  cout << dp[n];
  return 0;
}

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

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

#define y1 what

using namespace std;

const int N = (int)1e5 + 10;
const int M = (int)101;
const ll big = (1LL << 51);
const ll LINF = (ll)1e18;
const int INF = (int)1e9 + 7;
const int INF1 = (int)1e9 + 3;
const double EPS = (double)1e-6;
const double PI = 3.14159265359;

int n, c, k;
int t[N], dp[N];

int main() {
#define fn "balls"
#ifdef witch
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  //        freopen(fn".in", "r", stdin);
  //        freopen(fn".out", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> c >> k;
  for (int i = 1; i <= n; i++) {
    cin >> t[i];
  }
  sort(t + 1, t + n + 1);
  multiset<int> st;
  st.insert(0);
  for (int i = 1, l = 0; i <= n; i++) {
    while (l < i && (t[l + 1] + k < t[i] || i - l > c)) {
      st.erase(st.find(dp[l]));
      ++l;
    }
    dp[i] = *st.begin() + 1;
    st.insert(dp[i]);
    continue;
    dp[i] = INF;
    for (int j = 0; j < i; j++) {
      if (i - j <= c && t[j + 1] + k >= t[i]) {
        dp[i] = min(dp[i], dp[j] + 1);
      }
    }
  }
  cout << dp[n];
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n, c, k, q, sm = 0, z, st[100010], cud = 0;
// vector<int> t;

int main() {
  scanf("%d %d %d", &n, &c, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &st[i]);
  }
  sort(st, st + n);

  while (cud != n) {
    z = 0;
    for (int i = cud; i < n; i++) {
      if (st[i] <= st[cud] + k)
        z++;
      if (z == c)
        break;
    }
    sm++;
    cud += z;
  }

  printf("%d\n", sm);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n, c, k, q, sm = 0, z, st[100010], cud = 0;
// vector<int> t;

int main() {
  scanf("%d %d %d", &n, &c, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &st[i]);
  }
  sort(st, st + n);

  while (cud != n) {
    z = 0;
    for (int i = cud; i < n; i++) {
      if (st[i] <= st[cud] + k)
        z++;
      else
        break;
      if (z == c)
        break;
    }
    sm++;
    cud += z;
  }

  printf("%d\n", sm);
}
Problem p03785 (C++)

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

const int maxn = 100005;

int n, c, k;
int a[maxn];
int ans = 0;

void work() {
  for (int i = 1; i <= n; i++) {
    ans++;
    int take = c, t = a[i] + k;
    take--;
    while (a[i + 1] <= t && take)
      take--, i++;
  }
}

int main() {
  cin >> n >> c >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + 1 + n);
  work();
  cout << ans << endl;
  return 0;
}

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

const int maxn = 100005;

int n, c, k;
int a[maxn];
int ans = 0;

void work() {
  for (int i = 1; i <= n; i++) {
    ans++;
    int take = c, t = a[i] + k;
    take--;
    while (a[i + 1] <= t && take && i <= n)
      take--, i++;
  }
}

int main() {
  cin >> n >> c >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + 1 + n);
  work();
  cout << ans << endl;
  return 0;
}
Problem p03785 (C++)

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

using namespace std;
const int maxn = 100010;

int a[maxn];
int main() {
  int n;
  scanf("%d", &n);
  int c;
  scanf("%d", &c);
  int k;
  scanf("%d", &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  sort(a, a + n);
  int ans = 0;
  for (int i = 0, j = 0; i < n; i = j) {
    for (j = i + 1; j - i + 1 <= c && a[j] <= a[i] + k; ++j)
      ;
    ans += 1;
  }
  printf("%d\n", ans);
  return 0;
}


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

using namespace std;
const int maxn = 100010;

int a[maxn];
int main() {
  int n;
  scanf("%d", &n);
  int c;
  scanf("%d", &c);
  int k;
  scanf("%d", &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  sort(a, a + n);
  int ans = 0;
  for (int i = 0, j = 0; i < n; i = j) {
    for (j = i + 1; j < n && j - i + 1 <= c && a[j] <= a[i] + k; ++j)
      ;
    ans += 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03785 (C++)

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

const int N = 100000 + 10;

int n, c, k, t[N];

int main() {
  scanf("%d%d%d", &n, &c, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &t[i]);
  std::sort(t + 1, t + n + 1);
  int ans = 0;
  for (int i = 1; i <= n;) {
    int temp = t[i] + k;
    for (int j = c; j-- && t[i] <= temp;)
      ++i;
    ++ans;
  }
  printf("%d\n", ans);
  return 0;
}


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

const int N = 100000 + 10;

int n, c, k, t[N];

int main() {
  scanf("%d%d%d", &n, &c, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &t[i]);
  std::sort(t + 1, t + n + 1);
  int ans = 0;
  for (int i = 1; i <= n;) {
    int temp = t[i] + k;
    for (int j = c; j-- && i <= n && t[i] <= temp;)
      ++i;
    ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> P;

#define INF (1 << 29)
#define INFL (1ll << 60)
#define EPS (1e-8)
#define PI (acos(-1))
const ll MOD = 1000000007ll;

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  int t[112345];
  REP(i, n) scanf("%d", t + i);
  sort(t, t + n);

  int ans = 0;
  bool done[112345] = {};
  REP(i, n) {
    if (done[i])
      continue;

    ll now = t[i] + k;
    REP(j, c) if ((ll)t[i + j] <= now) done[i + j] = true;
    ans++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> P;

#define INF (1 << 29)
#define INFL (1ll << 60)
#define EPS (1e-8)
#define PI (acos(-1))
const ll MOD = 1000000007ll;

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  int t[112345];
  REP(i, n) scanf("%d", t + i);
  sort(t, t + n);

  int ans = 0;
  bool done[112345] = {};
  REP(i, n) {
    if (done[i])
      continue;

    ll now = t[i] + k;
    REP(j, c) if (i + j < n && t[i + j] <= now) done[i + j] = true;
    ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03785 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    n, c, k, *t = map(int, open(0).read().split())
    t = sorted(t)
    u = tuple(t)

    count = 0
    i = 0
    while i < n:
        x = u[i] + k
        y = min(i + c - 1, n - 1)
        while True:
            if u[y] <= x:
                break
            y -= 1

        count += 1
        i = y + 1

    print(count)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, c, k, *t = map(int, open(0).read().split())
    t = sorted(t)
    u = tuple(t)

    cnt = 1
    p, l = 0, u[0] + k
    for v in t:
        if v <= l and p < c:
            p += 1
        else:
            cnt += 1
            l = v + k
            p = 1
    print(cnt)


if __name__ == "__main__":
    main()
Problem p03785 (C++)

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

using namespace std;

int n, c, k, T[20000], ans;

int main() {
  scanf("%d%d%d", &n, &c, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &T[i]);
  sort(T + 1, T + 1 + n);
  int tot = 1, tmp = T[1];
  for (int i = 2; i <= n; i++) {
    if (T[i] > tmp + k) {
      ans++;
      tmp = T[i];
      tot = 0;
    }
    if (tot >= c) {
      ans++;
      tmp = T[i];
      tot = 0;
    }
    tot++;
  }
  cout << ans + 1 << endl;
}

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

using namespace std;

int n, c, k, T[200000], ans;

int main() {
  scanf("%d%d%d", &n, &c, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &T[i]);
  sort(T + 1, T + 1 + n);
  int tot = 1, tmp = T[1];
  for (int i = 2; i <= n; i++) {
    if (T[i] > tmp + k) {
      ans++;
      tmp = T[i];
      tot = 0;
    }
    if (tot >= c) {
      ans++;
      tmp = T[i];
      tot = 0;
    }
    tot++;
  }
  cout << ans + 1 << endl;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn ((int)1e5 + 10)
using namespace std;
int n;
long long c, k, res = 0, r;
vector<pair<long long, long long>> v;
bool vis[maxn];
void dfs(int pos, int y) {
  if (v[pos].first > y || r == c)
    return;
  ++r;
  vis[pos] = true;
  dfs(pos + 1, y);
}
int main() {
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(make_pair(x, x + k));
  }
  sort(v.begin(), v.end());
  memset(vis, false, sizeof(vis));
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      r = 0;
      ++res;
      dfs(i, v[i].second);
    }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn ((int)1e5 + 10)
using namespace std;
int n;
long long c, k, res = 0, r;
vector<pair<long long, long long>> v;
bool vis[maxn];
void dfs(int pos, int y) {
  if (v[pos].first > y || r == c)
    return;
  ++r;
  vis[pos] = true;
  if (pos == n - 1)
    return;
  dfs(pos + 1, y);
}
int main() {
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(make_pair(x, x + k));
  }
  sort(v.begin(), v.end());
  memset(vis, false, sizeof(vis));
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      r = 0;
      ++res;
      dfs(i, v[i].second);
    }
  cout << res << endl;
  return 0;
}
Problem p03785 (C++)

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> t(n);
  for (int &i : t) {
    cin >> i;
  }
  sort(t.begin(), t.end());
  int bus = 1, j;
  for (int i = 0; i < n; ++i) {
    if (t[i] > t[j] + k or i - j == c) {
      ++bus;
      j = i;
    }
  }
  cout << bus << "\n";
  return 0;
}


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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> t(n);
  for (int &i : t) {
    cin >> i;
  }
  sort(t.begin(), t.end());
  int bus = 1, j = 0;
  for (int i = 0; i < n; ++i) {
    if (t[i] > t[j] + k or i - j == c) {
      ++bus;
      j = i;
    }
  }
  cout << bus << "\n";
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Time Limit Exceeded):
//
// AtCoder Grand Contest 011
// A
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  vector<int> t(n);
  REP(i, n) cin >> t[i];
  sort(ALL(t));

  int ans = 0;
  int idx = 0;
  while (idx < n) {
    int st = -1;
    REP(i, c) {
      if (st == -1) {
        st = t[idx] + k;
        idx++;
      } else {
        if (st >= t[idx])
          idx++;
      }
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
//
// AtCoder Grand Contest 011
// A
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  int n, c, k;
  cin >> n >> c >> k;

  vector<int> t(n);
  REP(i, n) cin >> t[i];
  sort(ALL(t));

  int ans = 0;
  int idx = 0;
  while (idx < n) {
    int st = -1;
    REP(i, c) {
      if (st == -1) {
        st = t[idx] + k;
        idx++;
      } else {
        if (st >= t[idx])
          idx++;
        else
          break;
      }
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n, c, k;
int t[100100];

int main() {
  cin >> n >> c >> k;
  REP(i, n)
  cin >> t[i];
  sort(t, t + n);
  int ans = 0;
  int bus = 0;
  int busar = 0;
  int i = 0;
  while (i < n) {
    busar = t[i];
    for (int j = 1; j < c; j++) {
      if (i >= n)
        break;
      i++;
      if (t[i] - busar > k) {
        break;
      }
      if (j == c - 1)
        i++;
    }
    bus++;
  }
  cout << bus << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n, c, k;
int t[100100];

int main() {
  cin >> n >> c >> k;
  REP(i, n)
  cin >> t[i];
  sort(t, t + n);
  int ans = 0;
  int bus = 0;
  int busar = 0;
  int i = 0;
  if (c == 1) {
    cout << n << endl;
    return 0;
  }
  while (i < n) {
    busar = t[i];
    for (int j = 1; j < c; j++) {
      if (i >= n)
        break;
      i++;
      if (t[i] - busar > k) {
        break;
      }
      if (j == c - 1)
        i++;
    }
    bus++;
  }
  cout << bus << endl;
  return 0;
}
Problem p03785 (C++)

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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define PB push_back

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVi;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false);
  long long n, c, k;

  cin >> n >> c >> k;
  VI t(n);
  rep(i, n) { cin >> t[i]; }
  sort(t.begin(), t.end());
  int fs = 0;
  int bcnt = 0;
  int ans = 0;
  int i = 0;
  while (i < n) {
    while (t[i] <= t[fs] + k && bcnt < c) {
      bcnt++;
      i++;
    }
    fs = i;
    if (bcnt > 0) {
      ans++;
      bcnt = 0;
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define PB push_back

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVi;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false);
  long long n, c, k;

  cin >> n >> c >> k;
  VI t(n);
  rep(i, n) { cin >> t[i]; }
  sort(t.begin(), t.end());
  int fs = 0;
  int bcnt = 0;
  int ans = 0;
  int i = 0;
  while (i < n) {
    while (i < n && t[i] <= t[fs] + k && bcnt < c) {
      bcnt++;
      i++;
    }
    fs = i;
    if (bcnt > 0) {
      ans++;
      bcnt = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[10005];
int n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] + a[i] < a[i + 1])
      ans = 0;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100005];
int n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] + a[i] < a[i + 1])
      ans = 0;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long LL;
const int N = 100005;
LL foo[N];
LL too[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &foo[i]);
  }
  sort(foo, foo + n);
  for (int i = n - 1; i > 0; i--) {
    too[i] = foo[i];
    for (int j = i - 1; j >= 0; j--) {
      too[i] += foo[j];
    }
  }
  too[0] = foo[0];
  int h = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (2 * too[i] >= foo[i + 1]) {
      h++;
    } else {
      break;
    }
  }
  printf("%d\n", h);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long LL;
const int N = 100005;
LL foo[N];
LL too[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &foo[i]);
  }
  sort(foo, foo + n);
  for (int i = 0; i < n; i++) {
    if (i) {
      too[i] += (too[i - 1] + foo[i]);
    } else {
      too[i] = foo[i];
    }
  }
  too[0] = foo[0];
  int h = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (2 * too[i] >= foo[i + 1]) {
      h++;
    } else {
      break;
    }
  }
  printf("%d\n", h);
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
int n, a[10005], s, ans;
int main() {
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  std::sort(a, a + n);
  for (i = 0; i < n - 1; i++) {
    s += a[i];
    if (s * 2 >= a[i + 1])
      ans++;
    else
      ans = 0;
  }
  printf("%d", ans + 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
long long n, a[100005], s, ans;
int main() {
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  std::sort(a, a + n);
  for (i = 0; i < n - 1; i++) {
    s += a[i];
    if (s * 2 >= a[i + 1])
      ans++;
    else
      ans = 0;
  }
  printf("%d", ans + 1);
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
int main() {
  long int a[1 << 7], n, m = -1, i;
  std::cin >> n;
  for (i = 0; i < n; i++)
    std::cin >> a[i];
  std::sort(a, a + n);
  for (i = 0; i < n - 1; i++) {
    a[i + 1] += a[i];
    if (3 * a[i] < a[i + 1])
      m = i;
  }
  std::cout << n - m - 1;
}

Fixed submission:
#include <algorithm>
#include <iostream>
int main() {
  long int a[100001], n, m = -1, i;
  std::cin >> n;
  for (i = 0; i < n; i++)
    std::cin >> a[i];
  std::sort(a, a + n);
  for (i = 0; i < n - 1; i++) {
    a[i + 1] += a[i];
    if (3 * a[i] < a[i + 1])
      m = i;
  }
  std::cout << n - m - 1;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<long long int> a(n), as(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  as[0] = a[0];
  for (int i = 1; i < n; i++)
    as[i] = a[i] + as[i - 1];
  int ind = 0;
  for (int i = n - 1; i > 0; i--) {
    if (a[i] > 2 * as[i - 1]) {
      ind = i;
      break;
    }
  }
  cout << n - ind << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long int> a(n), as(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  as[0] = a[0];
  for (int i = 1; i < n; i++)
    as[i] = a[i] + as[i - 1];
  int ind = 0;
  for (int i = n - 1; i > 0; i--) {
    if (a[i] > 2 * as[i - 1]) {
      ind = i;
      break;
    }
  }
  cout << n - ind << endl;
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[10005];
int n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] * 2 < a[i + 1])
      ans = 0;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1000005];
int n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] * 2 < a[i + 1])
      ans = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long a[10001], n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] * 2 < a[i + 1])
      ans = 0;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long a[100001], n, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    a[i] += a[i - 1];
    ans++;
    if (a[i] * 2 < a[i + 1])
      ans = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p03786 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
typedef pair<ll, ll> pll;
void read(int &x) { scanf("%d", &x); }
void read(ll &x) { scanf("%lld", &x); }
template <typename T, typename... Args> void read(T &a, Args &...b) {
  read(a);
  read(b...);
}
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define x first
#define y second

int n;
ll a[100010];
ll s[100010];
ll ps[100010];

int main() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]), s[i] = a[i];
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; ++i)
    ps[i] = s[i] + ps[i - 1];
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int j = upper_bound(s + 1, s + n + 1, a[i]) - s - 1;
    ll sz = ps[j];
    while (j < n) {
      if (upper_bound(s + 1, s + n + 1, 2 * sz) - s - 1 == j)
        break;
    }
    if (j == n)
      ++ans;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
typedef pair<ll, ll> pll;
void read(int &x) { scanf("%d", &x); }
void read(ll &x) { scanf("%lld", &x); }
template <typename T, typename... Args> void read(T &a, Args &...b) {
  read(a);
  read(b...);
}
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define x first
#define y second

int n;
ll a[100010];
ll s[100010];
ll ps[100010];

int main() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]), s[i] = a[i];
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; ++i)
    ps[i] = s[i] + ps[i - 1];
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int j = upper_bound(s + 1, s + n + 1, a[i]) - s - 1;
    ll sz = ps[j];
    while (j < n) {
      if (upper_bound(s + 1, s + n + 1, 2 * sz) - s - 1 == j)
        break;
      j = upper_bound(s + 1, s + n + 1, 2 * sz) - s - 1;
      sz = ps[j];
    }
    if (j == n)
      ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03786 (C++)

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

typedef long long ll;

ll a[10003], sum[100003];
bool flag[100003];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  a[n] = 1e9;
  sort(a, a + n);
  sum[0] = a[0];
  for (int i = 1; i < n; ++i)
    sum[i] = sum[i - 1] + a[i];
  flag[n - 1] = flag[n] = true;
  int ans = 1;
  for (int i = n - 2; i >= 0; --i) {
    int ind = upper_bound(a, a + n, 2 * sum[i]) - a;
    --ind;
    flag[i] = flag[ind];
    if (flag[i])
      ++ans;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

ll a[100003], sum[100003];
bool flag[100003];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  a[n] = 1e9;
  sort(a, a + n);
  sum[0] = a[0];
  for (int i = 1; i < n; ++i)
    sum[i] = sum[i - 1] + a[i];
  flag[n - 1] = flag[n] = true;
  int ans = 1;
  for (int i = n - 2; i >= 0; --i) {
    int ind = upper_bound(a, a + n, 2 * sum[i]) - a;
    --ind;
    flag[i] = flag[ind];
    if (flag[i])
      ++ans;
  }
  cout << ans << endl;

  return 0;
}
Problem p03786 (C++)

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

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<ll> sum(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++)
    sum[i + 1] += sum[i] + a[i];
  int cnt = 1;

  for (int i = n - 1; i >= 0; i--) {
    // cout << sum[i] << endl;
    if (sum[i] * 2 < a[i])
      break;
    cnt++;
  }
  cout << cnt << endl;
}

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

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<ll> sum(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++)
    sum[i + 1] += sum[i] + a[i];
  int cnt = 1;

  for (int i = n - 1; i >= 0; i--) {
    // cout << sum[i] << endl;
    if (sum[i] * 2 < a[i])
      break;
    cnt++;
  }
  cout << cnt << endl;
}
Problem p03786 (C++)

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

int main() {
  int n, a[100000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int m = 0;
  for (int i = n - 1; i >= 0; i--) {
    int64_t s = a[i];
    int j;
    for (j = 0; j < n; j++) {
      if (a[j] <= 2 * s) {
        if (i != j) {
          s += a[j];
        }
      } else {
        break;
      }
    }
    if (j == n) {
      m++;
    } else {
      break;
    }
  }
  cout << m << endl;
  return 0;
}


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

int main() {
  int n, a[100000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int64_t s[100000];
  s[0] = a[0];
  for (int i = 1; i < n; i++) {
    s[i] = s[i - 1] + a[i];
  }
  int m = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (2 * s[i] >= a[i + 1]) {
      m++;
    } else {
      break;
    }
  }
  cout << m << endl;
  return 0;
}
Problem p03786 (Python)

Buggy submission (status: Time Limit Exceeded):
# https://atcoder.jp/contests/agc011/tasks/agc011_b

import sys

input = sys.stdin.buffer.readline

n = int(input())
A = sorted([int(i) for i in input().strip().split()])
_max = A[-1]
cul_sum = [0] * (n + 1)

for i in range(1, n + 1):
    cul_sum[i] = cul_sum[i - 1] + A[i - 1]

ans = -1

cur = 0
ptr = cur
_tmp = cul_sum[ptr]
while True:
    _tmp += A[ptr]
    if _tmp * 2 >= _max:
        ans = cur
        break
    elif _tmp * 2 < A[ptr + 1]:
        cur += 1
        ptr = cur
        _tmp = cul_sum[ptr]
    else:
        ptr += 1

print(n - ans)


Fixed submission:
# https://atcoder.jp/contests/agc011/tasks/agc011_b

import sys

input = sys.stdin.buffer.readline

n = int(input())
A = sorted([int(i) for i in input().strip().split()])
_max = A[-1]
cul_sum = [0] * (n + 1)

for i in range(1, n + 1):
    cul_sum[i] = cul_sum[i - 1] + A[i - 1]

ptr = 0
for i in range(1, n + 1):
    if cul_sum[i] * 2 < A[i]:
        ptr = i
    elif cul_sum[i] * 2 >= _max:
        print(n - ptr)
        sys.exit()
Problem p03787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define P(x) cout << x << endl
#define D(x) P(#x << ": " << x)
#define F(i, n) for (int i = 0; i < (int)(n); i++)
#define DEC(i, n) for (int i = (int)(n); --i >= 0;)
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 100000;

vector<int> adj[N];
int col[N];

bool bipa(int u, int c) {
  if (col[u])
    return col[u] == c;
  col[u] = c;
  bool ok = true;
  for (int v : adj[u])
    if (!bipa(v, 3 - c))
      ok = false;
  return ok;
}

signed main() {
  int n, m;
  cin >> n >> m;
  F(i, m) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int alone = 0, bip = 0, other = 0;
  F(i, n) if (!col[i]) {
    if (adj[i].empty()) {
      col[i] = 1;
      alone++;
    } else if (bipa(i, 1))
      bip++;
    else
      other++;
  }
  int res = n * n - (n - alone) * (n - alone);
  res += (other + bip) * (other + bip) + bip * bip;
  P(res);
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define P(x) cout << x << endl
#define D(x) P(#x << ": " << x)
#define F(i, n) for (int i = 0; i < (int)(n); i++)
#define DEC(i, n) for (int i = (int)(n); --i >= 0;)
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 100000;

vector<int> adj[N];
int col[N];

bool bipa(int u, int c) {
  if (col[u])
    return col[u] == c;
  col[u] = c;
  bool ok = true;
  for (int v : adj[u])
    if (!bipa(v, 3 - c))
      ok = false;
  return ok;
}

signed main() {
  int n, m;
  cin >> n >> m;
  F(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int alone = 0, bip = 0, other = 0;
  F(i, n) if (!col[i]) {
    if (adj[i].empty()) {
      col[i] = 1;
      alone++;
    } else if (bipa(i, 1))
      bip++;
    else
      other++;
  }
  int res = n * n - (n - alone) * (n - alone);
  res += (other + bip) * (other + bip) + bip * bip;
  P(res);
}
Problem p03787 (C++)

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

ll N, M, color[100001], A, B, C;
vector<int> G[100001];

bool dfs(int now, int c) {
  color[now] = c;
  for (int i = 0; i < G[now].size(); i++) {
    if (color[G[now][i]] != -1) {
      if (color[G[now][i]] == c)
        return false;
      continue;
    } else {
      if (!dfs(G[now][i], 1 - c))
        return false;
    }
  }
  return true;
}

void dfs2(int now) {
  color[now] = 2;
  for (int i = 0; i < G[now].size(); i++) {
    if (color[G[now][i]] == 2)
      dfs2(G[now][i]);
  }
  return;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 0; i < N; i++)
    color[i] = -1;
  for (int i = 0; i < N; i++) {
    if (!G[i].size())
      A++;
    else {
      if (color[i] != -1)
        continue;
      if (dfs(i, 0))
        B++;
      else {
        C++;
        dfs2(i);
      }
    }
  }
  ll ans = 2 * B * B + A * A + 2 * A * (N - A) + 2 * B * C + C * C;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

ll N, M, color[100001], A, B, C;
vector<int> G[100001];

bool dfs(int now, int c) {
  color[now] = c;
  for (int i = 0; i < G[now].size(); i++) {
    if (color[G[now][i]] != -1) {
      if (color[G[now][i]] == c)
        return false;
      continue;
    } else {
      if (!dfs(G[now][i], 1 - c))
        return false;
    }
  }
  return true;
}

void dfs2(int now) {
  color[now] = 2;
  for (int i = 0; i < G[now].size(); i++) {
    if (color[G[now][i]] != 2)
      dfs2(G[now][i]);
  }
  return;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 0; i < N; i++)
    color[i] = -1;
  for (int i = 0; i < N; i++) {
    if (!G[i].size())
      A++;
    else {
      if (color[i] != -1)
        continue;
      if (dfs(i, 0))
        B++;
      else {
        C++;
        dfs2(i);
      }
    }
  }
  ll ans = 2 * B * B + A * A + 2 * A * (N - A) + 2 * B * C + C * C;
  cout << ans << endl;
  return 0;
}
Problem p03787 (C++)

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

#define x first
#define y second
typedef pair<int, int> pii;
typedef long long ll;
const int MAXN = 5005;

int n, m;
int co[MAXN];
bool vis[MAXN];
vector<int> E[MAXN];

bool dfs(int u) {
  bool tag = 1;
  vis[u] = 1;
  for (int i = 0; i < (int)E[u].size(); i++) {
    int v = E[u][i];
    if (vis[v])
      tag &= (co[v] != co[u]);
    else {
      co[v] = co[u] ^ 1;
      tag &= dfs(v);
    }
  }
  return tag;
}

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

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    E[u].push_back(v);
    E[v].push_back(u);
  }
  int cnt = 0, p = 0, q = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    if (!E[i].size())
      cnt++;
    else {
      if (dfs(i))
        q++;
      else
        p++;
    }
  }
  printf("%lld\n", (ll)cnt * cnt + 2ll * cnt * (n - cnt) + p * p + 2ll * p * q +
                       2ll * q * q);

  fclose(stdin);
  fclose(stdout);
}


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

#define x first
#define y second
typedef pair<int, int> pii;
typedef long long ll;
const int MAXN = 100005;

int n, m;
int co[MAXN];
bool vis[MAXN];
vector<int> E[MAXN];

bool dfs(int u) {
  bool tag = 1;
  vis[u] = 1;
  for (int i = 0; i < (int)E[u].size(); i++) {
    int v = E[u][i];
    if (vis[v])
      tag &= (co[v] != co[u]);
    else {
      co[v] = co[u] ^ 1;
      tag &= dfs(v);
    }
  }
  return tag;
}

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

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    E[u].push_back(v);
    E[v].push_back(u);
  }
  int cnt = 0, p = 0, q = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    if (!E[i].size())
      cnt++;
    else {
      if (dfs(i))
        q++;
      else
        p++;
    }
  }
  printf("%lld\n", (ll)cnt * cnt + 2ll * cnt * (n - cnt) + p * p + 2ll * p * q +
                       2ll * q * q);

  fclose(stdin);
  fclose(stdout);
}