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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  P p[N];
  rep(i, N) {
    int a;
    cin >> a;
    p[i] = P(a, i + 1);
  }
  sort(p, p + N);
  reverse(p, p + N);
  multiset<int> vec;
  vec.insert(0);
  vec.insert(0);
  vec.insert(N + 1);
  vec.insert(N + 1);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int id = p[i].second;
    auto itr = lower_bound(vec.begin(), vec.end(), id);
    int R1 = *itr++;
    int R2 = *itr--;
    itr--;
    int L2 = *itr--;
    int L1 = *itr;
    ans += p[i].first * (id - L2) * (R2 - R1);
    ans += p[i].first * (L2 - L1) * (R1 - id);
    vec.insert(id);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  P p[N];
  rep(i, N) {
    int a;
    cin >> a;
    p[i] = P(a, i + 1);
  }
  sort(p, p + N);
  reverse(p, p + N);
  multiset<int> vec;
  vec.insert(0);
  vec.insert(0);
  vec.insert(N + 1);
  vec.insert(N + 1);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int id = p[i].second;
    auto itr = vec.lower_bound(id);
    int R1 = *itr++;
    int R2 = *itr--;
    itr--;
    int L2 = *itr--;
    int L1 = *itr;
    ans += p[i].first * (id - L2) * (R2 - R1);
    ans += p[i].first * (L2 - L1) * (R1 - id);
    vec.insert(id);
  }
  cout << ans << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_map>
#include <vector>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;

const ll INF = 1e9;
const ll MOD = 1000000007; // 1e9 + 7

int main(int argc, char **argv) {
  int N;
  cin >> N;
  vector<P> sorted;
  rep(i, N) {
    ll p;
    cin >> p;
    sorted.emplace_back(p, i); // first is value, second is index
  }
  sort(all(sorted));
  reverse(all(sorted));

  set<ll> st; // Contains the indices of larger values in each step.
  st.insert(-2);
  st.insert(-1); // left guard
  st.insert(N);
  st.insert(N + 1); // right guard

  ll ans = 0;
  for (auto e : sorted) { // Check value and index order by decreasing.
    ll v = e.first;
    ll i = e.second;

    auto it = upper_bound(all(st), i);
    --it;
    --it;
    ll l2 = *(it++);
    ll l1 = *(it++);
    ll r1 = *(it++);
    ll r2 = *it;

    if (r1 < N) { // r1 must be in [0, N-1]
      ans += v * (i - l1) * (r2 - r1);
    }
    if (l1 >= 0) { // l1 must be in [0, N-1]
      ans += v * (l1 - l2) * (r1 - i);
    }
    st.insert(i);
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_map>
#include <vector>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;

const ll INF = 1e9;
const ll MOD = 1000000007; // 1e9 + 7

int main(int argc, char **argv) {
  int N;
  cin >> N;
  vector<P> sorted;
  rep(i, N) {
    ll p;
    cin >> p;
    sorted.emplace_back(p, i); // first is value, second is index
  }
  sort(all(sorted));
  reverse(all(sorted));

  set<ll> st; // Contains the indices of larger values in each step.
  st.insert(-2);
  st.insert(-1); // left guard
  st.insert(N);
  st.insert(N + 1); // right guard

  ll ans = 0;
  for (auto e : sorted) { // Check value and index order by decreasing.
    ll v = e.first;
    ll i = e.second;

    auto it = st.upper_bound(i);
    --it;
    --it;
    ll l2 = *(it++);
    ll l1 = *(it++);
    ll r1 = *(it++);
    ll r2 = *it;

    if (r1 < N) { // r1 must be in [0, N-1]
      ans += v * (i - l1) * (r2 - r1);
    }
    if (l1 >= 0) { // l1 must be in [0, N-1]
      ans += v * (l1 - l2) * (r1 - i);
    }
    st.insert(i);
  }

  cout << ans << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;

int main(int argc, const char *argv[]) {
  int N;
  cin >> N;
  vector<ll> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];

  vector<ll> v(N);
  iota(v.begin(), v.end(), 0LL);
  sort(v.begin(), v.end(), [&](int i, int j) { return p[i] > p[j]; });

  multiset<ll> st;
  st.insert(-1);
  st.insert(-1);
  st.insert(N);
  st.insert(N);
  ll wi, xi, yi, zi, ans;
  ans = 0;
  for (auto i : v) {
    auto itr = lower_bound(st.begin(), st.end(), i);
    yi = *itr;
    itr++;
    zi = *itr;
    itr--;
    itr--;
    xi = *itr;
    itr--;
    wi = *itr;
    ans += p[i] * (xi - wi) * (yi - i);
    ans += p[i] * (i - xi) * (zi - yi);
    st.insert(i);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;

int main(int argc, const char *argv[]) {
  int N;
  cin >> N;
  vector<ll> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];

  vector<ll> v(N);
  iota(v.begin(), v.end(), 0LL);
  sort(v.begin(), v.end(), [&](int i, int j) { return p[i] > p[j]; });

  multiset<ll> st;
  st.insert(-1);
  st.insert(-1);
  st.insert(N);
  st.insert(N);
  ll wi, xi, yi, zi, ans;
  ans = 0;
  for (auto i : v) {
    auto itr = st.lower_bound(i);
    yi = *itr;
    itr++;
    zi = *itr;
    itr--;
    itr--;
    xi = *itr;
    itr--;
    wi = *itr;
    ans += p[i] * (xi - wi) * (yi - i);
    ans += p[i] * (i - xi) * (zi - yi);
    st.insert(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> id(n);
  rep(i, 0, n) {
    int tmp;
    cin >> tmp;
    id[tmp] = i + 1; // はいれつの中=つよさ,id[i]=順番
  }
  multiset<ll> ms;
  ms.insert(0);
  ms.insert(0);
  ms.insert(n + 1);
  ms.insert(n + 1);
  ll ans = 0;
  rel(i, n + 1, 1) {
    auto c = ms.lower_bound(id[i]);
    auto d = c;
    ++d;
    auto b = c;
    --b;
    auto a = b;
    --a;
    ll add = (*b - *a) * (*c - id[i]) + (id[i] - *b) * (*d - *c);
    ans += add * i;
    ms.insert(id[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> id(n + 1);
  rep(i, 0, n) {
    int tmp;
    cin >> tmp;
    id[tmp] = i + 1; // はいれつの中=つよさ,id[i]=順番
  }
  multiset<ll> ms;
  ms.insert(0);
  ms.insert(0);
  ms.insert(n + 1);
  ms.insert(n + 1);
  ll ans = 0;
  rel(i, n + 1, 1) {
    auto c = ms.lower_bound(id[i]);
    auto d = c;
    ++d;
    auto b = c;
    --b;
    auto a = b;
    --a;
    ll add = (*b - *a) * (*c - id[i]) + (id[i] - *b) * (*d - *c);
    ans += add * i;
    ms.insert(id[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll BIG = 1e15;

int main() {
  int N;
  cin >> N;
  vector<int> d(N + 1);
  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;
    d.at(x) = i;
  }

  ll ans = 0;
  multiset<int> S;
  S.insert(N + 1);
  S.insert(N + 1);
  S.insert(0);
  S.insert(0);
  for (ll i = N; i >= 1; i--) {
    S.insert(d.at(i));
    auto itr = lower_bound(S.begin(), S.end(), d.at(i));
    ll m0, l1, l2, r1, r2;
    itr++;
    r1 = *itr;
    itr++;
    r2 = *itr;
    itr--;
    itr--;
    m0 = *itr;
    itr--;
    l2 = *itr;
    itr--;
    l1 = *itr;
    ans += i * ((r2 - r1) * (m0 - l2) + (r1 - m0) * (l2 - l1));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll BIG = 1e15;

int main() {
  int N;
  cin >> N;
  vector<int> d(N + 1);
  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;
    d.at(x) = i;
  }

  ll ans = 0;
  multiset<int> S;
  S.insert(N + 1);
  S.insert(N + 1);
  S.insert(0);
  S.insert(0);
  for (ll i = N; i >= 1; i--) {
    S.insert(d.at(i));
    auto itr = S.find(d.at(i));
    ll m0, l1, l2, r1, r2;
    itr++;
    r1 = *itr;
    itr++;
    r2 = *itr;
    itr--;
    itr--;
    m0 = *itr;
    itr--;
    l2 = *itr;
    itr--;
    l1 = *itr;
    ans += i * ((r2 - r1) * (m0 - l2) + (r1 - m0) * (l2 - l1));
  }
  cout << ans << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x, y) cout << (x) << " " << (y) << endl;
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
typedef struct {
  ll lv;
  ll rv;
} Q;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  scanf("%lld", &N);
  ll P[100010] = {};
  rep(i, 0, N) scanf("%lld", &P[i]);
  vector<Q> arr(100010);
  arr[0] = (Q){-1, 1};
  arr[1] = (Q){0, 2};
  ll pos[100010] = {};
  rep(i, 0, N) {
    arr[i + 2] = (Q){i + 1, i + 3};
    rep(i, 0, N) pos[P[i]] = i + 2;
  }
  arr[N + 2] = (Q){N + 1, N + 3};
  arr[N + 3] = (Q){N + 2, N + 4};

  ll ans = 0;
  rep(n, 1, N) {
    ll p = pos[n];
    ll lv = arr[p].lv;
    ll rv = arr[p].rv;
    if (lv != 1) {
      ans += n * (lv - arr[lv].lv) * (rv - p);
    }
    if (rv != N + 2) {
      ans += n * (arr[rv].rv - rv) * (p - lv);
    }

    arr[lv].rv = arr[p].rv;
    arr[rv].lv = arr[p].lv;
  }
  print(ans);
}

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x, y) cout << (x) << " " << (y) << endl;
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
typedef struct {
  ll lv;
  ll rv;
} Q;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  scanf("%lld", &N);
  ll P[100010] = {};
  rep(i, 0, N) scanf("%lld", &P[i]);
  vector<Q> arr(100010);
  arr[0] = (Q){-1, 1};
  arr[1] = (Q){0, 2};
  ll pos[100010] = {};
  rep(i, 0, N) {
    arr[i + 2] = (Q){i + 1, i + 3};
    pos[P[i]] = i + 2;
  }
  arr[N + 2] = (Q){N + 1, N + 3};
  arr[N + 3] = (Q){N + 2, N + 4};

  ll ans = 0;
  rep(n, 1, N) {
    ll p = pos[n];
    ll lv = arr[p].lv;
    ll rv = arr[p].rv;
    if (lv != 1) {
      ans += n * (lv - arr[lv].lv) * (rv - p);
    }
    if (rv != N + 2) {
      ans += n * (arr[rv].rv - rv) * (p - lv);
    }

    arr[lv].rv = arr[p].rv;
    arr[rv].lv = arr[p].lv;
  }
  print(ans);
}
Problem p02919 (C++)

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

using namespace std;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, t;
  cin >> n;
  vector<ii> ar;
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    cin >> t, ar.push_back(make_pair(t, ctr1));

  sort(ar.begin(), ar.end());
  reverse(ar.begin(), ar.end());

  set<int> larger, smaller;
  ll rez = 0;
  for (auto nxt : ar) {
    auto up = upper_bound(larger.begin(), larger.end(), nxt.second);
    auto down = upper_bound(smaller.begin(), smaller.end(), -nxt.second);
    if (up != larger.end()) {
      auto it = up;
      ++it;
      int up_two = (it == larger.end() ? n : *it);
      int down_two = -(down == smaller.end() ? 1 : *down);
      rez += 1LL * nxt.first * (up_two - *up) * (nxt.second - down_two);
    }

    if (down != smaller.end()) {
      auto it = down;
      ++it;
      int down_two = (it == smaller.end() ? 1 : *it);
      int up_two = (up == larger.end() ? n : *up);
      rez += 1LL * nxt.first * (up_two - nxt.second) * (down_two - *down);
    }
    larger.insert(nxt.second);
    smaller.insert(-nxt.second);
  }
  cout << rez << "\n";
  return 0;
}

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

using namespace std;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, t;
  cin >> n;
  vector<ii> ar;
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    cin >> t, ar.push_back(make_pair(t, ctr1));

  sort(ar.begin(), ar.end());
  reverse(ar.begin(), ar.end());

  set<int> larger, smaller;
  ll rez = 0;
  for (auto nxt : ar) {
    auto up = larger.upper_bound(nxt.second);
    auto down = smaller.upper_bound(-nxt.second);
    if (up != larger.end()) {
      auto it = up;
      ++it;
      int up_two = (it == larger.end() ? n : *it);
      int down_two = -(down == smaller.end() ? 1 : *down);
      rez += 1LL * nxt.first * (up_two - *up) * (nxt.second - down_two);
    }

    if (down != smaller.end()) {
      auto it = down;
      ++it;
      int down_two = (it == smaller.end() ? 1 : *it);
      int up_two = (up == larger.end() ? n : *up);
      rez += 1LL * nxt.first * (up_two - nxt.second) * (down_two - *down);
    }
    larger.insert(nxt.second);
    smaller.insert(-nxt.second);
  }
  cout << rez << "\n";
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, p, l2, l1, r1, r2, res = 0;
  cin >> n;
  int index[111111];
  rep(i, n) {
    cin >> p;
    index[p] = i;
  }
  multiset<int> s;
  s.insert(-1);
  s.insert(-1);
  s.insert(n);
  s.insert(n);
  rev(i, n + 1, 1) {
    auto now = lower_bound(all(s), index[i]);
    r1 = *now;
    r2 = *++now;
    l1 = *-- --now;
    l2 = *--now;
    res += ((l1 - l2) * (r1 - index[i]) + (r2 - r1) * (index[i] - l1)) * i;
    s.insert(index[i]);
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, p, l2, l1, r1, r2, res = 0;
  cin >> n;
  int index[111111];
  rep(i, n) {
    cin >> p;
    index[p] = i;
  }
  multiset<int> s;
  s.insert(-1);
  s.insert(-1);
  s.insert(n);
  s.insert(n);
  rev(i, n + 1, 1) {
    auto now = s.lower_bound(index[i]);
    r1 = *now;
    r2 = *++now;
    l1 = *-- --now;
    l2 = *--now;
    res += ((l1 - l2) * (r1 - index[i]) + (r2 - r1) * (index[i] - l1)) * i;
    s.insert(index[i]);
  }
  cout << res << endl;
}
Problem p02920 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

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

  int n;
  cin >> n;

  int m = 1 << n;

  multiset<int> st;
  for (int i = 0; i < m; i++) {
    int s;
    cin >> s;
    st.insert(s);
  }

  vector<int> c(n);
  auto it = st.end();
  it--;
  c[0] = *it;
  st.erase(it);
  for (int k = 0; k < n; k++) {
    int l = 1 << k;
    for (int i = 0; i < l; i++) {
      auto it = st.lower_bound(c[i]);
      if (it == st.begin()) {
        cout << "No" << endl;
        exit(0);
      }
      it--;
      c[l + i] = *it;
      st.erase(it);
    }
  }

  cout << "Yes" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

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

  int n;
  cin >> n;

  int m = 1 << n;

  multiset<int> st;
  for (int i = 0; i < m; i++) {
    int s;
    cin >> s;
    st.insert(s);
  }

  vector<int> c(m);
  auto it = st.end();
  it--;
  c[0] = *it;
  st.erase(it);
  for (int k = 0; k < n; k++) {
    int l = 1 << k;
    for (int i = 0; i < l; i++) {
      auto it = st.lower_bound(c[i]);
      if (it == st.begin()) {
        cout << "No" << endl;
        exit(0);
      }
      it--;
      c[l + i] = *it;
      st.erase(it);
    }
  }

  cout << "Yes" << endl;

  return 0;
}
Problem p02920 (C++)

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

int n, s[N];
bool v[N];
priority_queue<int> pq[2];
int main() {
  int i, j;
  cin >> n;
  for (i = 0; i < 1 << n; i++) {
    scanf("%d", &s[i]);
  }
  sort(s, s + (1 << n), greater<int>());
  pq[0].push(s[0]);
  for (i = 0; i < n; i++) {
    for (j = 1; j < 1 << n; j++) {
      if (!v[j] && s[j] < pq[i % 2].top()) {
        v[j] = 1;
        pq[1 - i % 2].push(pq[i % 2].top());
        pq[1 - i % 2].push(s[j]);
        pq[i % 2].pop();
        if (pq[i % 2].empty())
          break;
      }
    }
    if (!pq[i % 2].empty()) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define N 300005
using namespace std;

int n, s[N];
bool v[N];
priority_queue<int> pq[2];
int main() {
  int i, j;
  cin >> n;
  for (i = 0; i < 1 << n; i++) {
    scanf("%d", &s[i]);
  }
  sort(s, s + (1 << n), greater<int>());
  pq[0].push(s[0]);
  for (i = 0; i < n; i++) {
    for (j = 1; j < 1 << n; j++) {
      if (!v[j] && s[j] < pq[i % 2].top()) {
        v[j] = 1;
        pq[1 - i % 2].push(pq[i % 2].top());
        pq[1 - i % 2].push(s[j]);
        pq[i % 2].pop();
        if (pq[i % 2].empty())
          break;
      }
    }
    if (!pq[i % 2].empty()) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}
Problem p02920 (C++)

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

using namespace std;
int n, val;
multiset<int> m1, m2;
int main() {
  cin >> n;
  for (int i = 1; i <= (1 << n); i++) {
    cin >> val;
    m1.insert(val);
  }
  auto it = m1.end();
  it--;
  int nr = *it;
  m2.insert(nr);
  m1.erase(it);
  for (int i = 1; i <= n; i++) {
    for (auto it = m2.begin(); it != m2.end(); it++) {
      nr = *it;
      auto it2 = m1.lower_bound(nr);
      it2--;
      int nr2 = *it2;
      if (nr2 >= nr) {
        cout << "No";
        return 0;
      }
      m2.insert(nr2);
      m1.erase(it2);
    }
  }
  cout << "Yes";
  return 0;
}


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

using namespace std;
int n, val;
multiset<int> m1, m2;
int main() {
  cin >> n;
  for (int i = 1; i <= (1 << n); i++) {
    cin >> val;
    m1.insert(val);
  }
  auto it = m1.end();
  it--;
  int nr = *it;
  m2.insert(nr);
  m1.erase(it);
  for (int i = 1; i <= n; i++) {
    for (auto it = m2.begin(); it != m2.end(); it++) {
      nr = *it;
      auto it2 = m1.lower_bound(nr);
      if (it2 == m1.begin()) {
        cout << "No";
        return 0;
      }
      it2--;
      int nr2 = *it2;
      if (nr2 >= nr) {
        cout << "No";
        return 0;
      }
      m2.insert(nr2);
      m1.erase(it2);
    }
  }
  cout << "Yes";
  return 0;
}
Problem p02920 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int n;
int m;
vector<int> ve;
multiset<int> se;

int main() {
  // freopen("input.txt", "r", stdin);
  scanf("%d", &n);
  m = (1 << n);
  for (int i = 0; i < m; i++) {
    int t;
    scanf("%d", &t);
    se.insert(t);
  }
  auto it = se.end();
  it--;
  ve.push_back(*it);
  se.erase(it);
  for (int i = 1; i < m; i += i) {
    for (int j = ve.size() - 1; j >= 0; j--) {
      auto it2 = se.lower_bound(ve[j]);
      if (*it2 >= ve[j] && it2 != se.begin())
        it2--;
      else if (*it2 >= ve[j]) {
        printf("No");
        return 0;
      }
      ve.push_back(*it2);
      se.erase(it2);
    }
  }
  printf("Yes");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int n;
int m;
vector<int> ve;
multiset<int> se;

int main() {
  // freopen("input.txt", "r", stdin);
  scanf("%d", &n);
  m = (1 << n);
  for (int i = 0; i < m; i++) {
    int t;
    scanf("%d", &t);
    se.insert(t);
  }
  auto it = se.end();
  it--;
  ve.push_back(*it);
  se.erase(it);
  for (int i = 1; i < m; i += i) {
    for (int j = ve.size() - 1; j >= 0; j--) {
      auto it2 = se.lower_bound(ve[j]);
      if (it2 == se.end() || *it2 >= ve[j] && it2 != se.begin())
        it2--;
      else if (*it2 >= ve[j]) {
        printf("No");
        return 0;
      }
      ve.push_back(*it2);
      se.erase(it2);
    }
  }
  printf("Yes");
  return 0;
}
Problem p02920 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  int n0 = (1 << n);
  multiset<int> ideal;
  rep(i, n0) {
    int tmp;
    cin >> tmp;
    ideal.insert(tmp);
  }
  vector<int> ans;
  auto ite = ideal.end();
  ans.push_back(*(--ite));
  ideal.erase(ite);
  rep(I, n) {
    vector<int> children = ans;
    rep(i, ans.size()) {
      int t = ans[i];
      auto it = lower_bound(all(ideal), t);
      if (ideal.begin() == it) {
        cout << "No" << endl;
        exit(0);
      }
      it--;
      children.push_back(*it);
      ideal.erase(it);
    }
    swap(ans, children);
  }
  cout << "Yes" << endl;
}


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

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  int n0 = (1 << n);
  multiset<int> ideal;
  rep(i, n0) {
    int tmp;
    cin >> tmp;
    ideal.insert(tmp);
  }
  vector<int> ans;
  auto ite = ideal.end();
  ans.push_back(*(--ite));
  ideal.erase(ite);
  rep(I, n) {
    vector<int> children = ans;
    rep(i, ans.size()) {
      int t = ans[i];
      auto it = ideal.lower_bound(t);
      if (ideal.begin() == it) {
        cout << "No" << endl;
        exit(0);
      }
      it--;
      children.push_back(*it);
      ideal.erase(it);
    }
    swap(ans, children);
  }
  cout << "Yes" << endl;
}
Problem p02920 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << i << std::endl

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!

  ll n;
  std::cin >> n;

  /*unusedはまだ生成してないスライム
    slimeは生成したスライム*/
  std::multiset<ll> unused, slime;
  rep(i, 0, 1 << n) {
    ll s;
    std::cin >> s;
    unused.insert(s);
  }

  // unusedの最大値を最初のスライムにする
  slime.insert(*unused.rbegin());
  unused.erase(*unused.rbegin());

  // 各秒を貪欲で考える
  rep(i, 0, n) {
    // この秒で生成するスライムを保管しておくtemp
    std::vector<ll> temp;

    // 今いるスライム各々が生成できる最大のスライムを作る
    for (auto itr = slime.rbegin(); itr != slime.rend(); itr++) {
      auto itr2 = std::lower_bound(all(unused), *itr);

      // 自分より小さいスライムがいないならNo
      if (itr2 == unused.begin()) {
        std::cout << "No";
        return 0;
      }
      itr2--;
      temp.push_back(*itr2);
      unused.erase(itr2);
    }

    rep(j, 0, 1 << i) slime.insert(temp[j]);
  }

  std::cout << "Yes";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << i << std::endl

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!

  ll n;
  std::cin >> n;

  /*unusedはまだ生成してないスライム
    slimeは生成したスライム*/
  std::multiset<ll> unused, slime;
  rep(i, 0, 1 << n) {
    ll s;
    std::cin >> s;
    unused.insert(s);
  }

  // unusedの最大値を最初のスライムにする
  slime.insert(*unused.rbegin());
  unused.erase(*unused.rbegin());

  // 各秒を貪欲で考える
  rep(i, 0, n) {
    // この秒で生成するスライムを保管しておくtemp
    std::vector<ll> temp;

    // 今いるスライム各々が生成できる最大のスライムを作る
    for (auto itr = slime.rbegin(); itr != slime.rend(); itr++) {
      auto itr2 = unused.lower_bound(*itr);

      // 自分より小さいスライムがいないならNo
      if (itr2 == unused.begin()) {
        std::cout << "No";
        return 0;
      }
      itr2--;
      temp.push_back(*itr2);
      unused.erase(itr2);
    }

    rep(j, 0, 1 << i) slime.insert(temp[j]);
  }

  std::cout << "Yes";

  return 0;
}
Problem p02920 (C++)

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
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 nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = 112345;
int A[MAX_N];
multiset<int> S, have;

bool solve(int N, multiset<int> &S, multiset<int> &have) {
  REP(age, N) {
    vector<int> nxt;
    for (int x : have) {
      auto it = S.lower_bound(x);
      if (it == S.begin())
        return false;
      --it;
      nxt.push_back(*it);
      S.erase(it);
    }
    for (int x : nxt)
      have.insert(x);
  }
  return true;
}

int main2() {
  S.clear();
  have.clear();
  int N = nextInt();
  int T = 1 << N;
  REP(i, T) A[i] = nextInt();
  sort(A, A + T);

  REP(i, T - 1) S.insert(A[i]);
  have.insert(A[T - 1]);

  cout << (solve(N, S, have) ? "Yes" : "No") << endl;

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}


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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
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 nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = (1 << 18) + 10;
int A[MAX_N];
multiset<int> S, have;

bool solve(int N, multiset<int> &S, multiset<int> &have) {
  REP(age, N) {
    vector<int> nxt;
    for (int x : have) {
      auto it = S.lower_bound(x);
      if (it == S.begin())
        return false;
      --it;
      nxt.push_back(*it);
      S.erase(it);
    }
    for (int x : nxt)
      have.insert(x);
  }
  return true;
}

int main2() {
  S.clear();
  have.clear();
  int N = nextInt();
  int T = 1 << N;
  REP(i, T) A[i] = nextInt();
  sort(A, A + T);

  REP(i, T - 1) S.insert(A[i]);
  have.insert(A[T - 1]);

  cout << (solve(N, S, have) ? "Yes" : "No") << endl;

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}
Problem p02920 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, C, L, R;
string S, T;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

typedef vector<P> vp;
typedef vector<vp> matP;

int main() {
  cin >> N;
  multiset<ll> goal;
  rep(j, (1LL << N)) {
    cin >> A;
    goal.insert(A);
  }
  queue<ll> now;
  auto big = goal.end();
  --big;
  now.push(*big);
  goal.erase(big);
  rep(i, N) {
    rep(j, (1LL << i)) {
      ll sli = now.front();
      now.pop();
      now.push(sli);
      auto son = lower_bound(goal.begin(), goal.end(), sli);
      /*cout<<i<<j<<endl;
      for(ll c : goal) cout<<c;
      cout<<endl; */
      if (son == goal.begin()) {
        cout << "No" << endl;
        return 0;
      }
      --son;
      now.push(*son);
      goal.erase(son);
    }
  }
  cout << "Yes" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, C, L, R;
string S, T;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

typedef vector<P> vp;
typedef vector<vp> matP;

int main() {
  cin >> N;
  multiset<ll> goal;
  rep(j, (1LL << N)) {
    cin >> A;
    goal.insert(A);
  }
  queue<ll> now;
  auto big = goal.end();
  --big;
  now.push(*big);
  goal.erase(big);
  rep(i, N) {
    rep(j, (1LL << i)) {
      ll sli = now.front();
      now.pop();
      now.push(sli);
      auto son = goal.lower_bound(sli);
      /*cout<<i<<j<<endl;
      for(ll c : goal) cout<<c;
      cout<<endl; */
      if (son == goal.begin()) {
        cout << "No" << endl;
        return 0;
      }
      --son;
      now.push(*son);
      goal.erase(son);
    }
  }
  cout << "Yes" << endl;
}
Problem p02920 (C++)

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

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> S(1UL << N);
  copy_n(istream_iterator<unsigned long>(cin), 1UL << N, S.begin());
  sort(S.rbegin(), S.rend());
  for (unsigned long i = 1; i < 1UL << N; ++i)
    if (S[i] == S[i - (1UL << (63 - __builtin_clzl(i)))])
      abort(); // return 0 & puts("No");
  puts("Yes");
  return 0;
}

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

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> S(1UL << N);
  copy_n(istream_iterator<unsigned long>(cin), 1UL << N, S.begin());
  sort(S.rbegin(), S.rend());
  vector<unsigned long> T{S[0]};
  list<unsigned long> l(S.begin() + 1, S.end());
  for (unsigned long i = 0, cnt; i < N; ++i) {
    cnt = 0;
    auto it = l.begin();
    while (it != l.end() && (cnt < 1UL << i)) {
      auto t = *it;
      if (T[cnt] > t) {
        T.push_back(t);
        it = l.erase(it);
        ++cnt;
      } else
        ++it;
    }
    if (cnt < 1UL << i)
      return 0 & puts("No");
    sort(T.rbegin(), T.rend());
  }
  puts("Yes");
  return 0;
}
Problem p02920 (C++)

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

template <typename A> string to_string(multiset<A> v) {
  bool first = true;
  string result = "[";
  for (A i : v) {
    if (!first)
      result += ", ";
    first = false;
    result += to_string(i);
  }
  return result + "]";
}

int main() {
  int n;
  cin >> n;
  multiset<int> slimes;

  for (int i = 0; i < pow(2, n); ++i) {
    int slime;
    cin >> slime;
    slimes.insert(slime);
  }
  cout << endl;
  multiset<int> created;

  ll firstSlime = *(slimes.rbegin());
  created.insert(firstSlime);
  slimes.erase(firstSlime);
  for (int i = 0; i < n; ++i) {
    multiset<int> buffer;
    for (multiset<int>::reverse_iterator it = created.rbegin();
         it != created.rend(); ++it) {
      ll slime = *it;

      if (slimes.lower_bound(slime) == slimes.begin()) {
        cout << "No" << endl;
        return 0;
      }

      ll toCreate = *max_element(slimes.begin(), slimes.lower_bound(slime));
      buffer.insert(toCreate);
      slimes.erase(slimes.lower_bound(toCreate));
    }
    for (int s : buffer)
      created.insert(s);
  }

  cout << "Yes" << endl;
}

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

template <typename A> string to_string(multiset<A> v) {
  bool first = true;
  string result = "[";
  for (A i : v) {
    if (!first)
      result += ", ";
    first = false;
    result += to_string(i);
  }
  return result + "]";
}

int main() {
  int n;
  cin >> n;
  multiset<int> slimes;

  for (int i = 0; i < pow(2, n); ++i) {
    int slime;
    cin >> slime;
    slimes.insert(slime);
  }
  cout << endl;
  multiset<int> created;

  ll firstSlime = *(slimes.rbegin());
  created.insert(firstSlime);
  slimes.erase(firstSlime);
  for (int i = 0; i < n; ++i) {
    multiset<int> buffer;
    for (multiset<int>::reverse_iterator it = created.rbegin();
         it != created.rend(); ++it) {
      ll slime = *it;

      if (slimes.lower_bound(slime) == slimes.begin()) {
        cout << "No" << endl;
        return 0;
      }

      ll toCreate = *(--slimes.lower_bound(slime));
      buffer.insert(toCreate);
      slimes.erase(slimes.lower_bound(toCreate));
    }
    for (int s : buffer)
      created.insert(s);
  }

  cout << "Yes" << endl;
}
Problem p02920 (C++)

Buggy submission (status: Time Limit Exceeded):
#define rep(i, m, n) for (int i = m; i < n; i++)
#define repM(i, m, n) for (int i = n - 1; i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
// typedef pair<int,int> pii;
//  d = distance(lower_bound(all(x)), upper_bound(all(x)));
int INF = 1e9;
int M = 1e9 + 7;
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

int main() {
  int N;
  cin >> N;
  int K = pow(2, N);
  int b;
  multiset<int> p;
  for (int i = 0; i < K; i++) {
    cin >> b;
    p.insert(b);
  }
  auto r = p.end();
  r--;
  vector<int> v;
  v.push_back(*r);
  p.erase(r);
  // int mx = *min_element(p.begin(), p.end());
  // vector<int> v; v.push_back(mx); p.erase(mx);

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < pow(2, i - 1); j++) {
      r = lower_bound(p.begin(), p.end(), v[j]);
      // int r = distance(p.begin(), lower_bound(p.begin(), p.end(), v[j]));
      // cout << *r << " ";
      if (r == p.begin()) {
        cout << "No" << endl;
        return 0;
      } else {
        v.push_back(*(--r));
        p.erase(r);
      }
    }
  }
  cout << "Yes" << endl;
}

Fixed submission:
#define rep(i, m, n) for (int i = m; i < n; i++)
#define repM(i, m, n) for (int i = n - 1; i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
// typedef pair<int,int> pii;
//  d = distance(lower_bound(all(x)), upper_bound(all(x)));
int INF = 1e9;
int M = 1e9 + 7;
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

int main() {
  int N;
  cin >> N;
  int K = pow(2, N);
  int b;
  multiset<int> p;
  for (int i = 0; i < K; i++) {
    cin >> b;
    p.insert(b);
  }
  auto r = p.end();
  r--;
  vector<int> v;
  v.push_back(*r);
  p.erase(r);
  // int mx = *min_element(p.begin(), p.end());
  // vector<int> v; v.push_back(mx); p.erase(mx);

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < pow(2, i - 1); j++) {
      r = p.lower_bound(v[j]);
      // int r = distance(p.begin(), lower_bound(p.begin(), p.end(), v[j]));
      // cout << *r << " ";
      if (r == p.begin()) {
        cout << "No" << endl;
        return 0;
      } else {
        v.push_back(*(--r));
        p.erase(r);
      }
    }
  }
  cout << "Yes" << endl;
}
Problem p02920 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = [int(i) for i in input().split()]
used = [0] * (2**n)
used[0] = 1
s.sort(reverse=True)
a = 1
b = [s[0]]
for _ in range(n):
    c = b.copy()
    j = 0
    for i in b:
        while j < 2**n:
            if used[j] == 0 and s[j] < i:
                c.append(s[j])
                used[j] = 1
                break
            j += 1
        if j >= 2**n:
            print("No")
            exit()
    b = sorted(c, reverse=True)

print("Yes")


Fixed submission:
n = int(input())
s = [int(i) for i in input().split()]
used = [0] * (2**n)
used[0] = 1
s.sort(reverse=True)
a = 1
b = [s[0]]
for _ in range(n):
    c = b[:]
    j = 0
    for i in b:
        while j < 2**n:
            if used[j] == 0 and s[j] < i:
                c.append(s[j])
                used[j] = 1
                break
            j += 1
        if j >= 2**n:
            print("No")
            exit()
    b = sorted(c, reverse=True)

print("Yes")
Problem p02920 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a, b) memset(a, b, sizeof(a))
using namespace std;
set<P<int, int>> s;
V<int> v, p;
int a[300000];
int main() {
  int n;
  cin >> n;
  int m = (1 << n);
  FOR(i, 1, m) cin >> a[i];
  sort(a + 1, a + m + 1, greater<int>());
  FOR(i, 2, m) { s.insert(MP(a[i], i)); }
  v.PB(a[1]);
  rep(t, 0, n) {
    p.clear();
    for (auto u : v) {
      auto iter = s.lower_bound(MP(u, 0));
      iter--;
      if ((*iter).F >= u) {
        cout << "No";
        RE 0;
      }
      p.PB((*iter).F);
      s.erase(iter);
    }
    for (auto u : p)
      v.PB(u);
  }
  cout << "Yes";
  RE 0;
}


Fixed submission:
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a, b) memset(a, b, sizeof(a))
using namespace std;
set<P<int, int>> s;
V<int> v, p;
int a[300000];
int main() {
  int n;
  cin >> n;
  int m = (1 << n);
  FOR(i, 1, m) cin >> a[i];
  sort(a + 1, a + m + 1, greater<int>());
  FOR(i, 2, m) { s.insert(MP(a[i], i)); }
  v.PB(a[1]);
  rep(t, 0, n) {
    p.clear();
    for (auto u : v) {
      auto iter = s.lower_bound(MP(u, 0));
      if (iter == s.begin()) {
        cout << "No";
        RE 0;
      }
      iter--;
      if ((*iter).F >= u) {
        cout << "No";
        RE 0;
      }
      p.PB((*iter).F);
      s.erase(iter);
    }
    for (auto u : p)
      v.PB(u);
  }
  cout << "Yes";
  RE 0;
}
Problem p02920 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  REP(i, 1 << n) cin >> s[i];

  int mx = 0;
  multiset<int> st1, st2;
  REP(i, 1 << n) {
    st1.insert(-s[i]);
    mx = max(mx, s[i]);
  }
  st1.erase(st1.find(-mx));
  st2.insert(-mx);
  REP(i, n) {
    multiset<int> st;
    for (int x : st2) {
      auto iter = st1.upper_bound(x);
      if (iter == st1.end()) {
        cout << "No\n";
        return 0;
      }
      st.insert(*iter);
      st1.erase(iter);
    }
    for (int x : st)
      st2.insert(x);
  }

  cout << "Yes\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

int main() {
  int n;
  cin >> n;
  vector<int> s(1 << n);
  REP(i, 1 << n) cin >> s[i];

  int mx = 0;
  multiset<int> st1, st2;
  REP(i, 1 << n) {
    st1.insert(-s[i]);
    mx = max(mx, s[i]);
  }
  st1.erase(st1.find(-mx));
  st2.insert(-mx);
  REP(i, n) {
    multiset<int> st;
    for (int x : st2) {
      auto iter = st1.upper_bound(x);
      if (iter == st1.end()) {
        cout << "No\n";
        return 0;
      }
      st.insert(*iter);
      st1.erase(iter);
    }
    for (int x : st)
      st2.insert(x);
  }

  cout << "Yes\n";
  return 0;
}
Problem p02920 (C++)

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

int main() {
  int n;
  cin >> n;
  const int N = 1 << n;
  multiset<int> st1;
  for (int i = 0; i < N; ++i) {
    int s;
    cin >> s;
    st1.insert(s);
  }
  int maxi = *st1.rbegin();
  st1.erase(st1.find(maxi));
  multiset<int> st2;
  st2.insert(maxi);
  while (st1.size()) {
    multiset<int> tmp;
    for (auto i = st2.rbegin(); i != st2.rend(); ++i) {
      int s = *i;
      auto itr = lower_bound(st1.begin(), st1.end(), s);
      if (itr == st1.begin()) {
        cout << "No\n";
        return 0;
      }
      itr = prev(itr);
      tmp.insert(*itr);
      st1.erase(itr);
    }
    for (int i : tmp) {
      st2.insert(i);
    }
  }
  cout << "Yes\n";
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  const int N = 1 << n;
  multiset<int> st1;
  for (int i = 0; i < N; ++i) {
    int s;
    cin >> s;
    st1.insert(s);
  }
  int maxi = *st1.rbegin();
  st1.erase(st1.find(maxi));
  multiset<int> st2;
  st2.insert(maxi);
  while (st1.size()) {
    multiset<int> tmp;
    for (auto i = st2.rbegin(); i != st2.rend(); ++i) {
      int s = *i;
      auto itr = st1.lower_bound(s);
      if (itr == st1.begin()) {
        cout << "No\n";
        return 0;
      }
      itr = prev(itr);
      tmp.insert(*itr);
      st1.erase(itr);
    }
    for (int i : tmp) {
      st2.insert(i);
    }
  }
  cout << "Yes\n";
  return 0;
}