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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], first = 0, second = 0;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= first) {
      second = first;
      first = a[i];
    } else if (a[i] >= second) {
      second = a[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == first)
      cout << second << endl;
    else
      cout << first << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 10;
int a[maxn], first = 0, second = 0;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= first) {
      second = first;
      first = a[i];
    } else if (a[i] >= second) {
      second = a[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == first)
      cout << second << endl;
    else
      cout << first << endl;
  }
  return 0;
}
Problem p02971 (C++)

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

int main() {

  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  // freopen("text.in", "r", stdin);
  // freopen("text.out", "w", stdout);

  int a[(int)1e5 + 1], n;
  cin >> n;
  int max = 0, place = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] > max) {
      max = a[i];
      place = i;
    }
  }
  if (n == 1)
    return cout << a[0] << '\n', 0;
  for (int i = 0; i < n; ++i) {
    if (i != place)
      cout << max << " ";
    else {
      int temp = max;
      a[i] = 0;
      cout << *max_element(a + 0, a + n) << " ";
      a[i] = temp;
    }
  }
  cout << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#define lli long long int
using namespace std;

int main() {

  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  // freopen("text.in", "r", stdin);
  // freopen("text.out", "w", stdout);

  int a[(int)2e5 + 1], n;
  cin >> n;
  int max = 0, place = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] > max) {
      max = a[i];
      place = i;
    }
  }
  if (n == 1)
    return cout << a[0] << '\n', 0;
  for (int i = 0; i < n; ++i) {
    if (i != place)
      cout << max << " ";
    else {
      int temp = max;
      a[i] = 0;
      cout << *max_element(a + 0, a + n) << " ";
      a[i] = temp;
    }
  }
  cout << '\n';
  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
const int maxn = 100000;
int N, A[maxn], mx1, mx2;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i < N; i++) {
    if (A[i] > mx1)
      mx2 = mx1, mx1 = A[i];
    else if (A[i] > mx2)
      mx2 = A[i];
  }
  for (int i = 0; i < N; i++)
    if (A[i] == mx1)
      printf("%d\n", mx2);
    else
      printf("%d\n", mx1);
}


Fixed submission:
#include <cstdio>
using namespace std;
const int maxn = 200000;
int N, A[maxn], mx1, mx2;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i < N; i++) {
    if (A[i] > mx1)
      mx2 = mx1, mx1 = A[i];
    else if (A[i] > mx2)
      mx2 = A[i];
  }
  for (int i = 0; i < N; i++)
    if (A[i] == mx1)
      printf("%d\n", mx2);
    else
      printf("%d\n", mx1);
}
Problem p02971 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;

const ll e5 = 1 << 20;
const ll mod = 998244353;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

int main() {
  int n;
  cin >> n;

  vector<int> a(n, 0);
  vector<int> a_num(n, 0);
  int max_a = 0;
  int max_a_ind = -1;
  int nex_max_a = 0;
  rep(i, n) {
    cin >> a[i];
    a_num[a[i]] += 1;
    if (a[i] > max_a) {
      max_a = a[i];
      max_a_ind = i;
    } else if (a[i] > nex_max_a) {
      nex_max_a = a[i];
    }
  }

  rep(i, n) {
    if (max_a_ind != i || a_num[a[i]] > 1) {
      cout << max_a << endl;
    } else {
      cout << nex_max_a << endl;
    }
  }

  return 0;
}

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

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;

const ll e5 = 1 << 20;
const ll mod = 998244353;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

int main() {
  int n;
  cin >> n;

  vector<int> a(n, 0);
  vector<int> a_num(200000, 0);
  int max_a = 0;
  int max_a_ind = -1;
  int nex_max_a = 0;
  rep(i, n) {
    cin >> a[i];
    a_num[a[i]] += 1;
    if (a[i] > max_a) {
      max_a = a[i];
      max_a_ind = i;
    } else if (a[i] > nex_max_a) {
      nex_max_a = a[i];
    }
  }

  rep(i, n) {
    if (max_a_ind != i || a_num[a[i]] > 1) {
      cout << max_a << endl;
    } else {
      cout << nex_max_a << endl;
    }
  }

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int n;
  int a[100000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int no1;
  int no2;

  for (int i = 0; i < n; i++) {
    if (a[i] > no1) {
      no2 = no1;
      no1 = a[i];
    } else if (a[i] > no2) {
      no2 = a[i];
    }
  }

  for (int i = 0; i < n; i++) {
    if (a[i] != no1) {
      printf("%d\n", no1);
    } else {
      printf("%d\n", no2);
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int n;
  int a[200000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int no1;
  int no2;

  for (int i = 0; i < n; i++) {
    if (a[i] > no1) {
      no2 = no1;
      no1 = a[i];
    } else if (a[i] > no2) {
      no2 = a[i];
    }
  }

  for (int i = 0; i < n; i++) {
    if (a[i] != no1) {
      printf("%d\n", no1);
    } else {
      printf("%d\n", no2);
    }
  }
  return 0;
}
Problem p02971 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  rep(i, N) {
    int tmp;
    tmp = A[i];
    A[i] = 0;
    int m = *max_element(A.begin(), A.end());
    cout << m << endl;
    A[i] = tmp;
  }

  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  auto iter1 = max_element(A.begin(), A.end());
  int m1 = *iter1;
  A[iter1 - A.begin()] = 0;
  auto iter2 = max_element(A.begin(), A.end());
  int m2 = *iter2;
  A[iter1 - A.begin()] = m1;

  rep(i, N) cout << ((A[i] != m1) ? m1 : m2) << endl;

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  vector<int> B(N);
  for (int i = 0; i < N; i++)
    B.at(i) = A.at(i);
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  int Max = B.at(0);
  int Max1 = B.at(1);
  for (int i = 0; i < N - 1; i++) {
    if (A.at(i) != Max)
      cout << Max << " ";
    else
      cout << Max1 << " ";
  }
  if (A.at(N - 1) != Max)
    cout << Max << endl;
  else
    cout << Max1 << endl;

  return 0;
}

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.at(i);
  vector<int> B(N);
  for (int i = 0; i < N; i++)
    B.at(i) = A.at(i);
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  int Max = B.at(0);
  int Max1 = B.at(1);
  for (int i = 0; i < N - 1; i++) {
    if (A.at(i) != Max)
      cout << Max << " ";
    else
      cout << Max1 << " ";
  }
  if (A.at(N - 1) != Max)
    cout << Max << endl;
  else
    cout << Max1 << endl;

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Time Limit Exceeded):

// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f; // (int)3e18;
const int maxn = 2000100;

int main() {
  _ int n;
  cin >> n;
  vi a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(all(b));
  for (int i = 0; i < n; i++) {
    int mx = *max_element(b.begin(), b.end());
    if (mx == a[i]) {
      cout << b[n - 2] << endl;
    } else
      cout << mx << endl;
  }

  return 0;
}


Fixed submission:

// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f; // (int)3e18;
const int maxn = 2000100;

int main() {
  _ int n;
  cin >> n;
  vi a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(all(b));
  for (int i = 0; i < n; i++) {
    int mx = b[n - 1];
    if (mx == a[i]) {
      cout << b[n - 2] << endl;
    } else
      cout << mx << endl;
  }

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define zy 2333333
#define bql 2147483647
using namespace std;
int n;
struct node {
  int num, id, max;
} a[100005];
bool cmp(node x, node y) { return x.num < y.num; }
bool cmpp(node x, node y) { return x.id < y.id; }
int main() {
  fast;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].num, a[i].id = i;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    if (a[i].num != a[n].num)
      a[i].max = a[n].num;
    else
      a[i].max = a[n - 1].num;
  sort(a + 1, a + n + 1, cmpp);
  for (int i = 1; i <= n; i++)
    cout << a[i].max << endl;

  return 0;
}


Fixed submission:
// #pragma GCC optimize("Ofast")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define zy 2333333
#define bql 2147483647
using namespace std;
int n;
struct node {
  int num, id, max;
} a[200005];
bool cmp(node x, node y) { return x.num < y.num; }
bool cmpp(node x, node y) { return x.id < y.id; }
int main() {
  fast;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].num, a[i].id = i;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    if (a[i].num != a[n].num)
      a[i].max = a[n].num;
    else
      a[i].max = a[n - 1].num;
  sort(a + 1, a + n + 1, cmpp);
  for (int i = 1; i <= n; i++)
    cout << a[i].max << endl;

  return 0;
}
Problem p02971 (C++)

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

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define null nullptr
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mp make_pair
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef unordered_map<int, int> mp;

const int mod = 1e9 + 7;
const int least = -1e9;
const int most = 1e9;

int32_t main() {
  FAST;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  vi a(n + 2, least);
  for (int x = 1; x <= n; x++)
    cin >> a[x];

  vi pref = a, suff = a;

  for (int x = 1; x <= n; x++)
    pref[x] = max(pref[x], pref[x - 1]);
  for (int x = n - 1; x > 0; x--)
    suff[x] = max(suff[x], suff[x + 1]);

  for (int x = 1; x <= n; x++) {
    cout << max(pref[x - 1], suff[x + 1]) << " ";
  }

  return 0;
}


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

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define null nullptr
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mp make_pair
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef unordered_map<int, int> mp;

const int mod = 1e9 + 7;
const int least = -1e9;
const int most = 1e9;

int32_t main() {
  FAST;

  int n;
  cin >> n;
  vi a(n + 2, least);
  for (int x = 1; x <= n; x++)
    cin >> a[x];

  vi pref = a, suff = a;

  for (int x = 1; x <= n; x++)
    pref[x] = max(pref[x], pref[x - 1]);
  for (int x = n - 1; x > 0; x--)
    suff[x] = max(suff[x], suff[x + 1]);

  for (int x = 1; x <= n; x++) {
    cout << max(pref[x - 1], suff[x + 1]) << " ";
  }

  return 0;
}
Problem p02971 (C++)

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

int main() {
  int a[nmax] = {}, n, max1 = -1, max2 = -1, x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[i] = x;
    if (x > max1) {
      max2 = max1;
      max1 = x;
    } else {
      if (max1 == x) {
        max2 = x;
      } else {
        if (x > max2) {
          max2 = x;
        }
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] != max1) {
      cout << max1 << endl;
    } else {
      cout << max2 << endl;
    }
  }

  return 0;
}


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

int main() {
  int a[nmax] = {}, n, max1 = -1, max2 = -1, x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[i] = x;
    if (x > max1) {
      max2 = max1;
      max1 = x;
    } else {
      if (max1 == x) {
        max2 = x;
      } else {
        if (x > max2) {
          max2 = x;
        }
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] != max1) {
      cout << max1 << endl;
    } else {
      cout << max2 << endl;
    }
  }

  return 0;
}
Problem p02971 (C++)

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

#define pb push_back
#define ll long long
#define f first
#define s second
#define ld long double
#define fa fflush(stdout);

using namespace std;
void data() {
#ifdef NURS
  freopen("main.in", "r", stdin);
  freopen("main.out", "w", stdout);
#endif
}
const int N = 5e5 + 100;
const ll mod = 1e9 + 7;

int main() {
  data();
  int n;
  cin >> n;
  int a[n + 1];
  int mx = 0;
  int kol = 0;
  set<int> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
    s.insert(a[i]);
  }
  s.erase(*s.rbegin());
  int ans2 = *s.rbegin();
  for (int i = 1; i <= n; i++) {
    if (mx == a[i]) {
      kol++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != mx) {
      cout << mx << '\n';
      continue;
    }
    if (kol > 1) {
      cout << mx << '\n';
      continue;
    }
    cout << ans2 << '\n';
  }
}
// JUDGE_ID: 295965SY
// Tis I

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

#define pb push_back
#define ll long long
#define f first
#define s second
#define ld long double
#define fa fflush(stdout);

using namespace std;
void data() {
#ifdef NURS
  freopen("main.in", "r", stdin);
  freopen("main.out", "w", stdout);
#endif
}
const int N = 5e5 + 100;
const ll mod = 1e9 + 7;

int main() {
  data();
  int n;
  cin >> n;
  int a[n + 1];
  int mx = 0;
  int kol = 0;
  set<int> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
    s.insert(a[i]);
  }
  s.erase(*s.rbegin());
  int ans2;
  if (s.size() > 0) {
    ans2 = *s.rbegin();
  }
  for (int i = 1; i <= n; i++) {
    if (mx == a[i]) {
      kol++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != mx) {
      cout << mx << '\n';
      continue;
    }
    if (kol > 1) {
      cout << mx << '\n';
      continue;
    }
    cout << ans2 << '\n';
  }
}
// JUDGE_ID: 295965SY
// Tis I
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
signed main() {
  int a, d;
  int b[100000], c[100000];
  ;
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
    c[i] = b[i];
  }
  sort(b, b + a, greater<int>());
  for (int i = 0; i < a; i++) {
    d = 0;
    if (b[0] != c[i]) {
      cout << b[0] << "\n";
      d = 1;
    }
    if (b[0] == c[i] && d == 0 || b[0] == c[i]) {
      cout << b[1] << "\n";
    }
  }
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
signed main() {
  int a, d;
  int b[1000000], c[1000000];
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
    c[i] = b[i];
  }
  sort(b, b + a, greater<int>());
  for (int i = 0; i < a; i++) {
    d = 0;
    if (b[0] != c[i]) {
      cout << b[0] << "\n";
      d = 1;
    }
    if (b[0] == c[i] && d == 0 || b[0] == c[i]) {
      cout << b[1] << "\n";
    }
  }
  return (0);
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define mFOR(i, a, b) for (int i = a; i > b; i--)
#define MP make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()
#define N 100007
#define INF 1000000007
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll fceil(ll a, ll b) { return (a % b == 0 ? a / b : a / b + 1); }
int main() {
  int n, a[N] = {}, mx1 = 0, mx2 = 0, ctr1 = 0, ctr2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    if (mx1 < a[i]) {
      mx2 = mx1;
      ctr2 = ctr1;
      mx1 = a[i];
      ctr1 = 1;
    } else if (mx1 == a[i]) {
      ctr1++;
    } else if (mx2 < a[i]) {
      mx2 = a[i];
      ctr2 = 1;
    } else if (mx2 == a[i]) {
      ctr2++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != mx1 || ctr1 > 1)
      cout << mx1 << endl;
    else
      cout << mx2 << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define mFOR(i, a, b) for (int i = a; i > b; i--)
#define MP make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()
#define N 200007
#define INF 1000000007
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll fceil(ll a, ll b) { return (a % b == 0 ? a / b : a / b + 1); }
int main() {
  int n, a[N] = {}, mx1 = 0, mx2 = 0, ctr1 = 0, ctr2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    if (mx1 < a[i]) {
      mx2 = mx1;
      ctr2 = ctr1;
      mx1 = a[i];
      ctr1 = 1;
    } else if (mx1 == a[i]) {
      ctr1++;
    } else if (mx2 < a[i]) {
      mx2 = a[i];
      ctr2 = 1;
    } else if (mx2 == a[i]) {
      ctr2++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != mx1 || ctr1 > 1)
      cout << mx1 << endl;
    else
      cout << mx2 << endl;
  }
  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const int INF = 1e18;
const int M = 1e9 + 7;
//__int128
const int maxn = 3e5;
int a[maxn], b[maxn];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  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 - 1]) {
      cout << b[n - 2] << endl;
    } else {
      cout << b[n - 1] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const int INF = 1e18;
const int M = 1e9 + 7;
//__int128
const int maxn = 3e5;
int a[maxn], b[maxn];
signed main() {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  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 - 1]) {
      cout << b[n - 2] << endl;
    } else {
      cout << b[n - 1] << endl;
    }
  }
  return 0;
}
Problem p02971 (C++)

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

int main() {
  int n;
  cin >> n;
  multiset<int> s;
  vector<int> v(n);
  int m = n;
  for (auto &i : v) {
    cin >> i;
    s.insert(i);
  }
  for (auto i : v) {
    s.erase(i);
    cout << *s.rbegin() << "\n";
    s.insert(i);
  }

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  multiset<int> s;
  vector<int> v(n);
  int m = n;
  for (auto &i : v) {
    cin >> i;
    s.insert(i);
  }
  for (auto i : v) {
    s.erase(s.find(i));
    cout << *s.rbegin() << "\n";
    s.insert(i);
  }

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
// Exception Handling - atcoder.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

#include <algorithm>
#include <iostream>
using namespace std;

const int N = 1e5;
int arr[N], tempp[N], n;
int main() {
  cin >> n;

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

  sort(tempp, tempp + n);
  prevMax = tempp[n - 2];
  curMax = tempp[n - 1];

  for (int i = 0; i < n; i++) {
    if (arr[i] == curMax)
      cout << prevMax << endl;
    else
      cout << curMax << endl;
  }
}


Fixed submission:
// Exception Handling - atcoder.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

#include <algorithm>
#include <iostream>
using namespace std;

const int N = 2e5 + 6;
int arr[N], tempp[N], n;
int main() {
  cin >> n;

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

  sort(tempp, tempp + n);
  prevMax = tempp[n - 2];
  curMax = tempp[n - 1];

  for (int i = 0; i < n; i++) {
    if (arr[i] == curMax)
      cout << prevMax << endl;
    else
      cout << curMax << endl;
  }
}
Problem p02971 (C++)

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

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

  int N;
  cin >> N;
  long a[110000];
  int z = 0, in;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (z < a[i]) {
      in = i;
      z = a[i];
    }
  }
  sort(a, a + N);
  if (a[N - 1] == a[N - 2]) {
    for (int i = 0; i < N; i++) {
      cout << a[N - 1] << endl;
    }
  } else {
    for (int i = 0; i < in; i++) {
      cout << a[N - 1] << endl;
    }
    cout << a[N - 2] << endl;
    for (int i = in + 1; i < N; i++) {
      cout << a[N - 1] << endl;
    }
  }
}

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

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

  int N;
  cin >> N;
  long a[210000];
  int z = 0, in;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (z < a[i]) {
      in = i;
      z = a[i];
    }
  }
  sort(a, a + N);
  if (a[N - 1] == a[N - 2]) {
    for (int i = 0; i < N; i++) {
      cout << a[N - 1] << endl;
    }
  } else {
    for (int i = 0; i < in; i++) {
      cout << a[N - 1] << endl;
    }
    cout << a[N - 2] << endl;
    for (int i = in + 1; i < N; i++) {
      cout << a[N - 1] << endl;
    }
  }
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
int main() {
  int N;
  cin >> N;
  vi a(N);
  set<int> s;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  for (int i = 0; i < N; i++) {
    auto it = s.find(a[i]);
    s.erase(it);
    cout << *s.rbegin() << endl;
    s.insert(a[i]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
int main() {
  int N;
  cin >> N;
  vi a(N);
  multiset<int> s;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  for (int i = 0; i < N; i++) {
    auto it = s.find(a[i]);
    s.erase(it);
    cout << *s.rbegin() << endl;
    s.insert(a[i]);
  }
  return 0;
}
Problem p02971 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
semi_max = 0
aa = []
for i in range(n):
    a = int(input())
    if i > 0:
        if a >= semi_max and a < max(aa):
            semi_max = a
        elif a == max(aa):
            semi_max = max(aa)
    aa.append(a)
    # print(semi_max)
for each in aa:
    print(max(aa) if each <= semi_max else semi_max)


Fixed submission:
n = int(input())
aa = [int(input()) for _ in range(n)]
aho = sorted(aa)
for a in aa:
    print(aho[-1] if a < aho[-1] else aho[-2])
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define mod 1e9
int a[100005], b[100005];
int32_t main() {
  int n;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    if (a[n - 1] == b[i])
      cout << a[n - 2] << "\n";
    else
      cout << a[n - 1] << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define mod 1e9
int a[200005], b[200005];
int32_t main() {
  int n;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    if (a[n - 1] == b[i])
      cout << a[n - 2] << "\n";
    else
      cout << a[n - 1] << "\n";
  }
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int INFTY = 1 << 21;
int L[50000], R[50000];

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = INFTY;
  int i = 0, j = 0;
  for (int p = left; p < right; p++) {
    if (L[i] <= R[j])
      A[p] = L[i++];
    else
      A[p] = R[j++];
  }
}

void sort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    sort(A, left, mid);
    sort(A, mid, right);
    merge(A, left, mid, right);
  }
}

int main() {
  int n, max = 0, p;
  cin >> n;
  int A[200005];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
      p = i;
    }
  }
  sort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i == p)
      cout << A[n - 2] << endl;
    else
      cout << A[n - 1] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int INFTY = 1 << 21;
int L[200005], R[200005];

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = INFTY;
  int i = 0, j = 0;
  for (int p = left; p < right; p++) {
    if (L[i] <= R[j])
      A[p] = L[i++];
    else
      A[p] = R[j++];
  }
}

void sort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    sort(A, left, mid);
    sort(A, mid, right);
    merge(A, left, mid, right);
  }
}

int main() {
  int n, max = 0, p;
  cin >> n;
  int A[200005];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
      p = i;
    }
  }
  sort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i == p)
      cout << A[n - 2] << endl;
    else
      cout << A[n - 1] << endl;
  }
  return 0;
}
Problem p02971 (C++)

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

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

const int MAX = 1e5 + 10;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

int N, A[MAX];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int maxi[2];
  maxi[0] = maxi[1] = 0;

  for (int i = 0; i < N; i++) {
    if (maxi[0] < A[i]) {
      maxi[1] = maxi[0];
      maxi[0] = A[i];
    } else if (maxi[1] < A[i]) {
      maxi[1] = A[i];
    }
  }

  for (int i = 0; i < N; i++) {
    if (maxi[0] == A[i])
      cout << maxi[1] << endl;
    else
      cout << maxi[0] << endl;
  }
}

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

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

const int MAX = 1e6 + 10;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

int N, A[MAX];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int maxi[2];
  maxi[0] = maxi[1] = 0;

  for (int i = 0; i < N; i++) {
    if (maxi[0] < A[i]) {
      maxi[1] = maxi[0];
      maxi[0] = A[i];
    } else if (maxi[1] < A[i]) {
      maxi[1] = A[i];
    }
  }

  for (int i = 0; i < N; i++) {
    if (maxi[0] == A[i])
      cout << maxi[1] << endl;
    else
      cout << maxi[0] << endl;
  }
}
Problem p02971 (C++)

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

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MAXX 100005

int n;
int a[MAXX];
int b[MAXX];

int main() {
  _FastIO;
  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 - 1]) {
      cout << b[n - 2] << endl;
    } else
      cout << b[n - 1] << endl;
  }
  return 0;
}


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

typedef long long ll;
#define pii pair<int, int>
#define _FastIO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define MAXX 200005

int n;
int a[MAXX];
int b[MAXX];

int main() {
  _FastIO;
  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 - 1]) {
      cout << b[n - 2] << endl;
    } else
      cout << b[n - 1] << endl;
  }
  return 0;
}
Problem p02971 (C++)

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

int main() {
  int N, A;
  cin >> N;
  vector<int> As(N);

  for (int i = 0; i < N; i++) {
    cin >> As.at(i);
  }

  for (int i = 0; i < N; i++) {
    vector<int> tempAs = As;
    tempAs.at(i) = 0;
    sort(tempAs.begin(), tempAs.end());
    cout << tempAs.at(N - 1) << endl;
  }
}

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

int main() {
  int N, A;
  cin >> N;
  vector<int> As(N);

  for (int i = 0; i < N; i++) {
    cin >> As.at(i);
  }

  vector<int> tempAs = As;
  sort(tempAs.begin(), tempAs.end());

  if (tempAs.at(N - 1) == tempAs.at(N - 2)) {
    for (int i = 0; i < N; i++) {
      cout << tempAs.at(N - 1) << endl;
    }
  } else {
    int place = 0;
    for (int j = 0; j < N; j++) {
      if (As.at(j) < tempAs.at(N - 1))
        place++;
      if (As.at(j) == tempAs.at(N - 1))
        break;
    }
    for (int i = 0; i < place; i++) {
      cout << tempAs.at(N - 1) << endl;
    }
    cout << tempAs.at(N - 2) << endl;
    for (int i = place + 1; i < N; i++) {
      cout << tempAs.at(N - 1) << endl;
    }
  }
}
Problem p02971 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    B = A;
    B[i] = 0;
    sort(B.begin(), B.end());
    cout << B[N - 1] << endl;
  }
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  B = A;

  sort(B.begin(), B.end());
  int max = B[N - 1];

  if (B[N - 2] == B[N - 1]) {
    for (int i = 0; i < N; i++) {
      cout << B[N - 1] << endl;
    }
  } else {

    for (int i = 0; i < N; i++) {
      if (A[i] == max) {
        cout << B[N - 2] << endl;
      } else {
        cout << max << endl;
      }
    }
  }
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[100010], b[100010];
  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 - 1])
      cout << b[n - 2] << endl;
    else
      cout << b[n - 1] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200010], b[200010];
  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 - 1])
      cout << b[n - 2] << endl;
    else
      cout << b[n - 1] << endl;
  }
  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((int)(x).size())
#define MSG(a) cout << #a << " " << a << endl;

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  REP(i, N) { cin >> v[i]; }

  int maxNum = *max_element(ALL(v));
  REP(i, N) {
    int tmp = v[i];
    if (tmp == maxNum && count(ALL(v), maxNum) == 1)
    // if (tmp == maxNum)
    {
      v[i] = *min_element(ALL(v));
      cout << *max_element(ALL(v)) << endl;
      v[i] = tmp;
    } else {
      cout << maxNum << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((int)(x).size())
#define MSG(a) cout << #a << " " << a << endl;

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  REP(i, N) { cin >> v[i]; }

  int maxNum = *max_element(ALL(v));
  if (count(ALL(v), maxNum) > 1) {
    REP(i, N) { cout << maxNum << endl; }
  } else {
    REP(i, N) {
      int tmp = v[i];
      if (tmp == maxNum) {
        v[i] = *min_element(ALL(v));
        cout << *max_element(ALL(v)) << endl;
        v[i] = tmp;
      } else {
        cout << maxNum << endl;
      }
    }
  }

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define itn int
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define F2(i, a, b) for (int i = a; i < (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
const int maxn = 1000 * 100;
const int maxa = 1000 * 1000 * 1000 + 7;
typedef long long ll;
const int mod = 1000 * 1000 * 1000 + 7;
using namespace std;
int t, n, m, T, d;
int main() {
#ifdef _DEBUG
  freopen("E:\\input.txt", "r", stdin);
  //	freopen("output.txt", "w", stdout);
#endif
  int i, j;
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n) {
    itn max1 = 0, max2 = 0, a[maxn];
    for (int i = 0; i < n; i++) {

      cin >> a[i];
      int &tmp = a[i];
      if (tmp > max1) {
        max2 = max1;
        max1 = tmp;
      } else if (tmp > max2) {
        max2 = tmp;
      }
    }
    F2(i, 0, n) {
      if (a[i] == max1)
        cout << max2;
      else
        cout << max1;
      cout << "\n";
    }
  }
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define itn int
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define F2(i, a, b) for (int i = a; i < (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
const int maxn = 2 * 1000 * 100;
const int maxa = 1000 * 1000 * 1000 + 7;
typedef long long ll;
const int mod = 1000 * 1000 * 1000 + 7;
using namespace std;
int t, n, m, T, d;
int main() {
#ifdef _DEBUG
  freopen("E:\\input.txt", "r", stdin);
  //	freopen("output.txt", "w", stdout);
#endif
  int i, j;
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n) {
    itn max1 = 0, max2 = 0, a[maxn];
    for (int i = 0; i < n; i++) {

      cin >> a[i];
      int &tmp = a[i];
      if (tmp > max1) {
        max2 = max1;
        max1 = tmp;
      } else if (tmp > max2) {
        max2 = tmp;
      }
    }
    F2(i, 0, n) {
      if (a[i] == max1)
        cout << max2;
      else
        cout << max1;
      cout << "\n";
    }
  }
  return 0;
}
Problem p02971 (C++)

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

typedef long long ll;
#define reg register
#define rep(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define drep(i, a, b) for (int i = a, i##end = b; i >= i##end; --i)

char IO;
int rd() {
  int s = 0, f = 0;
  while (!isdigit(IO = getchar()))
    if (IO == '-')
      f = 1;
  do
    s = (s << 1) + (s << 3) + (IO ^ '0');
  while (isdigit(IO = getchar()));
  return f ? -s : s;
}

const int N = 1e5 + 10;

int n, a[N];
int s[N], s1[N];

int main() {
  rep(i, 1, n = rd()) a[i] = rd(), s[i] = max(a[i], s[i - 1]);
  drep(i, n, 1) s1[i] = max(a[i], s1[i + 1]);
  rep(i, 1, n) { printf("%d\n", max(s[i - 1], s1[i + 1])); };
}


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

typedef long long ll;
#define reg register
#define rep(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define drep(i, a, b) for (int i = a, i##end = b; i >= i##end; --i)

char IO;
int rd() {
  int s = 0, f = 0;
  while (!isdigit(IO = getchar()))
    if (IO == '-')
      f = 1;
  do
    s = (s << 1) + (s << 3) + (IO ^ '0');
  while (isdigit(IO = getchar()));
  return f ? -s : s;
}

const int N = 5e5 + 10;

int n, a[N];
int s[N], s1[N];

int main() {
  rep(i, 1, n = rd()) a[i] = rd(), s[i] = max(a[i], s[i - 1]);
  drep(i, n, 1) s1[i] = max(a[i], s1[i + 1]);
  rep(i, 1, n) { printf("%d\n", max(s[i - 1], s1[i + 1])); };
}
Problem p02971 (C++)

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const int N = 1e5 + 5;

int n, a[N];
multiset<int> m;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m.insert(a[i]);
  }

  for (int i = 1; i <= n; i++) {
    m.erase(m.find(a[i]));
    auto it = m.end();
    it--;
    cout << *it << "\n";
    m.insert(a[i]);
  }

  return 0;
}


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

typedef long long ll;
const ll MOD = 1e9 + 7;
const int N = 2e5 + 5;

int n, a[N];
multiset<int> m;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m.insert(a[i]);
  }

  for (int i = 1; i <= n; i++) {
    m.erase(m.find(a[i]));
    auto it = m.end();
    it--;
    cout << *it << "\n";
    m.insert(a[i]);
  }

  return 0;
}
Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  rep(i, 0, N) cin >> A[i];
  multiset<i64> st;
  rep(i, 0, N) st.insert(A[i]);
  rep(i, 0, N) {
    st.erase(A[i]);
    cout << *(--st.end()) << endl;
    st.insert(A[i]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  rep(i, 0, N) cin >> A[i];
  multiset<i64> st;
  rep(i, 0, N) st.insert(A[i]);
  rep(i, 0, N) {
    st.erase(st.lower_bound(A[i]));
    cout << *(--st.end()) << endl;
    st.insert(A[i]);
  }
}