alexjercan/bugnet huggingface dataset:
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LL long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

LL N;
LL V[2016];
LL mem[3][2016][2016];

LL cal(int n, int p, int pp) {
  LL res = 0;
  if (mem[n][p][pp] != 0)
    return mem[n][p][pp];
  if (p - 1 == pp || p == 0 && pp == N - 1) {
    return 0;
  }
  if (n == 0) {
    rep(i, N) {
      int next1 = i + 1, next2 = i - 1;
      if (i + 1 == N)
        next1 = 0;
      if (i - 1 < 0)
        next2 = N - 1;
      res = max(cal(2, next1, next2) + V[i], res);
    }
  } else if (n == 2) {
    LL MAX;
    LL MAX_i;
    if (V[p] < V[pp]) {
      MAX = V[pp];
      MAX_i = pp - 1;
      if (pp - 1 < 0)
        MAX_i = N - 1;
      res = max(cal(1, p, MAX_i), res);
    } else {
      MAX = V[p];
      MAX_i = p + 1;
      if (p + 1 >= N)
        MAX_i = 0;
      res = max(cal(1, MAX_i, pp), res);
    }
  } else if (n == 1) {
    int next1 = p + 1, next2 = pp - 1;
    if (p + 1 == N)
      next1 = 0;
    if (pp - 1 < 0)
      next2 = N - 1;
    res = max(cal(2, next1, pp) + V[p], max(cal(2, p, next2) + V[pp], res));
  }
  return mem[n][p][pp] = res;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> V[i]; }
  cout << cal(0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

LL N;
LL V[2016];

LL cal(int n, int p, int pp) {
  LL res = 0;
  if (n == N)
    return 0;
  if (n == 0) {
    rep(i, N) {
      int next1 = i + 1, next2 = i - 1;
      if (i + 1 == N)
        next1 = 0;
      if (i - 1 < 0)
        next2 = N - 1;
      res = max(cal(n + 1, next1, next2) + V[i], res);
    }
  } else if (n % 2 != 0) {
    LL MAX;
    LL MAX_i;
    // cout<<V[p]<<" "<<V[pp]<<" IOI"<<endl;
    if (V[p] < V[pp]) {
      MAX = V[pp];
      MAX_i = pp - 1;
      if (pp - 1 < 0)
        MAX_i = N - 1;
      res = max(cal(n + 1, p, MAX_i), res);
    } else {
      MAX = V[p];
      MAX_i = p + 1;
      if (p + 1 >= N)
        MAX_i = 0;
      res = max(cal(n + 1, MAX_i, pp), res);
    }
  } else if (n % 2 == 0) {
    // cout<<V[p]<<" "<<V[pp]<<" JOI"<<endl;
    int next1 = p + 1, next2 = pp - 1;
    if (p + 1 == N)
      next1 = 0;
    if (pp - 1 < 0)
      next2 = N - 1;
    res = max(cal(n + 1, next1, pp) + V[p],
              max(cal(n + 1, p, next2) + V[pp], res));
  }
  return res;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> V[i]; }
  cout << cal(0, 0, 0) << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;
#define INF 1000000000000

long long dp[2010][2010];
long long a[2010];

int main() {
  int N;
  long long hog, res;
  bool fla;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  if (N == 1) {
    cout << a[0] << endl;
    return 0;
  }
  dp[0][N - 1] = a[N - 1];
  for (int i = 1; i < N; i++) {
    dp[i][N - 1] = a[i - 1];
  }
  fla = false;
  for (int i = N - 2; i >= 1; i--) {
    if (fla) {
      dp[0][i] = max(dp[0][i + 1] + a[i], dp[N - 1][i + 1] + a[N - 1]);
      for (int j = 1; j < N; j++) {
        dp[j][i] =
            max(dp[j][i + 1] + a[(j + i) % N], dp[j - 1][i + 1] + a[j - 1]);
      }
    } else {
      hog = -INF;
      if (a[0] < a[i]) {
        hog = dp[0][i + 1];
      }
      if (a[N - 1] > a[i - 1]) {
        hog = max(hog, dp[N - 1][i + 1]);
      }
      dp[0][i] = hog;
      for (int j = 1; j < N; j++) {
        hog = -INF;
        if (a[j] < a[(i + j) % N]) {
          hog = dp[j][i + 1];
        }
        if (a[j - 1] > a[(i + j - 1) % N]) {
          hog = max(hog, dp[j - 1][i + 1]);
        }
        dp[j][i] = hog;
      }
    }
    fla = !fla;
  }
  res = -INF;
  /*
  for(int i=0;i<N;i++){
          for(int j=1;j<N;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }
  */
  if (fla) {
    for (int i = 0; i < N; i++) {
      res = max(res, dp[i][1] + a[i]);
    }
  } else {
    for (int i = 0; i < N; i++) {
      res = max(res, dp[i][1]);
    }
  }
  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

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

long long dp[2010][2010];
long a[2010];

int main() {
  int N;
  long long hog, res;
  bool fla;
  cin >> N;
  for (int i = 0; i < N; i++) {
    memset(dp[i], 0, sizeof(dp));
  }
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (i != 0)
      dp[i][N - 1] = a[i - 1];
    else
      dp[i][N - 1] = a[N - 1];
  }
  fla = false;
  for (int i = N - 2; i >= 0; i--) {
    if (fla) {
      for (int j = 0; j < N; j++) {
        if (j != 0)
          dp[j][i] =
              max(dp[j][i + 1] + a[(j + i) % N], dp[j - 1][i + 1] + a[j - 1]);
        else
          dp[j][i] =
              max(dp[j][i + 1] + a[(j + i) % N], dp[N - 1][i + 1] + a[N - 1]);
      }
    } else {
      for (int j = 0; j < N; j++) {
        if (j != 0) {
          if (a[(j + i) % N] > a[j - 1]) {
            dp[j][i] = dp[j][i + 1];
          } else {
            dp[j][i] = dp[j - 1][i + 1];
          }
        } else {
          if (a[(j + i) % N] > a[N - 1]) {
            dp[j][i] = dp[j][i + 1];
          } else {
            dp[j][i] = dp[N - 1][i + 1];
          }
        }
      }
    }
    fla = !fla;
  }
  res = 0;
  for (int i = 0; i < N; i++) {
    res = max(res, dp[i][0]);
  }
  cout << res << endl;
  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define int long long

int N;
int A[6000];

int memo[4000][6000];
int dfs(int l, int r) {
  if (~memo[l][r])
    return memo[l][r];

  int ret;
  int cnt = r - l - 1;

  if (cnt == N)
    ret = 0;
  else if (cnt & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }

  return memo[l][r] = ret;
}

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

  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[N + i] = A[N * 2 + i] = a;
  }

  memset(memo, -1, sizeof(memo));

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);

  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

#define int long long

int N;
int A[6000];

int memo[6000][6000];
int dfs(int l, int r) {
  if (~memo[l][r])
    return memo[l][r];

  int ret;
  int cnt = r - l - 1;

  if (cnt == N)
    ret = 0;
  else if (cnt & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }

  return memo[l][r] = ret;
}

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

  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[N + i] = A[N * 2 + i] = a;
  }

  memset(memo, -1, sizeof(memo));

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);

  cout << ans << endl;
}
Problem p00539 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;

LL n, m, c;
LL a[200010], b[200010], d[200010];

VP road[100010];
LL dist[100010];
PLL dist_road[200010];
priority_queue<PLL, VP, greater<PLL>> Q;

LL last = 0, ans = INF;
LL last_dist = 0;

void dijk() {
  memset(dist, -1, sizeof dist);
  Q.push(MP(0, 1));

  while (!Q.empty()) {
    PLL now = Q.top();
    Q.pop();

    if (dist[now.second] == -1) {
      dist[now.second] = now.first;

      for (LL i = 0; i < road[now.second].size(); i++)
        Q.push(MP(now.first + road[now.second][i].first,
                  road[now.second][i].second));
    }
  }
  return;
}

int main() {
  scanf("%lld%lld%lld", &n, &m, &c);
  for (LL i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
    road[a[i]].PB(MP(d[i], b[i]));
    road[b[i]].PB(MP(d[i], a[i]));
  }

  dijk();

  for (LL i = 0; i < m; i++)
    dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);

  sort(dist_road, dist_road + m);

  for (LL i = 0; i < m; i++)
    last += d[i];
  ans = last;

  for (LL i = 0; i < m; i++) {
    last += c * (dist_road[i].first - last_dist);
    last_dist = dist_road[i].first;
    last -= d[dist_road[i].second];

    ans = min(ans, last);
  }

  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;

LL n, m, c;
LL a[200010], b[200010], d[200010];

VP road[100010];
LL dist[100010];
PLL dist_road[100010];
priority_queue<PLL, VP, greater<PLL>> Q;

LL last = 0, ans = INF;
LL last_dist = 0;

void dijk() {
  memset(dist, -1, sizeof dist);
  Q.push(MP(0, 1));

  while (!Q.empty()) {
    PLL now = Q.top();
    Q.pop();

    if (dist[now.second] == -1) {
      dist[now.second] = now.first;

      for (LL i = 0; i < road[now.second].size(); i++)
        Q.push(MP(now.first + road[now.second][i].first,
                  road[now.second][i].second));
    }
  }
  return;
}

int main() {
  scanf("%lld%lld%lld", &n, &m, &c);
  for (LL i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
    road[a[i]].PB(MP(d[i], b[i]));
    road[b[i]].PB(MP(d[i], a[i]));
  }

  dijk();

  for (LL i = 0; i < m; i++)
    dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);

  sort(dist_road, dist_road + m);

  for (LL i = 0; i < m; i++)
    last += d[i];
  ans = last;

  for (LL i = 0; i < m; i++) {
    last += c * (dist_road[i].first - last_dist);
    last_dist = dist_road[i].first;
    last -= d[dist_road[i].second];

    ans = min(ans, last);
  }

  printf("%lld\\n", ans);
  return 0;
}
Problem p00539 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define P pair<int, int> // cost to
using namespace std;

map<int, int> S;
int mincost[200000];
vector<P> rinsetu[200000];
signed main() {
  int sum = 0;
  int a, b, c;
  cin >> a >> b >> c;
  for (int d = 0; d < b; d++) {
    int e, f, g;
    scanf("%lld%lld%lld", &e, &f, &g);
    sum += g;
    e--;
    f--;
    rinsetu[e].push_back(P(g, f));
    rinsetu[f].push_back(P(g, e));
  }
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  fill(mincost, mincost + a, LLONG_MAX / 3);
  mincost[0] = 0;
  while (Q.size()) {
    P o = Q.top();
    Q.pop();
    if (o.first > mincost[o.second])
      continue;
    for (P t : rinsetu[o.second]) {
      if (mincost[t.second] > o.first + t.first) {
        mincost[t.second] = o.first + t.first;
        Q.push(P(mincost[t.second], t.second));
      }
    }
  }
  for (int i = 0; i < b; i++) {
    for (P j : rinsetu[i]) {
      if (i < j.second)
        S[max(mincost[i], mincost[j.second])] += j.first;
    }
  }
  int MIN = sum;
  for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
    (*p).second += (*t).second;
  }
  for (auto i = S.begin(); i != S.end(); i++) {
    MIN = min(MIN, (*i).first * c + sum - (*i).second);
  }
  cout << MIN << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define P pair<int, int> // cost to
using namespace std;

map<int, int> S;
int mincost[100000];
vector<P> rinsetu[100000];
signed main() {
  int sum = 0;
  int a, b, c;
  cin >> a >> b >> c;
  for (int d = 0; d < b; d++) {
    int e, f, g;
    scanf("%lld%lld%lld", &e, &f, &g);
    sum += g;
    e--;
    f--;
    rinsetu[e].push_back(P(g, f));
    rinsetu[f].push_back(P(g, e));
  }
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  fill(mincost, mincost + a, LLONG_MAX / 3);
  mincost[0] = 0;
  while (Q.size()) {
    P o = Q.top();
    Q.pop();
    if (o.first > mincost[o.second])
      continue;
    for (P t : rinsetu[o.second]) {
      if (mincost[t.second] > o.first + t.first) {
        mincost[t.second] = o.first + t.first;
        Q.push(P(mincost[t.second], t.second));
      }
    }
  }
  for (int i = 0; i < b; i++) {
    for (P j : rinsetu[i]) {
      if (i < j.second)
        S[max(mincost[i], mincost[j.second])] += j.first;
    }
  }
  int MIN = sum;
  for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
    (*p).second += (*t).second;
  }
  for (auto i = S.begin(); i != S.end(); i++) {
    MIN = min(MIN, (*i).first * c + sum - (*i).second);
  }
  cout << MIN << endl;
}
Problem p00539 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];

void dijkstra() {
  for (int i = 0; i < MAX; i++)
    d[i] = (1LL << 60);
  d[1] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 1));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    ll pos = p.second, cost = p.first;
    if (d[pos] < cost)
      continue;
    for (int i = 0; i < (int)G[pos].size(); i++) {
      edge e = G[pos][i];
      if (d[e.to] > cost + e.cost) {
        d[e.to] = cost + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ll sum = 0;
  scanf("%lld %lld %lld", &N, &M, &C);
  for (int i = 0; i < M; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    sum += c;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  dijkstra();
  map<ll, ll> mp;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      edge e = G[i][j];
      if (i < e.to)
        mp[max(d[i], d[e.to])] += e.cost;
    }
  }
  ll ans = sum;
  map<ll, ll>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    ll X = it->first, D = it->second;
    sum -= D;
    ans = min(ans, X * C + sum);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];

void dijkstra() {
  for (int i = 0; i < MAX; i++)
    d[i] = (1LL << 60);
  d[1] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 1));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    ll pos = p.second, cost = p.first;
    if (d[pos] < cost)
      continue;
    for (int i = 0; i < (int)G[pos].size(); i++) {
      edge e = G[pos][i];
      if (d[e.to] > cost + e.cost) {
        d[e.to] = cost + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ll sum = 0;
  scanf("%lld %lld %lld", &N, &M, &C);
  for (int i = 0; i < M; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    sum += c;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  dijkstra();
  map<ll, ll> mp;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      edge e = G[i][j];
      if (i < e.to)
        mp[max(d[i], d[e.to])] += e.cost;
    }
  }
  ll ans = sum;
  map<ll, ll>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    ll X = it->first, D = it->second;
    sum -= D;
    ans = min(ans, X * C + sum);
  }
  cout << ans << endl;
  return 0;
}
Problem p00540 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
int N, M, p, d, val[100009], a[100009], dp[150009];
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d %d", &d, &p), val[p - 1] = d;
  for (int i = 0; i < N - M; i++)
    scanf("%d", &a[i]);
  int l = 0, r = 1000000009;
  while (r - l > 1) {
    int m = (l + r) >> 1;
    p = 1;
    for (int i = 0; i < N - M; i++) {
      if (a[i] >= m)
        p++;
    }
    for (int i = 0; i < N; i++)
      dp[i] = (val[i] ? (val[i] >= m ? 0 : p) : 1);
    for (int i = 0; 2 * i < N; i++) {
      d = dp[i + N] = 0;
      for (int j = i * 3; j < i * 3 + 3; j++)
        dp[i + N] += dp[j], d = d > dp[j] ? d : dp[j];
      dp[i + N] -= d;
    }
    if (dp[N + (N >> 1) - 1] < p)
      l = m;
    else
      r = m;
  }
  printf("%d\\n", l);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
int N, M, p, d, val[100009], a[100009], dp[150009];
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d %d", &p, &d), val[p - 1] = d;
  for (int i = 0; i < N - M; i++)
    scanf("%d", &a[i]);
  int l = 0, r = 1000000009;
  while (r - l > 1) {
    int m = (l + r) >> 1;
    p = 1;
    for (int i = 0; i < N - M; i++) {
      if (a[i] <= m)
        p++;
    }
    for (int i = 0; i < N; i++)
      dp[i] = (val[i] ? (val[i] >= m ? 0 : p) : 1);
    for (int i = 0; 2 * i < N; i++) {
      d = dp[i + N] = 0;
      for (int j = i * 3; j < i * 3 + 3; j++)
        dp[i + N] += dp[j], d = d > dp[j] ? d : dp[j];
      dp[i + N] -= d;
    }
    if (dp[N + (N >> 1) - 1] < p)
      r = m;
    else
      l = m;
  }
  printf("%d\\n", r);
  return 0;
}
Problem p00540 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100000

int n;
int P[SIZE] = {0};
vector<int> D;

bool check(int k) {
  int dp[SIZE * 2] = {0};

  for (int i = 0; i < n; i++) {
    if (P[i] > 0) {
      if (P[i] >= k)
        dp[i] = 0;
      else
        dp[i] = INF;
    } else
      dp[i] = 1;
  }

  //[l,r)
  int l = 0, r = n, sum = 0;

  while (l + 1 < r) {

    sort(&dp[l], &dp[l] + 3);

    sum = dp[l] + dp[l + 1];

    if (sum >= INF)
      dp[r] = INF;
    else
      dp[r] = sum;

    l += 3;
    r++;
  }

  int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));

  if (dp[l] <= b) {
    return true;
  }

  return false;
}

int main() {
  int m, p, d;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &d, &p);
    P[p - 1] = d;
  }

  for (int i = 0; i < n - m; i++) {
    scanf("%d", &d);
    D.push_back(d);
  }

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

  //[l,r]
  int l = 1, r = 1000000000, mid;

  while (l < r) {
    mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  printf("%d\\n", l);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100 // 100000

int n;
int P[SIZE] = {0};
vector<int> D;

bool check(int k) {
  int dp[SIZE * 2] = {0};

  for (int i = 0; i < n; i++) {
    if (P[i] > 0) {
      if (P[i] >= k)
        dp[i] = 0;
      else
        dp[i] = INF;
    } else
      dp[i] = 1;
  }

  //[l,r)
  int l = 0, r = n, sum = 0;

  while (l + 1 < r) {

    sort(&dp[l], &dp[l] + 3);

    sum = dp[l] + dp[l + 1];

    if (sum >= INF)
      dp[r] = INF;
    else
      dp[r] = sum;

    l += 3;
    r++;
  }

  int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));

  if (dp[l] <= b) {
    return true;
  }

  return false;
}

int main() {
  int m, p, d;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &d, &p);
    P[p - 1] = d;
  }

  for (int i = 0; i < n - m; i++) {
    scanf("%d", &d);
    D.push_back(d);
  }

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

  //[l,r]
  int l = 1, r = 1000000000, mid;

  while (l < r) {
    mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  printf("%d\\n", l);

  return 0;
}
Problem p00540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

struct L {
  int left, mid, right;
  L() : left(-1), mid(-1), right(-1) {}
  L(int l, int m, int r) : left(l), mid(m), right(r) {}
};

int N, M;
int D[100010], P[100010];

int G;
L E[200010];

long long cost(int x) {
  if (E[x].left == -1)
    return E[x].mid;
  long long a = cost(E[x].left), b = cost(E[x].mid), c = cost(E[x].right);
  return a + b + c - max(a, max(b, c));
}

bool solve(int x) {
  for (int i = 0; i < G; i++) {
    if (E[i].left == -1)
      E[i].mid = 1;
  }
  for (int i = 0; i < M; i++) {
    if (D[i] >= x)
      E[P[i] - 1].mid = 0;
    else
      E[P[i] - 1].mid = INF;
  }

  int K = 0;
  for (int i = 0; i < N - M; i++)
    K += D[i + M] >= x;
  return cost(G - 1) <= K;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++)
    cin >> D[i] >> P[i];
  for (int i = 0; i < N - M; i++)
    cin >> D[i + M];

  queue<int> q;
  for (int i = 0; i < N; i++) {
    q.push(i);
    E[i] = L(-1, -1, -1);
  }

  G = N;
  while (q.size() >= 3) {
    int a = q.front();
    q.pop();
    int b = q.front();
    q.pop();
    int c = q.front();
    q.pop();
    E[G] = L(a, b, c);
    q.push(G);
    G += 1;
  }

  int lo = -1, hi = INF;
  while (hi != lo) {
    int mid = (lo + hi) / 2;
    if (solve(mid)) {
      if (solve(mid + 1))
        lo = mid + 1;
      else {
        hi = mid;
        break;
      }
    } else
      hi = mid - 1;
  }
  cout << hi << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

struct L {
  int left, mid, right;
  L() : left(-1), mid(-1), right(-1) {}
  L(int l, int m, int r) : left(l), mid(m), right(r) {}
};

int N, M;
int D[100010], P[100010];

int G;
L E[200010];

int cost(L x) {
  if (x.left == -1) {
    if (x.mid == 1)
      return 0;
    if (x.mid == 0)
      return INF;
    if (x.mid == -1)
      return 1;
  }
  int a = cost(E[x.left]), b = cost(E[x.mid]), c = cost(E[x.right]);
  return a + b + c - max(a, max(b, c));
}

bool solve(int x) {
  for (int i = 0; i < G; i++)
    E[i].mid = -1;
  for (int i = 0; i < M; i++)
    E[P[i] - 1].mid = D[i] >= x;

  int K = 0;
  for (int i = 0; i < N - M; i++)
    K += D[i + M] >= x;
  return K >= cost(E[G - 1]);
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++)
    cin >> D[i] >> P[i];
  for (int i = 0; i < N - M; i++)
    cin >> D[i + M];

  queue<int> q;
  for (int i = 0; i < N; i++) {
    q.push(i);
    E[i] = L(-1, -1, -1);
  }

  G = N;
  while (q.size() >= 3) {
    int a = q.front();
    q.pop();
    int b = q.front();
    q.pop();
    int c = q.front();
    q.pop();
    E[G] = L(a, b, c);
    q.push(G);
    G += 1;
  }

  int lo = -1, hi = INF;
  while (hi != lo) {
    int mid = (lo + hi) / 2;
    if (solve(mid)) {
      if (solve(mid + 1))
        lo = mid + 1;
      else {
        hi = mid;
        break;
      }
    } else
      hi = mid - 1;
  }
  cout << hi << "\\n";
  return 0;
}