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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e9;
const static ll MOD = 1e9 + 7;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll Max = A[N - 1];
  ll r = 0;
  ll id;
  for (int i = 0; i < N - 1; i++) {
    if (r < min(A[i], Max - A[i])) {
      r = min(A[i], Max - A[i]);
      id = i;
    }
  }
  cout << Max << " " << A[id] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e9;
const static ll MOD = 1e9 + 7;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll Max = A[N - 1];
  ll r = -1;
  ll id;
  for (int i = 0; i < N - 1; i++) {
    if (r < min(A[i], Max - A[i])) {
      r = min(A[i], Max - A[i]);
      id = i;
    }
  }
  cout << Max << " " << A[id] << endl;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  sort(A.begin(), A.end());
  int maxv = 0, maxi;
  for (int i = 0; i < n - 1; i++) {
    if (min(A[i], A[n - 1] - A[i]) > maxv) {
      maxv = min(A[i], A[n - 1] - A[i]);
      maxi = i;
    }
  }
  printf("%d %d\n", A[n - 1], A[maxi]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  sort(A.begin(), A.end());
  int maxv = 0, maxi = 0;
  for (int i = 0; i < n - 1; i++) {
    if (min(A[i], A[n - 1] - A[i]) > maxv) {
      maxv = min(A[i], A[n - 1] - A[i]);
      maxi = i;
    }
  }
  printf("%d %d\n", A[n - 1], A[maxi]);
  return 0;
}
Problem p03380 (C++)

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

int main() {
  int n, i, A[11111];

  std::cin >> n;

  for (i = 0; i < n; i++) {
    std::cin >> A[i];
  }

  sort(A, A + n);

  int ai = A[n - 1];
  int aj = A[0];

  for (i = 0; i < n - 1; i++) {
    if (abs(A[i] - ai / 2) <= abs(aj - ai / 2))
      aj = A[i];
  }

  std::cout << ai << " " << aj;

  return 0;
}


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

int main() {
  int n, i, A[111111];

  std::cin >> n;

  for (i = 0; i < n; i++) {
    std::cin >> A[i];
  }

  sort(A, A + n);

  int ai = A[n - 1];
  int aj = A[0];

  for (i = 0; i < n - 1; i++) {
    if (abs(A[i] - ai / 2) <= abs(aj - ai / 2))
      aj = A[i];
  }

  std::cout << ai << " " << aj;

  return 0;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = 1000000000 + 7;
const ll INF = LLONG_MAX;
const ll MAX_V = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int n, a[10005];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int amax = a[n - 1], dmin = amax + 1, half;
  for (int i = 0; i < n; i++) {
    if (amax % 2 == 0 && abs(a[i] - amax / 2) < dmin) {
      dmin = abs(a[i] - amax / 2);
      half = a[i];
    } else if (amax % 2 == 1 && (abs(a[i] - amax / 2) < dmin ||
                                 abs(a[i] - 1 - amax / 2) < dmin)) {
      dmin = min(abs(a[i] - amax / 2), abs(a[i] - 1 - amax / 2));
      half = a[i];
    }
  }
  cout << amax << " " << half << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = 1000000000 + 7;
const ll INF = LLONG_MAX;
const ll MAX_V = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int n, a[100005];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int amax = a[n - 1], dmin = amax + 1, half;
  for (int i = 0; i < n; i++) {
    if (amax % 2 == 0 && abs(a[i] - amax / 2) < dmin) {
      dmin = abs(a[i] - amax / 2);
      half = a[i];
    } else if (amax % 2 == 1 && (abs(a[i] - amax / 2) < dmin ||
                                 abs(a[i] - 1 - amax / 2) < dmin)) {
      dmin = min(abs(a[i] - amax / 2), abs(a[i] - 1 - amax / 2));
      half = a[i];
    }
  }
  cout << amax << " " << half << endl;
  return 0;
}
Problem p03380 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  double e = a[n - 1];
  double q = e / 2;
  double m = 100000000;
  int b;
  for (int i = 0; i < n; i++) {
    if (m > abs(a[i] - q)) {
      m = abs(a[i] - q);
      b = i;
    }
  }
  cout << a[n - 1] << " " << a[b] << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  double e = a[n - 1];
  double q = e / 2;
  double m = 2000000000;
  int b;
  for (int i = 0; i < n; i++) {
    if (m > abs(a[i] - q)) {
      m = abs(a[i] - q);
      b = i;
    }
  }
  cout << a[n - 1] << " " << a[b] << endl;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAX_N 10005
using namespace std;
/*
 * min(r, n-r)の最大値を求めればいい
 * このとき, nはaの最大値
 */
int n;
int a[MAX_N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);
  int left = a[n - 1], right = min(a[0], left - a[0]);
  for (int i = 0; i < n - 1; i++) {
    int tmp = min(a[i], left - a[i]);
    if (tmp > right)
      right = a[i];
  }
  cout << left << " " << right << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAX_N 100005
using namespace std;
/*
 * min(r, n-r)の最大値を求めればいい
 * このとき, nはaの最大値
 */
int n;
int a[MAX_N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);
  int left = a[n - 1], right = min(a[0], left - a[0]);
  for (int i = 0; i < n - 1; i++) {
    int tmp = min(a[i], left - a[i]);
    if (tmp > right)
      right = a[i];
  }
  cout << left << " " << right << endl;
}
Problem p03380 (C++)

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

int main() {
  int n;
  cin >> n;
  int a[10000];
  int MAX = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    MAX = max(MAX, a[i]);
  }
  double mid = MAX / 2;
  double MIN = 1e9;
  int b;
  for (int i = 0; i < n; i++) {
    if (a[i] != MAX && MIN > abs(mid - a[i])) {
      MIN = abs(mid - a[i]);
      b = a[i];
    }
  }
  cout << MAX << " " << b;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  int a[100000];
  int MAX = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    MAX = max(MAX, a[i]);
  }
  double mid = MAX / 2;
  double MIN = 1e9;
  int b;
  for (int i = 0; i < n; i++) {
    if (a[i] != MAX && MIN > abs(mid - a[i])) {
      MIN = abs(mid - a[i]);
      b = a[i];
    }
  }
  cout << MAX << " " << b;
  return 0;
}
Problem p03380 (C++)

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

using namespace std;

uintmax_t combination(unsigned int n, unsigned int r) {
  if (r * 2 > n)
    r = n - r;
  uintmax_t dividend = 1;
  uintmax_t divisor = 1;
  for (unsigned int i = 1; i <= r; ++i) {
    dividend *= (n - i + 1);
    divisor *= i;
  }
  return dividend / divisor;
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n, -1);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  int an = *max_element(x.begin(), x.end());
  int mi = distance(x.begin(), max_element(x.begin(), x.end()));
  x[mi] = *min_element(x.begin(), x.end());
  int med[2];
  if (an % 2 == 0) {
    med[0] = an / 2;
    med[1] = an / 2;
  } else {
    med[0] = (an - 1) / 2;
    med[1] = (an + 1) / 2;
  }
  int diff = 1000000000, ans;
  for (int i = 0; i < n; i++) {
    if (diff > min(abs(x[i] - med[0]), abs(x[i] - med[1]))) {
      ans = x[i];
      diff = min(abs(x[i] - med[0]), abs(x[i] - med[1]));
    }
  }

  int aan = *max_element(x.begin(), x.end());
  if (aan % 2 == 0) {
    med[0] = aan / 2;
    med[1] = aan / 2;
  } else {
    med[0] = (aan - 1) / 2;
    med[1] = (aan + 1) / 2;
  }
  int ddiff = 1000000000, aans;
  for (int i = 0; i < n; i++) {
    if (ddiff > min(abs(x[i] - med[0]), abs(x[i] - med[1]))) {
      aans = x[i];
      ddiff = min(abs(x[i] - med[0]), abs(x[i] - med[1]));
    }
  }
  if (combination(an, ans) < combination(aan, aans)) {
    an = aan;
    ans = aans;
  }
  cout << an << " " << ans << endl;
  return 0;
}

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

using namespace std;

uintmax_t combination(unsigned int n, unsigned int r) {
  if (r * 2 > n)
    r = n - r;
  uintmax_t dividend = 1;
  uintmax_t divisor = 1;
  for (unsigned int i = 1; i <= r; ++i) {
    dividend *= (n - i + 1);
    divisor *= i;
  }
  return dividend / divisor;
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n, -1);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  int an = *max_element(x.begin(), x.end());
  int mi = distance(x.begin(), max_element(x.begin(), x.end()));
  x[mi] = *min_element(x.begin(), x.end());
  int med[2];
  if (an % 2 == 0) {
    med[0] = an / 2;
    med[1] = an / 2;
  } else {
    med[0] = (an - 1) / 2;
    med[1] = (an + 1) / 2;
  }
  int diff = 1000000000, ans;
  for (int i = 0; i < n; i++) {
    if (diff > min(abs(x[i] - med[0]), abs(x[i] - med[1]))) {
      ans = x[i];
      diff = min(abs(x[i] - med[0]), abs(x[i] - med[1]));
    }
  }
  /*
      int aan=*max_element(x.begin(),x.end());
      if(aan%2==0){med[0]=aan/2;med[1]=aan/2;}
      else {med[0]=(aan-1)/2;med[1]=(aan+1)/2;}
      int ddiff=1000000000,aans;
      for(int i=0;i<n;i++) {
          if(ddiff>min(abs(x[i] - med[0]), abs(x[i] - med[1]))){
              aans=x[i];
              ddiff=min(abs(x[i] - med[0]), abs(x[i] - med[1]));
          }
      }
      if(combination(an,ans)<combination(aan,aans)){an=aan;ans=aans;}
      */
  cout << an << " " << ans << endl;
  return 0;
}
Problem p03380 (C++)

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

const int maxn = 1;
typedef long long ll;

int n, m, ans;
int a[maxn];

int main() {
  int i, j, k, T;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }

  double b = ans / 2.0;

  int ans2;

  ans2 = (a[1] == ans) ? a[2] : a[1];

  for (i = 1; i <= n; ++i) {
    if (a[i] != ans && abs(a[i] - b) < abs(ans2 - b)) {
      ans2 = a[i];
    }
  }

  cout << ans << " " << ans2;

  return 0;
}


Fixed submission:
//
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;

const int maxn = 2e5 + 7;
typedef long long ll;

int n, m, ans;
int a[maxn];

int main() {
  int i, j, k, T;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }

  double b = ans / 2.0;

  int ans2;

  ans2 = (a[1] == ans) ? a[2] : a[1];

  for (i = 1; i <= n; ++i) {
    if (a[i] != ans && abs(a[i] - b) < abs(ans2 - b)) {
      ans2 = a[i];
    }
  }

  cout << ans << " " << ans2;

  return 0;
}
Problem p03380 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int M = *max_element(a.begin(), a.end());
  int m = -1;
  int tmp;
  for (int i = 0; i < n; i++) {
    if (a[i] == *max_element(a.begin(), a.end()))
      continue;
    if (m == -1 || tmp > abs(2 * a[i] - M)) {
      tmp = abs(2 * a[i] - M);
      m = a[i];
    }
  }
  cout << M << ' ' << m << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int M = *max_element(a.begin(), a.end());
  int m = -1;
  int tmp;
  for (int i = 0; i < n; i++) {
    if (a[i] == M)
      continue;
    if (m == -1 || tmp > abs(2 * a[i] - M)) {
      tmp = abs(2 * a[i] - M);
      m = a[i];
    }
  }
  cout << M << ' ' << m << endl;
  return 0;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
  int n, ma = 0, m = 1000000000, i, t;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  ma = a[n - 1];
  printf("%d", ma);
  for (i = 0; i < n - 1; i++) {
    if (abs(a[i] - ma / 2) <= m) {
      m = abs(a[i] - a[ma / 2]);
      t = i;
    }
  }
  printf(" %d", a[t]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
  int n, ma = 0, m = 1000000000, i, t;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  ma = a[n - 1];
  printf("%d", ma);
  for (i = 0; i < n - 1; i++) {
    if (abs(a[i] - ma / 2) <= m) {
      m = abs(a[i] - ma / 2);
      t = i;
    }
  }
  printf(" %d", a[t]);
  return 0;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
/*
ID: kiwio
LANG: C++14
TASK:
*/

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

int n, num, x[200005], yay, dist, nd, ans, ind;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL); /*
#ifndef turtle
ofstream cout (".out");
ifstream cin (".in");
#endif*/
  cin >> num;
  for (int i = 1; i <= num; i++) {
    cin >> x[i];
    n = max(n, x[i]);
    if (x[i] == n) {
      ind = i;
    }
  }
  cout << n << " ";
  dist = 1000000000;
  for (int i = 1; i <= n; i++) {
    if (i != ind) {
      if (abs(n / 2 - x[i]) < dist and x[i] <= n) {
        dist = abs(n / 2 - x[i]);
        nd = x[i];
      }
    }
  }
  cout << nd;
}

Fixed submission:
/*
ID: kiwio
LANG: C++14
TASK:
*/

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

int n, num, x[200005], yay, dist, nd, ans, ind;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL); /*
#ifndef turtle
ofstream cout (".out");
ifstream cin (".in");
#endif*/
  cin >> num;
  for (int i = 1; i <= num; i++) {
    cin >> x[i];
    n = max(n, x[i]);
    if (x[i] == n) {
      ind = i;
    }
  }
  cout << n << " ";
  dist = 1000000000;
  for (int i = 1; i <= num; i++) {
    if (i != ind) {
      if (abs(n / 2 - x[i]) < dist and x[i] <= n) {
        dist = abs(n / 2 - x[i]);
        nd = x[i];
      }
    }
  }
  cout << nd;
}
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct Edge {
  ll to;
  ll cost;
};
long long combination(long n, long r, long long mod) {
  // nCrを返す　O(r)
  if (n < r)
    swap(n, r);
  long long bunbo = 1;
  long cnt = r;
  while (0 < cnt) {
    bunbo *= cnt;
    bunbo % mod;
    cnt--;
  }
  if (bunbo == 0)
    return 1;
  long long bunsi = 1;
  cnt = 0;
  while (cnt < r) {
    bunsi *= (n - cnt);
    bunbo %= mod;
    cnt++;
  }
  return bunsi / bunbo % mod;
}
int main() {
  int n;
  cin >> n;
  vector<long> a(n);
  REP(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  ll _n = a[n - 1];
  ll _r;
  if (combination(_n, a[n / 2], MOD) < combination(_n, a[n / 2 - 1], MOD)) {
    _r = a[n / 2 - 1];
  } else {
    _r = a[n / 2];
  }
  cout << _n << " " << _r << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct Edge {
  ll to;
  ll cost;
};
long long combination(long n, long r, long long mod) {
  // nCrを返す　O(r)
  if (n < r)
    swap(n, r);
  long long bunbo = 1;
  long cnt = r;
  while (0 < cnt) {
    bunbo *= cnt;
    bunbo % mod;
    cnt--;
  }
  if (bunbo == 0)
    return 1;
  long long bunsi = 1;
  cnt = 0;
  while (cnt < r) {
    bunsi *= (n - cnt);
    bunbo %= mod;
    cnt++;
  }
  return bunsi / bunbo % mod;
}
int main() {
  int n;
  cin >> n;
  vector<long> a(n);
  REP(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  ll _n = a[n - 1];
  ll _r = a[0];
  for (int i = 0; i < n - 1; i++) {
    if (abs(a[i] - _n / 2) <= abs(_r - _n / 2))
      _r = a[i];
  }
  cout << _n << " " << _r << endl;
}
Problem p03380 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX3(a, b, c) ((a) > (MAX(b, c)) ? (a) : (MAX(b, c)))
#define MIN3(a, b, c) ((a) < (MIN(b, c)) ? (a) : (MIN(b, c)))

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

int main() {
  ll n;
  ll a[100000];
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] < a[j])
        swap(a[i], a[j]);
    }
  }

  cout << a[0] << " ";
  ll max = a[0];
  if (max % 2 == 0) {
    ll mid = max / 2;
    for (int i = 1; i < n; i++) {
      if (a[i] < mid) {
        if (mid - a[i] > a[i - 1] - mid)
          cout << a[i - 1] << endl;
        else
          cout << a[i] << endl;
        break;
      }
    }
  } else {
    ll mid = max / 2;
    for (int i = 1; i < n; i++) {
      if (a[i] <= mid) {
        if (mid - a[i] > a[i - 1] - mid - 1)
          cout << a[i - 1] << endl;
        else
          cout << a[i] << endl;
        break;
      }
    }
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX3(a, b, c) ((a) > (MAX(b, c)) ? (a) : (MAX(b, c)))
#define MIN3(a, b, c) ((a) < (MIN(b, c)) ? (a) : (MIN(b, c)))

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

int main() {
  ll n;
  ll a[100000];
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n, greater<ll>());

  cout << a[0] << " ";
  ll max = a[0];
  if (max % 2 == 0) {
    ll mid = max / 2;
    for (int i = 1; i < n; i++) {
      if (a[i] < mid) {
        if (mid - a[i] > a[i - 1] - mid)
          cout << a[i - 1] << endl;
        else
          cout << a[i] << endl;
        break;
      }
    }
  } else {
    ll mid = max / 2;
    for (int i = 1; i < n; i++) {
      if (a[i] <= mid) {
        if (mid - a[i] > a[i - 1] - mid - 1)
          cout << a[i - 1] << endl;
        else
          cout << a[i] << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p03380 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    if n == 2:
        print(a[0], a[1])
    ma = max(a)
    for i in range(n):
        if ma // 2 - i in a:
            print(ma, ma // 2 - i)
            exit()
        if ma // 2 - i + 1 in a:
            print(ma, ma // 2 - i + 1)
            exit()


main()


Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    if n == 2:
        print(max(a), min(a))
    else:
        ma = max(a)
        sa = sorted([(i, abs(ma // 2 - a[i])) for i in range(n)], key=lambda x: x[1])
        print(ma, a[sa[0][0]])


main()
Problem p03380 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  int n, i, j, min, minj, max = 0, maxa, maxb;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  min = a[n - 1];
  for (i = 0; i < n; i++) {
    if (min > abs(2 * a[i] - a[n - 1])) {
      min = abs(2 * a[i] - a[n - 1]);
      minj = a[i];
    }
  }
  cout << a[n - 1] << " " << a[minj] << endl;
  return 0;
}


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

int main() {
  int n, i, j, min, minj, max = 0, maxa, maxb;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  min = a[n - 1];
  for (i = 0; i < n; i++) {
    if (min > abs(2 * a[i] - a[n - 1])) {
      min = abs(2 * a[i] - a[n - 1]);
      minj = a[i];
    }
  }
  cout << a[n - 1] << " " << minj << endl;
  return 0;
}
Problem p03381 (C++)

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

const int N = 1e5 + 10;
int n, x[N], ind[N];
vector<pair<int, int>> v;

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    v.push_back({x[i], i});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++)
    ind[v[i].second] = i;

  int m = (n + 1) / 2;
  m--;
  // cout << m << endl;

  for (int i = 0; i < n; i++) {
    if (ind[i] <= m)
      cout << v[m + 1].first << ' ';
    else
      cout << v[m].first << ' ';
  }
}


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

const int N = 3e5 + 10;
int n, x[N], ind[N];
vector<pair<int, int>> v;

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    v.push_back({x[i], i});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++)
    ind[v[i].second] = i;

  int m = (n + 1) / 2;
  m--;
  // cout << m << endl;

  for (int i = 0; i < n; i++) {
    if (ind[i] <= m)
      cout << v[m + 1].first << ' ';
    else
      cout << v[m].first << ' ';
  }
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void) {
  long long n, x, y, z;
  scanf("%lld", &n);
  long long a[n], b[n];
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    b[i] = a[i];
  }
  sort(a, a + 5);
  y = a[n / 2 - 1];
  z = a[n / 2];
  for (int i = 0; i < n; i++) {
    if (b[i] <= y) {
      printf("%lld\n", z);
    } else {
      printf("%lld\n", y);
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void) {
  long long n, x, y, z;
  scanf("%lld", &n);
  long long a[n], b[n];
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    b[i] = a[i];
  }
  sort(a, a + n);
  y = a[(n / 2) - 1];
  z = a[n / 2];
  for (int i = 0; i < n; i++) {
    if (b[i] <= y) {
      printf("%lld\n", z);
    } else {
      printf("%lld\n", y);
    }
  }
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now << 1)
#define mid ((l + r) >> 1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i & (-i))
#define par pair<int, int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define sep(i, x, y) for (int i = x; i >= y; --i)
#define fore(i, x) for (int i = fir[x]; i; i = nex[i])

cs int G = 3;
cs int N = 2e5;
cs int ff = 1e5 + 15;
cs int inf = 1e18 + 1;
cs int base = 2333;
cs int M = 998244353;
cs double eps = 1e-14;

struct Node {
  int x, id;
} A[ff];
bool operator<(Node B, Node C) { return B.x < C.x; }
int n, Ans[ff];
void init() {
  cin >> n;
  rep(i, 1, n) scanf("%lld", &A[i].x), A[i].id = i;
  int md = n / 2;
  sort(A + 1, A + 1 + n);
  rep(i, 1, md) Ans[A[i].id] = A[md + 1].x;
  rep(i, md + 1, n) Ans[A[i].id] = A[md].x;
  rep(i, 1, n) cout << Ans[i] << "\n";
}
signed main() {
  int Ts = 1;
  while (Ts--)
    init();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now << 1)
#define mid ((l + r) >> 1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i & (-i))
#define par pair<int, int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define sep(i, x, y) for (int i = x; i >= y; --i)
#define fore(i, x) for (int i = fir[x]; i; i = nex[i])

cs int G = 3;
cs int N = 2e5;
cs int ff = 1e6 + 15;
cs int inf = 1e18 + 1;
cs int base = 2333;
cs int M = 998244353;
cs double eps = 1e-14;

struct Node {
  int x, id;
} A[ff];
bool operator<(Node B, Node C) { return B.x < C.x; }
int n, Ans[ff];
void init() {
  cin >> n;
  rep(i, 1, n) scanf("%lld", &A[i].x), A[i].id = i;
  int md = n / 2;
  sort(A + 1, A + 1 + n);
  rep(i, 1, md) Ans[A[i].id] = A[md + 1].x;
  rep(i, md + 1, n) Ans[A[i].id] = A[md].x;
  rep(i, 1, n) cout << Ans[i] << "\n";
}
signed main() {
  int Ts = 1;
  while (Ts--)
    init();
}
Problem p03381 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

const int N = 1e5 + 5;

int n;
int a[N], b[N];

int32_t main() {
  IOS;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  int mid1 = b[n / 2];
  int mid2 = b[n / 2 + 1];
  for (int i = 1; i <= n; i++) {
    if (a[i] >= mid2) {
      cout << mid1 << endl;
    } else {
      cout << mid2 << endl;
    }
  }
  return 0;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

const int N = 2e5 + 5;

int n;
int a[N], b[N];

int32_t main() {
  IOS;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  int mid1 = b[n / 2];
  int mid2 = b[n / 2 + 1];
  for (int i = 1; i <= n; i++) {
    if (a[i] >= mid2) {
      cout << mid1 << endl;
    } else {
      cout << mid2 << endl;
    }
  }
  return 0;
}
Problem p03381 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
  int N;
  cin >> N;
  int i;
  vector<LL> A(N), B(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = A[i];
  }
  int l = N / 2 - 1;
  sort(B.begin(), B.end(), greater<LL>());
  int j = 0;
  for (i = 0; i <= N; i++) {
    for (j = 0; j < N; j++) {
      if (i == j) {
        if (B[l] > A[j]) {
          cout << B[l] << endl;
        } else {
          cout << B[l + 1] << endl;
        }
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
  int N;
  cin >> N;
  int i;
  vector<LL> A(N), B(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = A[i];
  }
  int l = N / 2 - 1;
  sort(B.begin(), B.end(), greater<LL>());
  int j = 0;
  for (i = 0; i < N; i++) {
    if (B[l] > A[i]) {
      cout << B[l] << endl;
    } else {
      cout << B[l + 1] << endl;
    }
  }
  return 0;
}
Problem p03381 (C++)

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

const int nmax = 100100;
int N;
int a[nmax];
int b[nmax];

void read() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
}

void solve() {
  sort(a, a + N);
  int l = (N + 1) / 2 - 1;
  for (int i = 0; i < N; ++i) {
    if (b[i] <= a[l])
      cout << a[l + 1] << endl;
    else
      cout << a[l] << endl;
  }
}

int main() {
  read();
  solve();
}

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

const int nmax = 200100;
int N;
int a[nmax];
int b[nmax];

void read() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
}

void solve() {
  sort(a, a + N);
  int l = (N + 1) / 2 - 1;
  for (int i = 0; i < N; ++i) {
    if (b[i] <= a[l])
      cout << a[l + 1] << endl;
    else
      cout << a[l] << endl;
  }
}

int main() {
  read();
  solve();
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define LINF 1000000000LL * 1000000
#define MAXN 45002
using namespace std;

int n;
int a[100000];
int b[100000];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    if (a[i] <= b[n / 2 - 1]) {
      cout << b[n / 2] << endl;
    } else
      cout << b[n / 2 - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define LINF 1000000000LL * 1000000
#define MAXN 45002
using namespace std;

int n;
int a[200000];
int b[200000];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    if (a[i] <= b[n / 2 - 1]) {
      cout << b[n / 2] << endl;
    } else
      cout << b[n / 2 - 1] << endl;
}
Problem p03381 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define vl vector<ll>
#define pl pair<ll, ll>
#define mp make_pair
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) for (ll i = 0; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
using namespace std;
ll n, k;
vl a, b;
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    a.push_back(k);
    b.push_back(k);
  }
  sort(all(a));
  for (int i = 0; i < n; i++) {
    auto point = lower_bound(all(a), b.at(i));
    a.erase(point + 0);
    cout << a.at(n / 2 - 1) << endl;
    a.insert(point, b.at(i));
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define vl vector<ll>
#define pl pair<ll, ll>
#define mp make_pair
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) for (ll i = 0; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
using namespace std;
ll n, k;
vl a, b;
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    a.push_back(k);
    b.push_back(k);
  }
  sort(all(a));
  for (int i = 0; i < n; i++) {
    ll point = lower_bound(all(a), b.at(i)) - a.begin();
    (point + 1 <= (n / 2)) ? cout << a.at(n / 2) << endl
                           : cout << a.at(n / 2 - 1) << endl;
  }
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef vector<pi> vpi;
typedef set<ll> si;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define f first
#define s second
ll INF = 1e18;
ll MOD = 1e9 + 7;
#define lb lower_bound
#define ub upper_bound
#define SZ(x) (ll) x.size()
#define ALL(x) x.begin(), x.end()
int l, r, A[100100], B[100100], N;

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    B[i] = A[i];
  }
  sort(A, A + N);
  l = A[N / 2 - 1];
  r = A[N / 2];
  for (int i = 0; i < N; ++i) {
    if (B[i] <= l)
      cout << r << '\n';
    else
      cout << l << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef vector<pi> vpi;
typedef set<ll> si;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define f first
#define s second
ll INF = 1e18;
ll MOD = 1e9 + 7;
#define lb lower_bound
#define ub upper_bound
#define SZ(x) (ll) x.size()
#define ALL(x) x.begin(), x.end()
int l, r, A[200100], B[200100], N;

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    B[i] = A[i];
  }
  sort(A, A + N);
  l = A[N / 2 - 1];
  r = A[N / 2];
  for (int i = 0; i < N; ++i) {
    if (B[i] <= l)
      cout << r << '\n';
    else
      cout << l << '\n';
  }
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
// Power Of Ninja Go
#include <bits/stdc++.h>
// #ifdef atom #else #endif
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define X first
#define Y second
#define pb push_back
vector<ii> vec;
const int maxn = 1e5 + 5;
int ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    vec.pb(ii(x, i));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < n / 2; i++) {
    ans[vec[i].Y] = vec[n / 2].X;
  }
  for (int i = n / 2; i < n; i++) {
    ans[vec[i].Y] = vec[n / 2 - 1].X;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}


Fixed submission:
// Power Of Ninja Go
#include <bits/stdc++.h>
// #ifdef atom #else #endif
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define X first
#define Y second
#define pb push_back
vector<ii> vec;
const int maxn = 2e5 + 5;
int ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    vec.pb(ii(x, i));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < n / 2; i++) {
    ans[vec[i].Y] = vec[n / 2].X;
  }
  for (int i = n / 2; i < n; i++) {
    ans[vec[i].Y] = vec[n / 2 - 1].X;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int n;
pair<int, pair<int, int>> a[N];

bool cmp(pair<int, pair<int, int>> P1, pair<int, pair<int, int>> P2) {
  return P1.second.first < P2.second.first;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i].first);
  for (int i = 1; i <= n; i++)
    a[i].second.first = i;
  sort(a + 1, a + n + 1);
  int t = n / 2;
  for (int i = 1; i <= n; i++) {
    int x = i - 1, y = n - i;
    //		a[i].second.second=a[i+(y-x)/2].first;
    if (i <= t)
      a[i].second.second = a[t + 1].first;
    else
      a[i].second.second = a[t].first;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    printf("%d\n", a[i].second.second);
}

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

int n;
pair<int, pair<int, int>> a[N];

bool cmp(pair<int, pair<int, int>> P1, pair<int, pair<int, int>> P2) {
  return P1.second.first < P2.second.first;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i].first);
  for (int i = 1; i <= n; i++)
    a[i].second.first = i;
  sort(a + 1, a + n + 1);
  int t = n / 2;
  for (int i = 1; i <= n; i++) {
    int x = i - 1, y = n - i;
    //		a[i].second.second=a[i+(y-x)/2].first;
    if (i <= t)
      a[i].second.second = a[t + 1].first;
    else
      a[i].second.second = a[t].first;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    printf("%d\n", a[i].second.second);
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define pii pair<long long, long long>
#define reset(a, b) memset(a, b, sizeof a)
#define ll long long
#define MOD 1000000007
#define MID (l + r) / 2
#define ALL(x) x.begin(), x.end()
#define dalam fl <= l &&r <= fr
#define kiwo 2 * (idx), l, MID
#define tengen 2 * idx + 1, MID + 1, r
#define debug(x) cout << #x << " = " << (x) << endl
#define mx 100003
#define pc(x) putchar_unlocked(x);

int bit[mx], a[mx], n;
vector<int> ve;

void upd(int idx, int val) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

int cari(int idx) {
  int ret = 0;
  for (; idx; idx -= idx & -idx)
    ret += bit[idx];
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ve.pb(a[i]);
  }
  sort(ALL(ve));
  for (int i = 1; i <= n; i++) {
    int in = lower_bound(ALL(ve), a[i]) - ve.begin() + 1;
    upd(in, 1);
  }
  for (int i = 1; i <= n; i++) {
    int in = lower_bound(ALL(ve), a[i]) - ve.begin() + 1;
    upd(in, -1);
    int lo = 1, hi = n, tengah;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (cari(mid - 1) <= n / 2 - 1) {
        lo = mid + 1;
        tengah = mid;
      } else
        hi = mid - 1;
    }
    upd(in, 1);
    cout << ve[tengah - 1] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define pii pair<long long, long long>
#define reset(a, b) memset(a, b, sizeof a)
#define ll long long
#define MOD 1000000007
#define MID (l + r) / 2
#define ALL(x) x.begin(), x.end()
#define dalam fl <= l &&r <= fr
#define kiwo 2 * (idx), l, MID
#define tengen 2 * idx + 1, MID + 1, r
#define debug(x) cout << #x << " = " << (x) << endl
#define mx 200003
#define pc(x) putchar_unlocked(x);

int bit[mx], a[mx], n;
vector<int> ve;

void upd(int idx, int val) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

int cari(int idx) {
  int ret = 0;
  for (; idx; idx -= idx & -idx)
    ret += bit[idx];
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ve.pb(a[i]);
  }
  sort(ALL(ve));
  for (int i = 1; i <= n; i++) {
    int in = lower_bound(ALL(ve), a[i]) - ve.begin() + 1;
    upd(in, 1);
  }
  for (int i = 1; i <= n; i++) {
    int in = lower_bound(ALL(ve), a[i]) - ve.begin() + 1;
    upd(in, -1);
    int lo = 1, hi = n, tengah;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (cari(mid - 1) <= n / 2 - 1) {
        lo = mid + 1;
        tengah = mid;
      } else
        hi = mid - 1;
    }
    upd(in, 1);
    cout << ve[tengah - 1] << endl;
  }
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, i, j, a[100005], b[100005];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", a + i);
  memcpy(b, a, sizeof(a));
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    if (b[i] <= a[n / 2])
      printf("%d\n", a[n / 2 + 1]);
    else
      printf("%d\n", a[n / 2]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, i, j, a[200005], b[200005];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", a + i);
  memcpy(b, a, sizeof(a));
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    if (b[i] <= a[n / 2])
      printf("%d\n", a[n / 2 + 1]);
    else
      printf("%d\n", a[n / 2]);
  }
  return 0;
}
Problem p03381 (C++)

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

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 23456;
int ans[MAXN];

int main() {
  int N;
  while (~scanf("%d", &N)) {
    int mid = N / 2;
    vector<pii> line;
    for (int i = 0; i < N; i++) {
      int tmp;
      scanf("%d", &tmp);
      line.push_back(make_pair(tmp, i));
    }
    sort(line.begin(), line.end());
    for (int i = 0; i < int(line.size()); i++) {
      if (i < mid)
        ans[line[i].second] = line[mid].first;
      else
        ans[line[i].second] = line[mid - 1].first;
      // printf("fst = %d second = %d\n", line[i].first, line[i].second);
    }
    for (int i = 0; i < N; i++) {
      printf("%d\n", ans[i]);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 234567;
int ans[MAXN];

int main() {
  int N;
  while (~scanf("%d", &N)) {
    int mid = N / 2;
    vector<pii> line;
    for (int i = 0; i < N; i++) {
      int tmp;
      scanf("%d", &tmp);
      line.push_back(make_pair(tmp, i));
    }
    sort(line.begin(), line.end());
    for (int i = 0; i < int(line.size()); i++) {
      if (i < mid)
        ans[line[i].second] = line[mid].first;
      else
        ans[line[i].second] = line[mid - 1].first;
      // printf("fst = %d second = %d\n", line[i].first, line[i].second);
    }
    for (int i = 0; i < N; i++) {
      printf("%d\n", ans[i]);
    }
  }
  return 0;
}
Problem p03381 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(i);
  }

  sort(b.begin(), b.end(), [a](int &p, int &q) { return a[p] < a[q]; });
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (i < n / 2) {
      ans[b[i]] = a[b[n / 2]];
    } else {
      ans[b[i]] = a[b[n / 2 - 1]];
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(i);
  }

  sort(b.begin(), b.end(), [&a](int &p, int &q) { return a[p] < a[q]; });
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (i < n / 2) {
      ans[b[i]] = a[b[n / 2]];
    } else {
      ans[b[i]] = a[b[n / 2 - 1]];
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  int N, Y[20000];
  vector<int> X;

  scanf("%d", &N);
  int i;
  REP(i, N) {
    cin >> Y[i];
    X.push_back(Y[i]);
  }

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

  REP(i, N) {
    if (Y[i] <= X[(N - 1) / 2]) {
      cout << X[(N - 1) / 2 + 1] << endl;
    } else {
      cout << X[(N - 1) / 2] << endl;
    }
  }

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  int N, Y[200000];
  vector<int> X;

  scanf("%d", &N);
  int i;
  REP(i, N) {
    cin >> Y[i];
    X.push_back(Y[i]);
  }

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

  REP(i, N) {
    if (Y[i] <= X[(N - 1) / 2]) {
      cout << X[(N - 1) / 2 + 1] << endl;
    } else {
      cout << X[(N - 1) / 2] << endl;
    }
  }

  return 0;
}
Problem p03381 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Rep(i, a, b) for (register int i = (a); i <= (b); ++i)
using namespace std;
template <typename T> inline void read(T &x) {
  T f;
  register char ch;
  x = 0;
  f = 1;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  x *= f;
}

const int MAXN = 1e5 + 7;

static int n, a[MAXN], b[MAXN];

inline void init() {
  read(n);
  Rep(i, 1, n) read(a[i]), b[i] = a[i];
}

inline void solve() {
  sort(b + 1, b + n + 1);
  Rep(i, 1, n) if (a[i] <= b[n / 2]) printf("%d\n", b[n / 2 + 1]);
  else printf("%d\n", b[n / 2]);
}

int main() {
  init();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define Rep(i, a, b) for (register int i = (a); i <= (b); ++i)
using namespace std;
template <typename T> inline void read(T &x) {
  T f;
  register char ch;
  x = 0;
  f = 1;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  x *= f;
}

const int MAXN = 2e5 + 7;

static int n, a[MAXN], b[MAXN];

inline void init() {
  read(n);
  Rep(i, 1, n) read(a[i]), b[i] = a[i];
}

inline void solve() {
  sort(b + 1, b + n + 1);
  Rep(i, 1, n) if (a[i] <= b[n / 2]) printf("%d\n", b[n / 2 + 1]);
  else printf("%d\n", b[n / 2]);
}

int main() {
  init();
  solve();
  return 0;
}
Problem p03381 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll n;
ll a[101010];
P b[101010];
ll res[101010];

int main() {
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    b[i] = P(a[i], i);
  }
  sort(b, b + n);
  ll m = (n + 1) / 2;
  m--;
  rep(i, n) {
    if (i <= m)
      res[b[i].se] = b[m + 1].fi;
    else
      res[b[i].se] = b[m].fi;
  }
  rep(i, n) cout << res[i] << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll n;
ll a[201010];
P b[201010];
ll res[201010];

int main() {
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    b[i] = P(a[i], i);
  }
  sort(b, b + n);
  ll m = (n + 1) / 2;
  m--;
  rep(i, n) {
    if (i <= m)
      res[b[i].se] = b[m + 1].fi;
    else
      res[b[i].se] = b[m].fi;
  }
  rep(i, n) cout << res[i] << endl;
  return 0;
}