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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int, int>::iterator
#define sit set<int>::iterator
#define itrm(g, x) for (mit g = x.begin(); g != x.end(); g++)
#define itrs(g, x) for (sit g = x.begin(); g != x.end(); g++)
#define ltype int
#define rep(i, j, k) for (ltype(i) = (j); (i) <= (k); (i)++)
#define rap(i, j, k) for (ltype(i) = (j); (i) < (k); (i)++)
#define per(i, j, k) for (ltype(i) = (j); (i) >= (k); (i)--)
#define pii pair<int, int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
int n, mx;
string s[100005];
map<string, int> m;
vector<string> ans;
int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    cin >> s[i];
    m[s[i]]++;
    mx = max(mx, m[s[i]]);
  }
  rep(i, 1, n) {
    if (mx == m[s[i]]) {
      m[s[i]]--;
      ans.pb(s[i]);
    }
  }
  sort(ans.begin(), ans.end());
  rap(i, 0, ans.size()) { cout << ans[i] << '\n'; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int, int>::iterator
#define sit set<int>::iterator
#define itrm(g, x) for (mit g = x.begin(); g != x.end(); g++)
#define itrs(g, x) for (sit g = x.begin(); g != x.end(); g++)
#define ltype int
#define rep(i, j, k) for (ltype(i) = (j); (i) <= (k); (i)++)
#define rap(i, j, k) for (ltype(i) = (j); (i) < (k); (i)++)
#define per(i, j, k) for (ltype(i) = (j); (i) >= (k); (i)--)
#define pii pair<int, int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
int n, mx;
string s[200005];
map<string, int> m;
vector<string> ans;
int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    cin >> s[i];
    m[s[i]]++;
    mx = max(mx, m[s[i]]);
  }
  rep(i, 1, n) {
    if (mx == m[s[i]]) {
      m[s[i]]--;
      ans.pb(s[i]);
    }
  }
  sort(ans.begin(), ans.end());
  rap(i, 0, ans.size()) { cout << ans[i] << '\n'; }
  return 0;
}
Problem p02773 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

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

  vector<int> b(n);

  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (a.at(i) == a.at(j) && i != j) {
        b.at(i)++;
      }
    }
  }

  int k = 0;

  for (int i = 0; i < n; i++)
    k = max(k, b.at(i));

  for (int i = 0; i < n; i++) {
    if (b.at(i) == k)
      cout << a.at(i) << endl;
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

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

  vector<int> b(n);

  for (int i = n - 1; i > 0; i--) {
    if (a.at(i) == a.at(i - 1)) {
      b.at(i - 1)++;
      b.at(i - 1) += b.at(i);
      a.at(i) = ' ';
    }
  }

  int k = 0;

  for (int i = 0; i < n; i++)
    k = max(k, b.at(i));

  for (int i = 0; i < n; i++) {
    if (b.at(i) == k)
      cout << a.at(i) << endl;
  }
}
Problem p02773 (C++)

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

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  // while(cin>>n){
  cin >> n;
  map<string, int> mp;
  int res(0);
  string s;
  for (int i = 0; i < n; ++i)
    cin >> s, mp[s]++, res = max(res, mp[s]);

  for (auto x : mp)
    if (x.second == res)
      cout << x.first << endl;

  // }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  // while(cin>>n){
  cin >> n;
  map<string, int> mp;
  int res(0);
  string s;
  for (int i = 0; i < n; ++i)
    cin >> s, mp[s]++, res = max(res, mp[s]);

  for (auto x : mp)
    if (x.second == res)
      cout << x.first << endl;

  // }

  return 0;
}
Problem p02773 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())

# 文字列の出現回数を格納
S = {}
for _ in range(N):
    s = input()
    if s in S:
        S[s] += 1
    else:
        S[s] = 1

# 出現回数が最大の文字列をansに格納し、昇順にソートする
ans = [k for k, v in S.items() if v == max(S.values())]
ans.sort()

# 出力
for a in ans:
    print(a)


Fixed submission:
N = int(input())

# 文字列の出現回数を格納
S = {}
for _ in range(N):
    s = input()
    if s in S:
        S[s] += 1
    else:
        S[s] = 1

# 出現回数が最大の文字列をansに格納し、昇順にソートする
# ans = [k for k, v in S.items() if v == max(S.values())]
# ans.sort()
max_value = max(S.values())
ans = sorted([k for k, v in S.items() if v == max_value])

# 出力
for a in ans:
    print(a)
Problem p02773 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define all(v) (v).begin(), (v).end()
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define int long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) (v).begin(), (v).end()
const int mod = 1e9 + 7;
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}

vector<string> ans;
signed main() {
  ios::sync_with_stdio(0);
  vector<string> v;
  int n;
  cin >> n;
  rep(i, 1, n) {
    string s;
    cin >> s;
    v.pb(s);
  }
  sort(all(v));
  int mx = 0;
  for (int i = 0; i < v.size(); i++) {
    int p = i;
    while (p + 1 < v.size() && v[p + 1] == v[p])
      p++;
    int cnt = p - i + 1;
    if (cnt > mx) {
      ans.clear();
      ans.push_back(v[p]);
      mx = cnt;
    } else if (cnt == mx)
      ans.push_back(v[p]);
  }
  for (auto i : ans)
    cout << i << endl;

  getchar();
  getchar();
}

Fixed submission:
#include "bits/stdc++.h"
#define all(v) (v).begin(), (v).end()
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define int long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) (v).begin(), (v).end()
const int mod = 1e9 + 7;
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}

vector<string> ans;
signed main() {
  ios::sync_with_stdio(0);
  vector<string> v;
  int n;
  cin >> n;
  rep(i, 1, n) {
    string s;
    cin >> s;
    v.pb(s);
  }
  sort(all(v));
  int mx = 0;
  for (int i = 0; i < v.size(); i++) {
    int p = i;
    while (p + 1 < v.size() && v[p + 1] == v[p])
      p++;
    int cnt = p - i + 1;
    if (cnt > mx) {
      ans.clear();
      ans.push_back(v[p]);
      mx = cnt;
    } else if (cnt == mx)
      ans.push_back(v[p]);
    i = p;
  }
  for (auto i : ans)
    cout << i << endl;

  getchar();
  getchar();
}
Problem p02773 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  map<string, int> M;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (!M.count(s))
      M.at(s) = 1;
    else
      M.at(s) += 1;
  }
  int Max = 0;

  for (auto p : M) {
    auto k = p.first;
    auto v = p.second;
    if (Max <= v)
      Max = v;
  }
  for (auto p : M) {
    auto k = p.first;
    auto v = p.second;
    if (v == Max)
      cout << k << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  map<string, int> M;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (!M.count(s))
      M[s] = 1;
    else
      M.at(s) += 1;
  }
  int Max = 0;

  for (auto p : M) {
    auto k = p.first;
    auto v = p.second;
    if (Max <= v)
      Max = v;
  }
  for (auto p : M) {
    auto k = p.first;
    auto v = p.second;
    if (v == Max)
      cout << k << endl;
  }

  return 0;
}
Problem p02773 (C++)

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

using namespace std;

string s[100005], an[1000005];
map<string, int> mp;

int main() {
  int n, ans = 0, cnt = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    ++mp[s[i]];
    ans = max(ans, mp[s[i]]);
  }
  for (int i = 1; i <= n; ++i)
    if (mp[s[i]] == ans) {
      --mp[s[i]];
      an[++cnt] = s[i];
    }
  sort(an + 1, an + 1 + cnt);
  for (int i = 1; i <= cnt; ++i)
    cout << an[i] << endl;
  return 0;
}

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

using namespace std;

string s[200005], an[1000005];
map<string, int> mp;

int main() {
  int n, ans = 0, cnt = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    ++mp[s[i]];
    ans = max(ans, mp[s[i]]);
  }
  for (int i = 1; i <= n; ++i)
    if (mp[s[i]] == ans) {
      --mp[s[i]];
      an[++cnt] = s[i];
    }
  sort(an + 1, an + 1 + cnt);
  for (int i = 1; i <= cnt; ++i)
    cout << an[i] << endl;
  return 0;
}
Problem p02773 (C++)

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

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;
int main(void) {
  int N;
  int max = 0;
  string s[100000];
  map<string, int> m;

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

  sort(s, s + N);
  string s1 = s[0];
  if (m[s[0]] == max)
    cout << s1 << endl;
  for (int i = 0; i < N; i++) {
    if (s1 == s[i])
      continue;
    s1 = s[i];
    if (m[s[i]] == max)
      cout << s[i] << endl;
  }
}


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

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;
int main(void) {
  int N;
  int max = 0;
  string s[200002];
  map<string, int> m;

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

  sort(s, s + N);
  string s1 = s[0];
  if (m[s[0]] == max)
    cout << s1 << endl;
  for (int i = 0; i < N; i++) {
    if (s1 == s[i])
      continue;
    s1 = s[i];
    if (m[s[i]] == max)
      cout << s[i] << endl;
  }
}
Problem p02773 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string s[100001], ans[100001];
int cnt;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  sort(s + 1, s + 1 + n);
  int cur = 1, maxx = 0;
  for (int i = 2; i <= n; ++i) {
    if (s[i] != s[i - 1]) {
      if (cur > maxx)
        maxx = cur, cnt = 1, ans[cnt] = s[i - 1];
      else if (cur == maxx)
        ans[++cnt] = s[i - 1];
      cur = 1;
    } else
      ++cur;
  }
  if (cur > maxx)
    maxx = cur, cnt = 1, ans[cnt] = s[n];
  else if (cur == maxx)
    ans[++cnt] = s[n];
  cur = 1;
  for (int i = 1; i <= cnt; ++i)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string s[200001], ans[200001];
int cnt;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  sort(s + 1, s + 1 + n);
  int cur = 1, maxx = 0;
  for (int i = 2; i <= n; ++i) {
    if (s[i] != s[i - 1]) {
      if (cur > maxx)
        maxx = cur, cnt = 1, ans[cnt] = s[i - 1];
      else if (cur == maxx)
        ans[++cnt] = s[i - 1];
      cur = 1;
    } else
      ++cur;
  }
  if (cur > maxx)
    maxx = cur, cnt = 1, ans[cnt] = s[n];
  else if (cur == maxx)
    ans[++cnt] = s[n];
  cur = 1;
  for (int i = 1; i <= cnt; ++i)
    cout << ans[i] << endl;
  return 0;
}
Problem p02773 (C++)

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

using namespace std;

int main(void) {
  int n, maxj = 1;
  cin >> n;
  string s;
  map<string, int> mapj;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (mapj.count(s)) {
      mapj.at(s)++;
      maxj = max(maxj, mapj.at(s));
    } else {
      mapj.insert(make_pair(s, 1));
    }
  }
  for (pair<string, int> i : mapj) {
    if (i.second == maxj) {
      cout << i.first << endl;
    }
  }
  return 0;
}

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

using namespace std;

int main(void) {
  int n, maxj = 1;
  cin >> n;
  string s;
  map<string, int> mapj;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (mapj.count(s)) {
      mapj.at(s)++;
      maxj = max(maxj, mapj.at(s));
    } else {
      mapj.insert(make_pair(s, 1));
    }
  }
  for (map<string, int>::iterator i = mapj.begin(); i != mapj.end(); i++) {
    if (i->second == maxj) {
      cout << i->first << endl;
    }
  }
  return 0;
}
Problem p02773 (C++)

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

string s[100005];
map<string, int> mp;
vector<string> v;

int main() {
  ios::sync_with_stdio(false);
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    mp[s[i]]++;
    ans = max(ans, mp[s[i]]);
  }
  for (int i = 1; i <= n; i++) {
    if (mp[s[i]] == ans) {
      // cout<<s[i]<<'\n';
      v.push_back(s[i]);
      mp[s[i]] = 0;
    }
  }
  sort(v.begin(), v.end());
  for (auto s : v) {
    cout << s << '\n';
  }
}


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

string s[200005];
map<string, int> mp;
vector<string> v;

int main() {
  ios::sync_with_stdio(false);
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    mp[s[i]]++;
    ans = max(ans, mp[s[i]]);
  }
  for (int i = 1; i <= n; i++) {
    if (mp[s[i]] == ans) {
      // cout<<s[i]<<'\n';
      v.push_back(s[i]);
      mp[s[i]] = 0;
    }
  }
  sort(v.begin(), v.end());
  for (auto s : v) {
    cout << s << '\n';
  }
}
Problem p02773 (C++)

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

using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<string> S;
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    S.push_back(s);
  }
  sort(S.begin(), S.end());
  int count = 1;
  int count_max = 1;
  deque<string> ans;
  ans.push_back(S[0]);
  for (int i = 1; i <= N; ++i) {
    if (S[i] == ans.back() && i != N) {
      ++count;
    } else {
      if (count < count_max) {
        ans.pop_back();
      } else if (count > count_max) {
        ans.clear();
        ans.push_back(S[i - 1]);
        count_max = count;
      }
      if (i == N)
        break;
      count = 1;
      ans.push_back(S[i]);
    }
  }
  for_each(ans.begin(), ans.end(), [](string s) { cout << s << '\n'; });
  return 0;
}


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

using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<string> S;
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    S.push_back(s);
  }
  sort(S.begin(), S.end());
  int count = 1;
  int count_max = 1;
  deque<string> ans;
  ans.push_back(S[0]);
  for (int i = 1; i <= N; ++i) {
    if (i != N && S[i] == ans.back()) {
      ++count;
    } else {
      if (count < count_max) {
        ans.pop_back();
      } else if (count > count_max) {
        ans.clear();
        ans.push_back(S[i - 1]);
        count_max = count;
      }
      if (i == N)
        break;
      count = 1;
      ans.push_back(S[i]);
    }
  }
  for_each(ans.begin(), ans.end(), [](string s) { cout << s << '\n'; });
  return 0;
}
Problem p02773 (C++)

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

using namespace std;
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a) * (b) / GCD(a, b))
#define PI (3.141592653589793238462643383279502884197169399375105820974944)
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define VLL vector<ll>
#define VS vector<string>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const int N = 150555;
const int M = 1155;

VS v[N];
void solve() {
  int n;
  cin >> n;

  map<string, int> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    m[s]++;
  }

  for (auto now : m) {
    string x = now.n1;
    int y = now.n2;
    v[y].PB(x);
  }

  for (int i = n; i >= 1; i--) {
    if (v[i].empty())
      continue;
    sort(v[i].begin(), v[i].end());
    for (auto now : v[i]) {
      cout << now << endl;
    }
    return;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
#ifdef dxz05
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else

#endif

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}

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

using namespace std;
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a) * (b) / GCD(a, b))
#define PI (3.141592653589793238462643383279502884197169399375105820974944)
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define VLL vector<ll>
#define VS vector<string>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const int N = 250555;
const int M = 1155;

VS v[N];
void solve() {
  int n;
  cin >> n;

  map<string, int> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    m[s]++;
  }

  for (auto now : m) {
    string x = now.n1;
    int y = now.n2;
    v[y].PB(x);
  }

  for (int i = n; i >= 1; i--) {
    if (v[i].empty())
      continue;
    sort(v[i].begin(), v[i].end());
    for (auto now : v[i]) {
      cout << now << endl;
    }
    return;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
#ifdef dxz05
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else

#endif

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}
Problem p02773 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void print(const vector<int> &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> s;
  map<string, int> mp;
  rep(i, n) {
    string a;
    cin >> a;
    s.push_back(a);
  }
  rep(i, n) mp[s[i]] = 0;
  rep(i, n) mp[s[i]]++;
  int k = 0;
  for (auto m : mp) {
    k = max(k, m.second);
  }
  sort(all(s));
  int p, q;
  p = q = 0;
  while (q < n) {
    while (s[p] == s[q] && q < n)
      q++;
    if (k == (q - p)) {
      out(s[p]);
    }
    p = q;
  }
  return 0;
}


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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void print(const vector<int> &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> s;
  map<string, int> mp;
  rep(i, n) {
    string a;
    cin >> a;
    s.push_back(a);
  }
  rep(i, n) mp[s[i]] = 0;
  rep(i, n) mp[s[i]]++;
  int k = 0;
  for (auto m : mp) {
    k = max(k, m.second);
  }
  sort(all(s));
  int p, q;
  p = q = 0;
  while (q < n) {
    while (q < n && s[p] == s[q])
      q++;
    if (k == (q - p)) {
      out(s[p]);
    }
    p = q;
  }
  return 0;
}
Problem p02773 (C++)

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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repto(i, n) for (int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  int N;
  cin >> N;
  vector<string> S(N);

  rep(i, N) cin >> S[i];
  sort(all(S));
  vector<int> s;
  int sum = 0;
  // rep(i, N)
  //   cout << S[i] << ' ';
  // cout << endl;
  int cnt = 0;
  rep(i, N) {
    cnt++;
    if (S[i] != S[i + 1] || i == N - 1) {
      sum = max(cnt, sum);
      cnt = 0;
      s.pb(i);
    }
  }
  // rep(i, s.size())
  //   cout << s[i] << ' ';
  // cout << endl;
  rep(i, s.size()) {
    if (i == 0) {
      if (s[i] + 1 == sum)
        cout << S[s[i]] << endl;
    } else {
      if (s[i] - s[i - 1] == sum) {
        cout << S[s[i]] << endl;
      }
    }
  }
}


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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repto(i, n) for (int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  int N;
  cin >> N;
  vector<string> S(N);

  rep(i, N) cin >> S[i];
  sort(all(S));
  vector<int> s;
  int sum = 0;
  // rep(i, N)
  //   cout << S[i] << ' ';
  // cout << endl;
  int cnt = 0;
  rep(i, N) {
    cnt++;
    if ((i != N - 1 && S[i] != S[i + 1]) || i == N - 1) {
      sum = max(cnt, sum);
      cnt = 0;
      s.pb(i);
    }
  }
  // rep(i, s.size())
  //   cout << s[i] << ' ';
  // cout << endl;
  rep(i, s.size()) {
    if (i == 0) {
      if (s[i] + 1 == sum)
        cout << S[s[i]] << endl;
    } else {
      if (s[i] - s[i - 1] == sum) {
        cout << S[s[i]] << endl;
      }
    }
  }
}
Problem p02773 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define dbg cout << "Freak" << endl;
#define dbgg(i) cout << i << endl;
#define N 1e9 + 7
#define pb push_back
#define pi acos(-1)
using namespace std;
ll arr[100005];
ll t;
vector<string> v, v1;
map<string, ll> mp;
map<string, string> np;
int main() {
  ll a, b, c, d, e, f = 0, i, j;
  cin >> a;
  string s;
  for (i = 0; i < a; i++) {
    cin >> s;
    mp[s]++;
    if (mp[s] == 1)
      v.pb(s);
  }
  for (i = 0; i < v.size(); i++) {
    f = max(f, mp[v[i]]);
  }
  for (i = 0; i < v.size(); i++) {
    if (mp[v[i]] != f)
      mp[v[i]] = 0;
    else
      v1.pb(v[i]);
  }
  sort(v1.begin(), v1.end());
  for (i = 0; i < v.size(); i++)
    cout << v1[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define dbg cout << "Freak" << endl;
#define dbgg(i) cout << i << endl;
#define N 1e9 + 7
#define pb push_back
#define pi acos(-1)
using namespace std;
ll arr[100005];
ll t;
vector<string> v, v1;
map<string, ll> mp;
map<string, string> np;
int main() {
  ll a, b, c, d, e, f = 0, i, j;
  cin >> a;
  string s;
  for (i = 0; i < a; i++) {
    cin >> s;
    mp[s]++;
    if (mp[s] == 1)
      v.pb(s);
  }
  for (i = 0; i < v.size(); i++) {
    f = max(f, mp[v[i]]);
  }
  for (i = 0; i < v.size(); i++) {
    if (mp[v[i]] != f)
      mp[v[i]] = 0;
    else
      v1.pb(v[i]);
  }
  sort(v1.begin(), v1.end());
  for (i = 0; i < v1.size(); i++)
    cout << v1[i] << endl;
}
Problem p02773 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Counter


def main():
    N = int(input())
    S = [input() for _ in range(N)]
    S = Counter(S)

    result = []
    for key, value in S.items():
        if value == S.most_common()[0][1]:
            result.append(key)
    result.sort()
    for r in result:
        print(r)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Counter


def main():
    N = int(input())
    S = [input() for _ in range(N)]
    S = Counter(S)

    SM = S.most_common()[0][1]
    S = sorted(S.items(), key=lambda x: [-x[1], x[0]])
    for key, value in S:
        if value == SM:
            print(key)
        else:
            break


if __name__ == "__main__":
    main()
Problem p02773 (Python)

Buggy submission (status: Runtime Error):
def main():
    n = int(input())
    words = {}
    for _ in range(n):
        a = input()
        if a in words:
            words[a] += 1
        else:
            words[a] = 1

    max_val = max(words.values())
    # words_sorted = sorted(words.items(), key=lambda x: x[0])
    for key, value in sorted(words):
        if value != max_val:
            continue
        else:
            print(key)
            # key valueを両方printするとき
            # print('{}:{}'.format(key, value))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    words = {}
    for _ in range(n):
        a = input()
        if a in words:
            words[a] += 1
        else:
            words[a] = 1

    max_val = max(words.values())
    # words_sorted = sorted(words.items(), key=lambda x: x[0])
    for key, value in sorted(words.items()):
        if value != max_val:
            continue
        else:
            print(key)
            # key valueを両方printするとき
            # print('{}:{}'.format(key, value))


if __name__ == "__main__":
    main()
Problem p02773 (Python)

Buggy submission (status: Time Limit Exceeded):
import collections
import sys

input = sys.stdin.readline

q = int(input())
a = [input().rstrip() for _ in range(q)]

dic = collections.Counter(a)

keys = [k for k, v in dic.items() if v == max(dic.values())]
keys_s = sorted(keys)
for i in keys_s:
    print(i)


Fixed submission:
import collections
import sys

input = sys.stdin.readline

q = int(input())
a = [input().rstrip() for _ in range(q)]

dic = collections.Counter(a)

ma = max(dic.values())
keys = [k for k, v in dic.items() if v == ma]
keys_s = sorted(keys)
for i in keys_s:
    print(i)
Problem p02773 (C++)

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

using namespace std;

int main() {

  long int n;
  scanf("%ld", &n);
  string s[n];
  long int i, max = 0, cnt = 0;

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

  sort(s, s + n);

  for (i = 0; i < n - 1; ++i) {
    if (s[i] == s[i + 1])
      cnt++;

    else {
      if (max < cnt)
        max = cnt;

      cnt = 0;
    }
  }
  if (max < cnt)
    max = cnt;

  cnt = 0;
  for (i = 0; i < n; ++i) {
    if (s[i] == s[i + 1])
      cnt++;

    else {
      if (max == cnt)
        cout << s[i] << endl;

      cnt = 0;
    }
  }

  return 0;
}

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

using namespace std;

int main() {

  long int n;
  scanf("%ld", &n);
  string s[n];
  long int i, max = 0, cnt = 0;

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

  sort(s, s + n);

  for (i = 0; i < n - 1; ++i) {
    if (s[i] == s[i + 1])
      cnt++;

    else {
      if (max < cnt)
        max = cnt;

      cnt = 0;
    }
  }
  if (max < cnt)
    max = cnt;

  cnt = 0;
  for (i = 0; i < n; ++i) {
    if (i != n - 1 && s[i] == s[i + 1])
      cnt++;

    else {
      if (max == cnt)
        cout << s[i] << endl;

      cnt = 0;
    }
  }

  return 0;
}
Problem p02773 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#define mem(i, a) memset(i, a, sizeof(i))
#define PI acos(-1.0)
#define DMAX 100000 + 5
#define MOD 20190823
#define eps 1e-8
#define mp make_pair
#define INF 1000000000 + 9
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
inline void read(int &f) {
  f = 0;
  ll x = 1;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-') {
      x = -1;
    }
    ch = getchar();
  }
  while (ch <= 57 && ch >= 48) {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  f *= x;
}
int n;
string s[DMAX];
map<string, int> ma;
int ans = 0;
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    ma[s[i]]++;
    ans = max(ans, ma[s[i]]);
  }
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == s[i - 1]) {
      continue;
    } else {
      if (ma[s[i]] == ans) {
        cout << s[i] << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#define mem(i, a) memset(i, a, sizeof(i))
#define PI acos(-1.0)
#define DMAX 200000 + 5
#define MOD 20190823
#define eps 1e-8
#define mp make_pair
#define INF 1000000000 + 9
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
inline void read(int &f) {
  f = 0;
  ll x = 1;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-') {
      x = -1;
    }
    ch = getchar();
  }
  while (ch <= 57 && ch >= 48) {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  f *= x;
}
int n;
string s[DMAX];
map<string, int> ma;
int ans = 0;
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    ma[s[i]]++;
    ans = max(ans, ma[s[i]]);
  }
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == s[i - 1]) {
      continue;
    } else {
      if (ma[s[i]] == ans) {
        cout << s[i] << endl;
      }
    }
  }
  return 0;
}
Problem p02774 (C++)

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

long long check(long long x) {
  long long ret = 0, n = a.size();
  for (int i = 0; i < n; ++i) {
    if (a[i] == 0) {
      ret += n - 1;
    } else if (a[i] < 0) {

      int l = 0, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[i] * a[mid] > x)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ret += n - l;
      if (a[i] * a[i] <= x)
        ret--;
    } else {
      int l = 0, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[i] * a[mid] <= x)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ret += l;
      if (a[i] * a[i] <= x)
        ret--;
    }
  }
  assert(ret % 2 == 0);
  return ret / 2;
}

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

  long long n, k;
  cin >> n >> k;

  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  sort(a.begin(), a.end());
  long long l = -1e18, r = 1e18;

  while (l < r) {
    long long mid = (l + r) >> 1;
    if (check(mid) >= k)
      r = mid;
    else
      l = mid + 1;
  }

  cout << r << '\n';
}

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

long long check(long long x) {
  long long ret = 0, n = a.size();
  for (int i = 0; i < n; ++i) {
    if (a[i] == 0) {
      if (0 <= x)
        ret += n - 1;
    } else if (a[i] < 0) {

      int l = 0, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[i] * a[mid] > x)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ret += n - l;
      if (a[i] * a[i] <= x)
        ret--;
    } else {
      int l = 0, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[i] * a[mid] <= x)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ret += l;
      if (a[i] * a[i] <= x)
        ret--;
    }
  }
  assert(ret % 2 == 0);
  return ret / 2;
}

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

  long long n, k;
  cin >> n >> k;

  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  sort(a.begin(), a.end());
  long long l = -1e18, r = 1e18;

  while (l < r) {
    long long mid = (l + r) >> 1;
    if (check(mid) >= k)
      r = mid;
    else
      l = mid + 1;
  }

  cout << r << '\n';
}
Problem p02774 (C++)

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

using namespace std;

const int M = 20010;
const long long M2 = 1e18;
long long n, k, l, r, mid, ll, rr, mmid, cnt, i, a[M];

int main() {

  cin >> n >> k;

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

  sort(a, a + n);

  l = -M2;
  r = M2;

  while (l + 1 < r) {
    mid = l + r >> 1;
    cnt = 0;
    for (i = 0; i < n; i++) {

      if (a[i] < 0) {

        ll = i;
        rr = n;

        while (ll + 1 < rr) {

          mmid = ll + rr >> 1;
          if (a[i] * a[mmid] <= mid)
            rr = mmid;
          else
            ll = mmid;
        }

        cnt += n - rr;
      } else {
        ll = i;
        rr = n;

        while (ll + 1 < rr) {

          mmid = ll + rr >> 1;
          if (a[i] * a[mmid] <= mid)
            ll = mmid;
          else
            rr = mmid;
        }

        cnt += ll - i;
      }
    }
    if (cnt < k)
      l = mid;
    else
      r = mid;
  }

  cout << r;

  return 0;
}

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

using namespace std;

const long long M = 2e5 + 10;
const long long M2 = 1e18;
long long n, k, l, r, mid, ll, rr, mmid, cnt, i, a[M];

int main() {

  cin >> n >> k;

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

  sort(a, a + n);

  l = -M2;
  r = M2;

  while (l + 1 < r) {
    mid = l + r >> 1;
    cnt = 0;
    for (i = 0; i < n; i++) {

      if (a[i] < 0) {

        ll = i;
        rr = n;

        while (ll + 1 < rr) {

          mmid = ll + rr >> 1;
          if (a[i] * a[mmid] <= mid)
            rr = mmid;
          else
            ll = mmid;
        }

        cnt += n - rr;
      } else {
        ll = i;
        rr = n;

        while (ll + 1 < rr) {

          mmid = ll + rr >> 1;
          if (a[i] * a[mmid] <= mid)
            ll = mmid;
          else
            rr = mmid;
        }

        cnt += ll - i;
      }
    }
    if (cnt < k)
      l = mid;
    else
      r = mid;
  }

  cout << r;

  return 0;
}
Problem p02774 (C++)

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

const ll INF = ll(1e18) + 1;

ll binary_search(ll x, ll ai, vector<ll> a) {
  int n = a.size();
  int l = -1;
  int r = n;
  if (ai < 0) {
    while (l + 1 < r) {
      int c = (l + r) / 2;
      if (ai * a[c] < x)
        r = c;
      else
        l = c;
    }
    return n - r;
  } else {
    while (l + 1 < r) {
      int c = (l + r) / 2;
      if (ai * a[c] < x)
        l = c;
      else
        r = c;
    }
    return r;
  }
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  sort(a.begin(), a.end());
  ll l = -INF, r = INF;
  while (l + 1 < r) {
    ll x = (l + r) / 2;
    bool ok = [&] {
      ll tot = 0;
      rep(i, n) {
        tot += binary_search(x, a[i], a);
        if (a[i] * a[i] < x)
          tot--;
      }
      tot /= 2;
      return tot < k;
    }();
    if (ok)
      l = x;
    else
      r = x;
  }
  cout << l << endl;
  return 0;
}

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

const ll INF = ll(1e18) + 1;

ll binary_search(ll x, ll ai, vector<ll> a) {
  int n = a.size();
  int l = -1;
  int r = n;
  if (ai < 0) {
    while (l + 1 < r) {
      int c = (l + r) / 2;
      if (ai * a[c] < x)
        r = c;
      else
        l = c;
    }
    return n - r;
  } else {
    while (l + 1 < r) {
      int c = (l + r) / 2;
      if (ai * a[c] < x)
        l = c;
      else
        r = c;
    }
    return r;
  }
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  sort(a.begin(), a.end());
  ll l = -INF, r = INF;
  while (l + 1 < r) {
    ll x = (l + r) / 2;
    bool ok = [&] {
      ll tot = 0;
      rep(i, n) {
        if (a[i] < 0) {
          int l = -1, r = n;
          while (l + 1 < r) {
            int c = (l + r) / 2;
            if (a[c] * a[i] < x)
              r = c;
            else
              l = c;
          }
          tot += n - r;
        } else {
          int l = -1, r = n;
          while (l + 1 < r) {
            int c = (l + r) / 2;
            if (a[c] * a[i] < x)
              l = c;
            else
              r = c;
          }
          tot += r;
        }
        if (a[i] * a[i] < x)
          tot--;
      }
      tot /= 2;
      return tot < k;
    }();
    if (ok)
      l = x;
    else
      r = x;
  }
  cout << l << endl;
  return 0;
}
Problem p02774 (C++)

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

using namespace std;
using ll = long long;

const int N = 200005;

int n;
ll k;

int a[N];

ll f(ll lim) {

  ll ans = 0;
  int neg_ptr = 0, pos_ptr = n;

  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      if (lim >= 0)
        ans += n - 1;
    } else if (a[i] > 0) {
      while (pos_ptr - 1 >= 0 and 1LL * a[i] * a[pos_ptr - 1] > lim) {
        pos_ptr--;
      }
      if (i < pos_ptr)
        ans += pos_ptr - 1;
      else
        ans += pos_ptr;
    } else { // a[i] < 0
      while (neg_ptr < n and 1LL * a[i] * a[neg_ptr] > lim) {
        neg_ptr++;
      }
      while (neg_ptr - 1 >= 0 and 1LL * a[i] * a[neg_ptr - 1] <= lim)
        neg_ptr--;
      if (i >= neg_ptr)
        ans += n - neg_ptr - 1;
      else
        ans += n - neg_ptr;
    }
  }
  assert(ans % 2 == 0);
  return ans / 2;
}

int main() {
  scanf("%d %lld", &n, &k);

  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  sort(a, a + n);

  ll lo = -1e18, hi = 1e18, ans = 0;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (f(mid) >= k)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  printf("%lld\n", ans);
}


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

using namespace std;
using ll = long long;

const int N = 200005;

int n;
ll k;

int a[N];

ll f(ll lim) {

  ll ans = 0;
  int neg_ptr = 0, pos_ptr = n;

  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      if (lim >= 0)
        ans += n - 1;
    } else if (a[i] > 0) {
      while (pos_ptr - 1 >= 0 and 1LL * a[i] * a[pos_ptr - 1] > lim) {
        pos_ptr--;
      }
      while (pos_ptr < n and 1LL * a[i] * a[pos_ptr] <= lim)
        pos_ptr++;
      if (i < pos_ptr)
        ans += pos_ptr - 1;
      else
        ans += pos_ptr;
    } else { // a[i] < 0
      while (neg_ptr < n and 1LL * a[i] * a[neg_ptr] > lim) {
        neg_ptr++;
      }
      while (neg_ptr - 1 >= 0 and 1LL * a[i] * a[neg_ptr - 1] <= lim)
        neg_ptr--;
      if (i >= neg_ptr)
        ans += n - neg_ptr - 1;
      else
        ans += n - neg_ptr;
    }
  }
  assert(ans % 2 == 0);
  return ans / 2;
}

int main() {
  scanf("%d %lld", &n, &k);

  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  sort(a, a + n);

  ll lo = -1e18, hi = 1e18, ans = 0;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (f(mid) >= k)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  printf("%lld\n", ans);
}
Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i)

const int MAX_N = 200;
int N;
ll K;
ll A[MAX_N];
const ll INF = (ll)1e18 + 1LL;

bool cmp(ll a, ll b, ll X) { return a * b < X; }

int b_search(ll key, ll X, bool right_true, bool (*isOK)(ll, ll, ll)) {
  int ng = right_true ? N : -1;
  int ok = right_true ? -1 : N;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isOK(key, A[mid], X))
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

ll order_ascending(ll X) { // 積がX未満になるのが何個あるか
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    if (A[i] >= 0) {
      int candidate = b_search(A[i], X, true, cmp);
      if (i <= candidate)
        --candidate;
      res += (ll)candidate + 1;
    } else {
      int candidate = b_search(A[i], X, false, cmp);
      if (candidate <= i)
        ++candidate;
      res += (ll)N - candidate;
    }
  }
  return res / 2LL;
}

int main() {
  scanf("%d %lld", &N, &K);
  for (int i = 0; i < N; ++i)
    scanf("%lld", &A[i]);
  sort(A, A + N);

  ll low = -INF, high = INF;
  while (abs(high - low) > 1) {
    ll mid = (low + high) / 2;
    if (order_ascending(mid) < K)
      low = mid;
    else
      high = mid;
  }
  printf("%lld\n", low);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i)

const int MAX_N = 200100;
int N;
ll K;
ll A[MAX_N];
const ll INF = (ll)1e18 + 1LL;

bool cmp(ll a, ll b, ll X) { return a * b < X; }

int b_search(ll key, ll X, bool right_true, bool (*isOK)(ll, ll, ll)) {
  int ng = right_true ? N : -1;
  int ok = right_true ? -1 : N;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isOK(key, A[mid], X))
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

ll order_ascending(ll X) { // 積がX未満になるのが何個あるか
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    if (A[i] >= 0) {
      int candidate = b_search(A[i], X, true, cmp);
      if (i <= candidate)
        --candidate;
      res += (ll)candidate + 1;
    } else {
      int candidate = b_search(A[i], X, false, cmp);
      if (candidate <= i)
        ++candidate;
      res += (ll)N - candidate;
    }
  }
  return res / 2LL;
}

int main() {
  scanf("%d %lld", &N, &K);
  for (int i = 0; i < N; ++i)
    scanf("%lld", &A[i]);
  sort(A, A + N);

  ll low = -INF, high = INF;
  while (abs(high - low) > 1) {
    ll mid = (low + high) / 2;
    if (order_ascending(mid) < K)
      low = mid;
    else
      high = mid;
  }
  printf("%lld\n", low);
  return 0;
}
Problem p02774 (C++)

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

ll n, k;

int64_t intceil(int64_t a, int64_t b) {
  if (a >= 0) {
    return (a + b - 1) / b;
  } else {
    return a / b;
  }
}
int64_t intfloor(int64_t a, int64_t b) {
  if (a < 0) {
    return (a - b + 1) / b;
  } else {
    return a / b;
  }
}

int main() {
  cin >> n >> k;
  vector<int> a(n);
  vector<ll> b(n);
  for (int i = 0; i < n; i++) {
    ll z;
    cin >> z;
    a[i] = z;
    b[i] = z * z;
  }
  sort(a.begin(), a.end());
  ll lb = -1e18 - 1, ub = 1e18;
  int i = 0;
  while (lb + 1 < ub) {
    bool fl = lb < ub;
    ll mid = (ub + lb) / 2;
    ll cnt = 0; // mid以下の個数
    for (int i = 0; i < n; i++) {
      if (a[i] < 0) {
        cnt += a.begin() + i -
               lower_bound(a.begin(), a.begin() + i, intceil(-mid, -a[i]));
      } else if (a[i] > 0) {
        // cnt+=upper_bound(a.begin(),a.end(),intfloor(mid,a[i]))-a.begin();
        cnt += upper_bound(a.begin(), a.begin() + i, intfloor(mid, a[i])) -
               a.begin();
      } else {
        if (mid >= 0) {
          cnt += i;
        }
      }
      // if (b[i]<=mid) cnt--;
    }
    // cnt/=2;
    if (cnt >= k) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << ub << endl;
}

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

ll n, k;

int64_t intceil(int64_t a, int64_t b) {
  if (a >= 0) {
    return (a + b - 1) / b;
  } else {
    return a / b;
  }
}
int64_t intfloor(int64_t a, int64_t b) {
  if (a < 0) {
    return (a - b + 1) / b;
  } else {
    return a / b;
  }
}

int main() {
  cin >> n >> k;
  vector<int> a(n);
  vector<ll> b(n);
  for (int i = 0; i < n; i++) {
    ll z;
    cin >> z;
    a[i] = z;
    b[i] = z * z;
  }
  sort(a.begin(), a.end());
  ll lb = -1e18 - 1, ub = 1e18;
  int i = 0;
  while (lb + 1 < ub) {
    bool fl = lb < ub;
    ll mid = (ub + lb) / 2;
    ll cnt = 0; // mid以下の個数
    for (int i = 0; i < n; i++) {
      if (a[i] < 0) {
        cnt += a.begin() + i -
               lower_bound(a.begin(), a.begin() + i, intceil(-mid, -a[i]));
      } else if (a[i] > 0) {
        // cnt+=upper_bound(a.begin(),a.end(),intfloor(mid,a[i]))-a.begin();
        cnt += upper_bound(a.begin(), a.begin() + i, intfloor(mid, a[i])) -
               a.begin();
      } else {
        if (mid >= 0) {
          cnt += i;
        }
      }
      // if (b[i]<=mid) cnt--;
    }
    // cnt/=2;
    if (cnt >= k) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << ub << endl;
}