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

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < (int)N; i++)
#define p(S) cout << (S) << endl
using namespace std;
const int inf = 1e9;

int main() {
  int n;
  cin >> n;
  int dp[100010], effect[100010];
  REP(i, 0, n) cin >> effect[i];
  REP(i, 1, n) dp[i] = inf;
  queue<int> q;
  dp[0] = 0;
  q.push(0);
  // bfs
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    REP(i, 1, 7) {
      int next = now + i;
      if (next > n - 1)
        break;
      while (dp[next] == inf) {
        if (effect[next] == 0)
          q.push(next);
        dp[next] = dp[now] + 1;
        next += effect[next];
      }
    }
    if (dp[n - 1] < inf)
      break;
  }
  p(dp[n - 1]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < (int)N; i++)
#define p(S) cout << (S) << endl
using namespace std;
const int inf = 1e9;

int main() {
  int n;
  cin >> n;
  int dp[100010], effect[100010];
  REP(i, 0, n) cin >> effect[i];
  REP(i, 1, n) dp[i] = inf;
  queue<int> q;
  dp[0] = 0;
  q.push(0);
  // bfs
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    REP(i, 1, 7) {
      int next = now + i;
      if (next > n - 1)
        break;
      while (dp[next] == inf) {
        if (effect[next] == 0) {
          dp[next] = dp[now] + 1;
          q.push(next);
        }
        next += effect[next];
      }
    }
    if (dp[n - 1] < inf) {
      break;
    }
  }
  p(dp[n - 1]);
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  vector<int> p(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i];

  vector<int> minCost(n, INT_MAX);
  minCost[0] = 0;
  multimap<int, int> mm;
  mm.insert(make_pair(0, 0));
  while (!mm.empty()) {
    int cost = mm.begin()->first;
    int pos = mm.begin()->second;
    mm.erase(mm.begin());
    if (cost > minCost[pos])
      continue;

    if (p[pos] == 0) {
      for (int i = 1; i <= 6; ++i) {
        int next = pos + i;
        if (next >= n)
          break;
        if (cost + 1 < minCost[next]) {
          minCost[next] = cost + 1;
          mm.insert(make_pair(cost + 1, next));
        }
      }
    } else {
      int next = pos + p[pos];
      if (cost < minCost[next]) {
        minCost[next] = cost;
        mm.insert(make_pair(cost, next));
      }
    }
  }

  cout << minCost[n - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

int n;
vector<int> p;
vector<int> memo;

int solve(int a) {
  if (a >= n)
    return INF;
  if (memo[a] != -1)
    return memo[a];
  memo[a] = INF;

  int ret = -1;
  int b = a;
  while (p[b] != 0) {
    b += p[b];
    if (memo[b] != -1) {
      ret = memo[b];
      break;
    }
  };

  if (ret == -1) {
    ret = INF;
    for (int i = 1; i <= 6; ++i)
      ret = min(ret, solve(b + i) + 1);
  }

  b = a;
  do {
    memo[b] = ret;
    b += p[b];
  } while (p[b] != 0 && memo[b] == -1);

  return ret;
}

int main() {
  cin >> n;
  p.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i];

  memo.assign(n, -1);
  memo[n - 1] = 0;
  cout << solve(0) << endl;

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

#include <algorithm>
#include <iostream>
#define INF 1000000000
using namespace std;
int n, mp[100010], dp[100010];
bool visited[100010];

int mk_mp(int ima) {
  if (visited[ima] || mp[ima] == -INF)
    return -INF;
  visited[ima] = 1;
  if (mp[ima] != 0)
    mp[ima] = max(-INF, mp[ima] + mk_mp(ima + mp[ima]));
  if (mp[ima] < -10000000)
    mp[ima] = -INF;
  visited[ima] = 0;
  return mp[ima];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> mp[i];
  for (int i = 0; i < n; i++)
    mp[i] = mk_mp(i);
  for (int i = 1; i <= n; i++)
    dp[i] = -1;

  int cnt = 0;
  while (dp[n - 1] == -1) {
    cnt++;
    for (int i = 0; i < n; i++) {
      if (dp[i] != cnt - 1)
        continue;
      for (int j = 1; j <= 6; j++) {
        int nx = i + j;
        while (mp[nx] != -INF && mp[nx] != 0)
          nx += mp[nx];
        if (mp[nx] != -INF && dp[nx] == -1)
          dp[nx] = cnt;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 100000000
using namespace std;
int mp[100010], n;
bool used[100001];

int mk_mp(int ima) {
  if (used[ima] || mp[ima] < -1000000)
    return -INF;
  used[ima] = 1;
  int res = 0;
  if (mp[ima] != 0)
    res = mk_mp(ima + mp[ima]);
  used[ima] = 0;
  return mp[ima] + res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> mp[i];
  for (int i = 0; i < n; i++)
    mp[i] = mk_mp(i), used[i] = 1;
  int dp[100001] = {1}, ans = 0;
  n--;
  while (!dp[n]) {
    ans++;
    for (int i = n; i >= 0; i--) {
      for (int j = 1; j <= 6 && dp[i] == 1; j++) {
        int nx = i + j;
        while (mp[nx] > -1000000 && mp[nx] != 0)
          nx += mp[nx];
        if (mp[nx] < -1000000)
          continue;
        dp[min(n, nx)] = 1;
      }
      if (dp[i] != 0)
        dp[i]++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) { return cost < e.cost; }
  bool operator>(const edge &e) { return cost > e.cost; }
};

int n;
vector<edge> es[100011];
int main() {
  cin >> n;
  int masu[100011];
  REP(i, n) {
    int a;
    cin >> a;
    if (a == 0) {
      FOR(j, 1, 7) {
        edge e = {i, i + j, 1};
        es[i].push_back(e);
      }
    } else {
      edge e = {i, i + a, 0};
      es[i].push_back(e);
    }
  }
  int d[111111];
  fill(d, d + 111111, INF);
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, es[v].size()) {
      edge e = es[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  cout << d[n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) { return cost < e.cost; }
  bool operator>(const edge &e) { return cost > e.cost; }
};

int n;
vector<edge> es;
int main() {
  cin >> n;
  int masu[100011];
  REP(i, n) {
    int a;
    cin >> a;
    if (a == 0) {
      FOR(j, 1, 7) {
        edge e = {i, i + j, 1};
        es.push_back(e);
      }
    } else {
      edge e = {i, i + a, 0};
      es.push_back(e);
    }
  }
  int d[111111];
  fill(d, d + 111111, INF);
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, es.size()) {
      edge e = es[i];
      if (e.from != v)
        continue;
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  cout << d[n - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n;
int p[100005];
int m[100005];
/*
vector<int> v;
int dfs(int x){
  for(int i=0;i<v.size();i++) if(v[i]==x) return -2;
  v.push_back(x);
  if(p[x]==0) return (m[x]=x);
  if(m[x]!=-1) return m[x];
  return (m[x]=dfs(x+p[x]));
}
*/
int main() {
  cin >> n;
  int i, j, k;
  fill(p, p + 100005, 0);
  for (i = 0; i < n; i++)
    cin >> p[i];

  /*
  fill(m,m+100005,-1);
  for(i=0;i<n;i++){
    if(m[i]==-1){
      v.clear();
      m[i]=dfs(i);
    }
  }
  */

  int d[100005];
  int inf = 1 << 20;
  fill(d, d + 100005, inf);

  queue<P> q;
  P a;
  q.push(P(0, 0));
  while (!q.empty()) {
    a = q.front();
    q.pop();
    if (a.first < 0)
      continue;
    // cout << a.first << ":" << a.second << endl;
    if (d[a.first] <= a.second)
      continue;
    d[a.first] = a.second;
    if (p[a.first] == 0) {
      if (a.first + 6 > n) {
        d[n - 1] = min(d[n - 1], a.second + 1);
        continue;
      }
      for (i = 1; i <= 6; i++) {
        q.push(P(a.first + i, a.second + 1));
      }
    } else {
      q.push(P(a.first + p[a.first], a.second));
    }
  }

  cout << d[n - 1] << endl;
  // for(i=1;i<=n;i++) cout << m[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n;
int p[100005];
int m[100005];
vector<int> v;
int dfs(int x) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] == x)
      return -2;
  v.push_back(x);
  if (p[x] == 0)
    return (m[x] = x);
  if (m[x] != -1)
    return m[x];
  return (m[x] = dfs(x + p[x]));
}
int main() {
  cin >> n;
  int i, j, k;
  fill(p, p + 100005, 0);
  for (i = 0; i < n; i++)
    cin >> p[i];

  fill(m, m + 100005, -1);
  for (i = 0; i < n; i++) {
    if (m[i] == -1) {
      v.clear();
      m[i] = dfs(i);
    }
  }
  int d[100005];
  int inf = 1 << 20;
  fill(d, d + 100005, inf);

  queue<P> q;
  P a;
  q.push(P(0, 0));
  while (!q.empty()) {
    a = q.front();
    q.pop();
    if (a.first < 0)
      continue;
    // cout << a.first << ":" << a.second << endl;
    if (d[a.first] < a.second)
      continue;
    d[a.first] = a.second;
    if (a.first + 6 > n) {
      d[n - 1] = min(d[n - 1], a.second + 1);
      continue;
    }
    for (i = 1; i <= 6; i++) {
      q.push(P(m[a.first + i], a.second + 1));
    }
  }

  cout << d[n - 1] << endl;
  // for(i=1;i<=n;i++) cout << m[i] << endl;

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

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

const int INF = 1 << 29;

struct state {
  int pos;
  int time;

  state(int a, int b) : pos(a), time(b) {}

  bool operator<(const state &o) const {
    return time == o.time ? pos > o.pos : time > o.time;
  }
};

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

  vector<int> p(N);
  vector<int> visited(N, INF);
  visited[0] = 0;

  for (int x = 0; x < N; ++x) {
    cin >> p[x];
  }

  priority_queue<state> que;

  que.push(state(0, 0));

  while (!que.empty()) {
    state s = que.top();
    que.pop();

    if (s.time > visited[s.pos]) {
      continue;
    }

    if (s.pos >= N - 1) {
      break;
    }

    if (p[s.pos] == 0) {
      for (int x = 1; x <= 6; ++x) {
        int nxt = min(s.pos + x, N - 1);
        if (s.time + 1 < visited[nxt]) {
          visited[nxt] = s.time + 1;
          que.push(state(nxt, s.time + 1));
        }
      }
    } else {
      int nxt = s.pos + p[s.pos];
      if (s.time < visited[nxt]) {
        visited[nxt] = s.time;
        que.push(state(nxt, s.time));
      }
    }
  }

  cout << visited[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

const int MAX_N = 100000;

int N;
int p[MAX_N];
int mc[MAX_N];
int to[MAX_N];
int visited[MAX_N];

int dfs(int n) {
  visited[n] = 1;
  n += p[n];
  if (p[n] == 0)
    return n;
  if (to[n] >= 0)
    return to[n];
  if (visited[n] > 0)
    return -1;
  return to[n] = dfs(n);
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", p + i);
    mc[i] = INF;
    to[i] = -1;
  }
  queue<P> que;
  que.push(P(0, 0));
  mc[0] = 0;
  while (!que.empty()) {
    P st = que.front();
    que.pop();
    int m = st.first, cost = st.second;
    if (m >= N - 1)
      break;
    if (cost > mc[m])
      continue;
    for (int i = 1; i <= 6; i++) {
      if (m + i > N - 1)
        break;
      if (to[m + i] < 0) {
        fill(visited, visited + N, 0);
        to[m + i] = dfs(m + i);
      }
      if (to[m + i] >= 0) {
        if (cost + 1 < mc[to[m + i]]) {
          mc[to[m + i]] = cost + 1;
          que.push(P(to[m + i], cost + 1));
        }
      }
    }
  }

  printf("%d\\n", mc[N - 1]);
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define INF -1000000
#define F first
#define S second
using namespace std;

int N, imap[100010], MINE;
int dp[100010];
bool Init() {
  cin >> N;
  if (N == 0)
    return false;
  for (int i = 0; i < N; i++) {
    cin >> imap[i];
    dp[i] = 1 << 29;
  }
  for (int i = N; i < 100010; i++)
    dp[i] = 1 << 29;
  MINE = 1 << 29;
  return true;
}

int main() {
  // while(true){
  // if(!Init())break;
  Init();

  deque<int> que;
  que.push_front(0);
  dp[0] = 0;

  while (!que.empty()) {
    int p = que.front();
    que.pop_front();

    if (p >= N - 1) {
      cout << dp[p] << endl;
      break;
    }
    if (imap[p] != 0) {
      int np = p + imap[p];
      if (dp[np] <= dp[p])
        continue;
      dp[np] = dp[p];
      que.push_front(np); // 飛ばされてきた場合を優先する
      continue;
    }
    for (int i = 1; i <= 6; i++) {
      int np = p + i;
      if (dp[np] <= dp[p] + 1)
        continue;
      dp[np] = dp[p] + 1;
      que.push_back(np);
    }
  }
  // for(int i=1;i<N;i++)cout << "dp["<< i << "] = "  << dp[i] << endl;

  //}
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define INF -1000000
#define F first
#define S second
using namespace std;

int N, imap[100010], MINE;
int dp[100010];
bool Init() {
  cin >> N;
  if (N == 0)
    return false;
  for (int i = 0; i < N; i++) {
    cin >> imap[i];
    dp[i] = 1 << 29;
  }
  for (int i = N; i < 100010; i++)
    dp[i] = 1 << 29;
  MINE = 1 << 29;
  return true;
}

int main() {
  while (true) {
    if (!Init())
      break;

    deque<int> que;
    que.push_front(0);
    dp[0] = 0;

    while (!que.empty()) {
      int p = que.front();
      que.pop_front();

      if (p >= N - 1) {
        cout << dp[p] << endl;
        break;
      }
      if (imap[p] != 0) {
        int np = p + imap[p];
        if (dp[np] <= dp[p])
          continue;
        dp[np] = dp[p];
        que.push_front(np); // 飛ばされてきた場合を優先する
        continue;
      }
      for (int i = 1; i <= 6; i++) {
        int np = p + i;
        if (dp[np] <= dp[p] + 1)
          continue;
        dp[np] = dp[p] + 1;
        que.push_back(np);
      }
    }
    // for(int i=1;i<N;i++)cout << "dp["<< i << "] = "  << dp[i] << endl;
  }
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 28;
int N;

int create_next(vector<int> &next, vector<int> &p, int now) {
  if (!p[now])
    return next[now] = now;
  if (next[now] == -2)
    return next[now] = INF;
  if (next[now] > -1)
    return next[now];
  next[now] = -2;
  int now_ = now + p[now];
  if (now_ < 0)
    now_ = 0;
  else if (now_ >= N)
    now_ = N - 1;
  return next[now] = create_next(next, p, now_);
}

int main() {
  cin >> N;
  vector<int> p(N);
  for (auto &i : p)
    cin >> i;

  vector<int> dp(N, INF);
  dp[0] = 0;
  vector<int> next(N, -1);
  for (int i = 0; i < N; i++) {
    if (!p[i])
      next[i] = i;
    else
      create_next(next, p, i);
  }

  priority_queue<int, vector<int>, greater<int>> que;
  que.push(0);

  while (que.size()) {
    int now = que.top();
    que.pop();

    for (int i = 1; i <= 6; i++) {
      int nx = now + i;
      if (nx >= N)
        nx = N - 1;
      nx = next[nx];

      if (nx == INF)
        continue;
      if (dp[now] + 1 < dp[nx]) {
        dp[nx] = dp[now] + 1;
        que.push(nx);
      }
    }
  }

  cout << dp[N - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 28;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (auto &i : p)
    cin >> i;

  vector<int> dp(N, INF);
  dp[0] = 0;

  for (int i = 0; i < N; i++) {
    if (dp[i] == INF)
      continue;
    for (int j = 1; j <= 6; j++) {
      int next = i + j;
      if (next < 0)
        next = 0;
      if (next >= N)
        next = N - 1;
      vector<bool> flag(N, false);
      flag[next] = true;
      bool cnt = false;
      int next_ = next + p[next];
      if (next_ < 0)
        next_ = 0;
      if (next_ >= N)
        next_ = N - 1;
      while (next != next_) {
        next = next_;
        if (flag[next]) {
          cnt = true;
          break;
        }
        flag[next] = true;
        next_ = next + p[next];
        if (next_ < 0)
          next_ = 0;
        if (next_ >= N)
          next_ = N - 1;
      }
      if (cnt)
        continue;
      if (next < 0)
        next = 0;
      if (next >= N)
        next = N - 1;

      dp[next] = min(dp[next], dp[i] + 1);
    }
  }

  cout << dp[N - 1] << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
int main() {
  int n;
  cin >> n;
  int a[n + 10];
  bool ch[n + 100];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = n; i < n + 10; i++)
    a[i] = -1;
  for (int i = 0; i < n + 100; i++)
    ch[i] = 0;
  queue<pair<int, int>> q;
  q.push(mk(0, 0));
  ch[0] = 1;
  while (!q.empty()) {
    pair<int, int> b = q.front();
    q.pop();
    int d = b.first, c = b.second;
    if (d == n - 1) {
      cout << c << endl;
      return 0;
    }
    for (int i = 1; i < 7; i++) {
      if (a[d + i] + d + i >= n || ch[a[d + i] + d + i])
        continue;
      if (a[d + i]) {
        if ((a[d + i]) == -(a[d + i + a[d + i]])) {
          ch[d + i] = 1;
          goto L;
        }
        int p = a[d + i], dd = d + i;
        while (1) {
          if (a[dd + p] == 0)
            break;
          if (ch[dd]) {
            goto L;
          }
          ch[dd] = 1;
          dd = dd + p;
          p = a[dd];
        }
        q.push(mk(dd + p, c + 1));
        ch[dd + p] = 1;
      }
      if (a[d + i] == 0) {
        q.push(mk(d + i, c + 1));
        ch[d + i] = 1;
      }
    L:;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
int main() {
  int n;
  cin >> n;
  int a[n + 10];
  bool ch[n + 100];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = n; i < n + 10; i++)
    a[i] = -1;
  for (int i = 0; i < n + 100; i++)
    ch[i] = 0;
  queue<pair<int, int>> q;
  q.push(mk(0, 0));
  ch[0] = 1;
  while (!q.empty()) {
    pair<int, int> b = q.front();
    q.pop();
    int d = b.first, c = b.second;
    if (d == n - 1) {
      cout << c << endl;
      return 0;
    }
    for (int i = 1; i < 7; i++) {
      if (a[d + i] + d + i >= n || ch[a[d + i] + d + i])
        continue;
      if (a[d + i] > 0) {
        if ((a[d + i]) == -(a[d + i + a[d + i]])) {
          ch[d + i] = 1;
          goto L;
        }
        int p = a[d + i], dd = d + i;
        while (1) {
          if (a[dd + p] == 0)
            break;
          if ((a[dd]) == -(a[dd + a[dd]])) {
            ch[dd] = 1;
            goto L;
          }
          dd = dd + p;
          p = a[dd];
        }
        q.push(mk(dd + p, c + 1));
        ch[dd + p] = 1;
      }
      if (a[d + i] == 0) {
        q.push(mk(d + i, c + 1));
        ch[d + i] = 1;
      }
    L:;
    }
  }
}
Problem p01449 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int N, p[100000], v[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    v[i] = (1 << 24);
  }

  queue<int> q;
  q.push(0);
  v[0] = 0;

  while (!q.empty()) {
    int t = q.front();
    q.pop();

    if (t == N - 1) {
      cout << v[t] << endl;
      break;
    }

    for (int d = 1; d <= 6; d++) {
      int nt = t + d;
      if (nt >= N)
        continue;

      while (v[t] + 1 < v[nt]) {
        v[nt] = v[t] + 1;
        nt += p[nt];
      }
      if (p[nt] == 0 && v[nt] == v[t] + 1) {
        q.push(nt);
      }
    }
  }
}

Error: MLE

Fixed submission:

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

int main() {
  int N, p[100000], v[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    v[i] = (1 << 24);
  }

  queue<int> q;
  q.push(0);
  v[0] = 0;

  while (!q.empty()) {
    int t = q.front();
    q.pop();

    if (t == N - 1) {
      cout << v[t] << endl;
      break;
    }

    for (int d = 1; d <= 6; d++) {
      int nt = t + d;
      if (nt >= N)
        continue;

      while (v[t] + 1 < v[nt]) {
        v[nt] = v[t] + 1;
        nt += p[nt];
      }
      if (p[nt] == 0) {
        q.push(nt);
      }
    }
  }
}