Problem p03380 (C++)

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

using namespace std;
typedef long long ll;
#define tr(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define scontains(c, x) ((c).find(x) != (c).end()) // O(log n)
#define contains(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n)
#define Ichar(_x)                                                              \
  char _x;                                                                     \
  scanf("%c", &_x);
#define Iint(_x)                                                               \
  int _x;                                                                      \
  scanf("%d", &_x);
#define Ill(_x)                                                                \
  ll _x;                                                                       \
  scanf("%lld", &_x);
#define inc(a, b, x) (x) > a && (x) < b
#define incE(a, b, x) (x) >= a && (x) <= b
#define max(a, b) (a) >= (b) ? (a) : (b)
#define fileio                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
const double pi = 3.14159265358979323846;

#define F first
#define S second
const int MX = 1e5 + 10, MD = 1e9 + 7;
long long n, m, a[MX], ans, tmp, mx, t1, t2, ind, res;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) {
      mx = a[i];
      ind = i;
    }
  }
  ans = mx + 1;
  ll t = 50000000000000;
  for (ll i = 0; i < pow(10, 10);) {
    std::cout << "";
    i++;
    t /= abs(t - i);
  }
  if (mx % 2 == 0) {
    t1 = mx / 2;
    for (int i = 0; i < n; i++) {
      if (ind == i)
        continue;
      if (abs(a[i] - t1) < ans) {
        ans = abs(a[i] - t1);
        res = a[i];
      }
    }
  } else {
    t1 = mx / 2;
    t2 = mx / 2 + 1;
    for (int i = 0; i < n; i++) {
      if (ind == i)
        continue;
      if (abs(a[i] - t1) < ans) {
        ans = abs(a[i] - t1);
        res = a[i];
      } else if (abs(a[i] - t2) < ans) {
        ans = abs(a[i] - t2);
        res = a[i];
      }
    }
  }
  cout << mx << " " << res;
}


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

using namespace std;
typedef long long ll;
#define tr(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define scontains(c, x) ((c).find(x) != (c).end()) // O(log n)
#define contains(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n)
#define Ichar(_x)                                                              \
  char _x;                                                                     \
  scanf("%c", &_x);
#define Iint(_x)                                                               \
  int _x;                                                                      \
  scanf("%d", &_x);
#define Ill(_x)                                                                \
  ll _x;                                                                       \
  scanf("%lld", &_x);
#define inc(a, b, x) (x) > a && (x) < b
#define incE(a, b, x) (x) >= a && (x) <= b
#define max(a, b) (a) >= (b) ? (a) : (b)
#define fileio                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
const double pi = 3.14159265358979323846;

#define F first
#define S second
const int MX = 1e5 + 10, MD = 1e9 + 7;
long long n, m, a[MX], ans, tmp, mx, t1, t2, ind, res;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) {
      mx = a[i];
      ind = i;
    }
  }
  ans = mx + 1;
  ll t = 50000000000000;
  for (ll i = 0; i < pow(10, 9);) {
    printf("");
    i++;
    t /= abs(t - i);
  }
  if (mx % 2 == 0) {
    t1 = mx / 2;
    for (int i = 0; i < n; i++) {
      if (ind == i)
        continue;
      if (abs(a[i] - t1) < ans) {
        ans = abs(a[i] - t1);
        res = a[i];
      }
    }
  } else {
    t1 = mx / 2;
    t2 = mx / 2 + 1;
    for (int i = 0; i < n; i++) {
      if (ind == i)
        continue;
      if (abs(a[i] - t1) < ans) {
        ans = abs(a[i] - t1);
        res = a[i];
      } else if (abs(a[i] - t2) < ans) {
        ans = abs(a[i] - t2);
        res = a[i];
      }
    }
  }
  cout << mx << " " << res;
}