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

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

vector<int> T;
vector<int> M;
vector<bool> check;
int N;
const int NOT = -2;

int calc(int idx) {
  if (T[idx] == NOT)
    return NOT;
  if (T[idx] >= 0)
    return T[idx];
  if (check[idx]) {
    T[idx] = NOT;
    return NOT;
  }
  check[idx] = true;
  if (M[idx] == 0) {
    T[idx] = idx;
    return idx;
  }
  return T[idx] = calc(idx + M[idx]);
}

class Node {
public:
  int idx, cost;
  Node(int idx, int cost) : idx(idx), cost(cost) {}
  Node();
};

bool operator<(Node a, Node b) { return a.cost > b.cost; }

int main() {
  cin >> N;
  T.resize(N, -1);
  check.resize(N, false);
  M.resize(N);
  for (int i = 0; i < N; i++)
    cin >> M[i];

  T[0] = 0;
  T[N - 1] = N - 1;
  for (int i = N - 1; i >= 1; i--) {
    if (!check[i])
      calc(i);
  }
  vector<int> ans(N, N + 1);
  priority_queue<Node> que;
  que.push(Node(0, 0));

  //	cout<<endl;
  int res = N + 1;
  while (!que.empty()) {
    Node now = que.top();
    que.pop();
    //		cout<<now.idx<<endl;
    if (T[now.idx] == NOT)
      continue;
    if (ans[now.idx] <= now.cost)
      continue;
    ans[now.idx] = now.cost;
    for (int i = 1; i <= 6; i++) {
      if (now.idx + i <= N - 1) {
        if (T[now.idx + i] >= 0)
          que.push(Node(T[now.idx + i], now.cost + 1));
      }
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> T;
vector<int> M;
vector<bool> check;
int N;
const int NOT = -2;

int calc(int idx) {
  if (T[idx] == NOT)
    return NOT;
  if (T[idx] >= 0)
    return T[idx];
  if (check[idx]) {
    T[idx] = NOT;
    return NOT;
  }
  check[idx] = true;
  if (M[idx] == 0) {
    T[idx] = idx;
    return idx;
  }
  return T[idx] = calc(idx + M[idx]);
}

class Node {
public:
  int idx, cost;
  Node(int idx, int cost) : idx(idx), cost(cost) {}
  Node();
};

bool operator<(Node a, Node b) { return a.cost < b.cost; }

int main() {
  cin >> N;
  T.resize(N, -1);
  check.resize(N, false);
  M.resize(N);
  for (int i = 0; i < N; i++)
    cin >> M[i];

  T[0] = 0;
  T[N - 1] = N - 1;
  for (int i = N - 1; i >= 1; i--) {
    if (!check[i])
      calc(i);
  }
  vector<int> ans(N, N + 1);
  priority_queue<Node> que;
  que.push(Node(0, 0));

  //	cout<<endl;
  int res = N + 1;
  while (!que.empty()) {
    Node now = que.top();
    que.pop();
    //		cout<<now.idx<<endl;
    if (T[now.idx] == NOT)
      continue;
    if (ans[now.idx] <= now.cost)
      continue;
    ans[now.idx] = now.cost;
    for (int i = 1; i <= 6; i++) {
      if (now.idx + i <= N - 1) {
        if (T[now.idx + i] >= 0)
          que.push(Node(T[now.idx + i], now.cost + 1));
      }
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

const int INF = 1 << 30;

vector<int> P, to;

int find(int v) {
  if (to[v] >= 0)
    return to[v];
  if (P[v] == 0)
    return to[v] = v;
  to[v] = INF;
  return to[v] = find(v + P[v]);
}

int main() {
  int N;
  cin >> N;
  P.resize(N);
  for (auto &p : P)
    cin >> p;

  to.assign(N, -1);
  for (int v = 0; v < N; ++v)
    find(v);

  vector<int> dist(N, INF);
  dist[0] = 0;
  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int d = 1; d <= 6 && d + v < N; ++d) {
      int sv = to[v + d];
      if (sv == INF || dist[sv] < INF)
        continue;
      dist[sv] = dist[v] + 1;
      que.push(sv);
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

const int INF = 1 << 30;

vector<int> P, to;

int find(int v) {
  if (P[v] == 0)
    return to[v] = v;
  if (to[v] > 0)
    return to[v];
  to[v] = INF;
  return to[v] = find(v + P[v]);
}

int main() {
  int N;
  cin >> N;
  P.resize(N);
  for (auto &p : P)
    cin >> p;

  to.assign(N, -1);
  for (int v = 0; v < N; ++v)
    find(v);

  vector<int> dp(N, INF);
  dp[0] = 0;
  for (int v = 0; v < N; ++v) {
    for (int d = 1; d <= 6; ++d) {
      int sv = to[v + d];
      if (sv < INF) {
        dp[sv] = min(dp[sv], dp[v] + 1);
      }
    }
  }

  cout << dp[N - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

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

#define FOR(i, s, t) for (int i = s; i < t; i++)
using VI = vector<int>;

int main(void) {
  int N;
  cin >> N;
  vector<int> a(N);
  FOR(i, 0, N) { cin >> a[i]; }
  VI m(N + 1, 1e9);
  queue<int> q;
  q.push(0);
  m[0] = 0;
  while (!q.empty()) {
    int pos = q.front();
    q.pop();
    if (a[pos] == 0) {
      FOR(k, 1, 6 + 1) {
        int nx = min(N - 1, pos + k);
        if (m[nx] > m[pos] + 1) {
          m[nx] = m[pos] + 1;
          q.push(nx);
        }
      }
    } else {
      int nx = pos + a[pos];
      if (m[nx] > m[pos]) {
        m[nx] = m[pos];
        q.push(nx);
      }
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000, MAX = 200000;

int N;
int p[MAX], to[MAX], dp[MAX];
set<int> roop;
vector<int> from[MAX];

int find(int n) {
  if (n < 0)
    return -2;
  if (to[n] != -1) {
    return to[n];
  }
  if (p[n] == 0) {
    from[n].push_back(n);
    return to[n] = n;
  }
  if (roop.count(n)) {
    return to[n] = -2;
  }
  roop.insert(n);
  int aaa = find(min(n + p[n], N - 1));
  if (aaa != -2)
    from[aaa].push_back(n);
  return to[n] = aaa;
}

int dfs(int n) {
  if (n < 0)
    return INF;
  if (dp[n] != INF) {
    return dp[n];
  }
  int mi = INF;
  for (auto f : from[n]) {
    for (int i = max(-f, -6); i < 0; i++) {
      if (p[f + i] == 0) {
        mi = min(mi, dfs(f + i));
      }
    }
  }
  return dp[n] = (mi == INF ? INF : mi + 1);
}

int main() {
  cin >> N;
  fill(to, to + MAX, -1);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < N; i++) {
    if (to[i] == -1) {
      if (p[i] == 0) {
        to[i] = i;
        from[i].push_back(i);
        continue;
      }
      roop.clear();
      roop.insert(i);
      int f = find(i + p[i]);
      if (f != -2)
        from[f].push_back(i);
      to[i] = f;
    }
  }
  fill(dp, dp + MAX, INF);
  dp[0] = 0;
  cout << dfs(N - 1) << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
#define pb push_back

int n;
vector<int> dat;

bool input() {
  cin >> n;

  rep(i, n) {
    int a;
    cin >> a;
    dat.pb(a);
  }

  return true;
}

const int N = 111111;

class P {
public:
  int pos, val;
  P(int pos, int val) : pos(pos), val(val) {}
  bool operator<(const P &a) const {
    if (val == a.val)
      return pos > a.pos;
    ;
    return val > a.val;
  }
};

typedef pair<int, int> pii;

vector<pii> edge[N];
int visit[N];
void solve() {

  rep(i, n) {
    if (dat[i] != 0) {
      edge[i].pb(pii(i + dat[i], 0));
    }
  }

  rep(i, n) {
    rep(j, 6) {
      if (dat[i] == 0) {
        if (i + j + 1 <= n - 1) {
          edge[i].pb(pii(i + j + 1, 1));
        }
      }
    }
  }

  priority_queue<P> que;
  que.push(P(0, 0));

  rep(i, N) visit[i] = 0;

  while (!que.empty()) {
    P u = que.top();
    que.pop();

    if (visit[u.pos] == 1)
      continue;
    visit[u.pos] = 1;

    // printf("pos = %d  val=%d\\n",u.pos, u.val);

    if (u.pos == n - 1) {
      cout << u.val << endl;
      break;
    }

    rep(i, edge[u.pos].size()) {
      que.push(P(edge[u.pos][i].first, u.val + edge[u.pos][i].second));
    }
  }
}

int main() {
  input();
  solve();
}

Error: 0

Fixed submission:

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

int N;
vector<int> p;
bool visited[10010];
bool is_loop[10010];
bool dijkstra_visited[10010];

struct edge {
  int to;
  int cost;
  edge(int t_, int c_) {
    to = t_;
    cost = c_;
  }
};

struct status {
  int amt;
  int v;
  status(int a_, int v_) {
    amt = a_;
    v = v_;
  }
  bool operator<(const status &obj) const { return this->amt > obj.amt; }
};

vector<vector<edge>> e;

bool loop(int n) {
  if (visited[n])
    return is_loop[n];
  visited[n] = true;
  is_loop[n] = true;
  if (p[n] == 0)
    return is_loop[n] = false;
  return is_loop[n] = loop(n + p[n]);
}

int main() {
  cin >> N;
  p.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < N; i++) {
    loop(i);
  }
  e.resize(N);
  for (int i = 0; i < N; i++) {
    if (p[i] == 0) {
      for (int j = 0; j < 6; j++) {
        if (!is_loop[i + j + 1]) {
          e[i].push_back(edge(i + j + 1, 1));
        }
      }
    } else {
      if (!is_loop[i]) {
        e[i].push_back(edge(i + p[i], 0));
      }
    }
  }

  priority_queue<status> q;
  q.push(status(0, 0));
  int res = 1e9;
  while (!q.empty()) {
    status cur = q.top();
    q.pop();
    if (dijkstra_visited[cur.v])
      continue;
    dijkstra_visited[cur.v] = true;
    if (cur.v == N - 1) {
      res = cur.amt;
      break;
    }
    for (int i = 0; i < e[cur.v].size(); i++) {
      if (!dijkstra_visited[e[cur.v][i].to]) {
        q.push(status(cur.amt + e[cur.v][i].cost, e[cur.v][i].to));
      }
    }
  }
  cout << res << endl;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int p[100000], to[100000];
set<int> used;

int find(int n) {
  if (to[n] != -1) {
    return to[n];
  }
  if (p[n] == 0) {
    return to[n] = n;
  }
  if (used.count(n)) {
    return to[n] = -2;
  }
  used.insert(n);
  return to[n] = find(n + p[n]);
}

int main() {
  int N;
  cin >> N;
  fill(to, to + 100000, -1);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < N; i++) {
    used.clear();
    find(i);
  }
  int res = 0;
  set<int> q;
  q.insert(0);
  used.clear();
  used.insert(0);
  while (!q.empty()) {
    res++;
    auto tmp = q;
    q.clear();
    for (auto p : tmp) {
      for (int i = 1; i <= 6; i++) {
        if (p + i >= N - 1 || to[p + i] == N - 1) {
          goto end;
        }
        if (to[p + i] != -2 && used.count(to[p + i]) == 0) {
          q.insert(to[p + i]);
          used.insert(to[p + i]);
        }
      }
    }
  }
end:
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int p[100000], to[100000];
set<int> used;

int find(int n) {
  if (to[n] != -1) {
    return to[n];
  }
  if (p[n] == 0) {
    return to[n] = n;
  }
  if (used.count(n)) {
    return to[n] = -2;
  }
  used.insert(n);
  return to[n] = find(n + p[n]);
}

int main() {
  int N;
  cin >> N;
  fill(to, to + 100000, -1);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < N; i++) {
    used.clear();
    find(i);
  }
  int res = 0;
  set<int> q;
  q.insert(0);
  while (!q.empty()) {
    res++;
    auto tmp = q;
    q.clear();
    for (auto p : tmp) {
      for (int i = 1; i <= 6; i++) {
        if (p + i >= N - 1 || to[p + i] == N - 1) {
          goto end;
        }
        if (to[p + i] != -2) {
          q.insert(to[p + i]);
        }
      }
    }
  }
end:
  cout << res << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
#define INF 999999999

int v[100000];
int memo[100000];
int N;
int ans;

void dfs(int pos, int step, int count, queue<int> &Que) {
  int next = pos + step;
  if (next >= N - 1) {
    ans = min(ans, count);
    return;
  }
  if (memo[next] <= count) {
    return;
  }
  memo[next] = count;
  if (v[next] == 0) {
    Que.push(next);
  } else {
    dfs(next, v[next], count, Que);
  }
}

void solve() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    memo[i] = INF;
  }
  ans = INF;
  memo[0] = 0;
  queue<int> Que;
  Que.push(0);
  while (!Que.empty()) {
    int pos = Que.front();
    Que.pop();
    for (int step = 1; step <= 6; ++step) {
      dfs(pos, step, memo[pos] + 1, Que);
    }
  }
  cout << ans << endl;
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void solve() {
  int N;
  cin >> N;
  vector<int> field(N);
  for (int i = 0; i < N; ++i) {
    cin >> field[i];
  }
  queue<int> Que;
  Que.push(0);
  int count = 1;
  bool loopField[100001] = {false};
  while (true) {
    queue<int> NextQue;
    while (!Que.empty()) {
      for (int i = 1; i <= 6; ++i) {
        int current = Que.front() + i;
        if (current >= N - 1) {
          goto END;
        }
        if (loopField[current]) {
          continue;
        }
        bool used[100001] = {false};
        bool loopFlag = false;
        while (field[current] != 0) {
          if (used[current]) {
            loopFlag = true;
            loopField[current] = true;
            break;
          }
          used[current] = true;
          current += field[current];
          if (current >= N - 1) {
            goto END;
          }
        }
        if (!loopFlag) {
          NextQue.push(current);
        }
      }
      Que.pop();
    }
    while (!NextQue.empty()) {
      Que.push(NextQue.front());
      NextQue.pop();
    }
    ++count;
  }
END:
  cout << count << endl;
}

int main() {
  solve();
  return (0);
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int N, mas[100001];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> mas[i];
  const int inf = (1 << 30);
  vector<int> cost(N, inf);
  typedef pair<int, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  cost[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.second >= N - 1) {
      cout << p.first << endl;
      break;
    }
    if (p.first > cost[p.second])
      continue;
    if (mas[p.second] == 0) {
      for (int i = 1; i <= 6; i++) {
        if (p.second + i >= 0 && p.second + i < N &&
            p.first + 1 < cost[p.second + i]) {
          cost[p.second + i] = p.first + 1;
          que.push(P(p.first + 1, p.second + i));
        }
      }
    } else {
      if (p.second + mas[p.second] >= 0 && p.second + mas[p.second] < N &&
          p.first < cost[p.second + mas[p.second]]) {
        cost[p.second + mas[p.second]] = p.first;
        que.push(P(p.first, p.second + mas[p.second]));
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int N, mas[100001];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> mas[i];
  const int inf = (1 << 30);
  vector<int> cost(N, inf);
  typedef pair<int, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  cost[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.second >= N - 1) {
      cout << p.first << endl;
      break;
    }
    if (p.first > cost[p.second])
      continue;
    if (mas[p.second] == 0) {
      for (int i = 1; i <= 6; i++) {
        if (p.first + 1 < cost[p.second + i]) {
          cost[p.second + i] = p.first + 1;
          que.push(P(p.first + 1, p.second + i));
        }
      }
    } else {
      if (p.first < cost[p.second + mas[p.second]]) {
        cost[p.second + mas[p.second]] = p.first;
        que.push(P(p.first, p.second + mas[p.second]));
      }
    }
  }
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int w[400], n, W, dp[20005];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  int sum = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++)
      dp[j] = 0;
    dp[cnt] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = W - w[j]; k >= cnt; k--) {
        dp[k + w[j]] += dp[k];
        dp[k + w[j]] %= 1000000007;
      }
    }
    for (int j = W - w[i] + 1; j <= W; j++) {
      sum += dp[j];
      sum %= 1000000007;
    }
    cnt += w[i];
    if (cnt > W)
      break;
  }
  if (cnt <= W)
    sum++;
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int w[400], n, W, dp[20005];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  int sum = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++)
      dp[j] = 0;
    dp[cnt] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = W - w[j]; k >= cnt; k--) {
        dp[k + w[j]] += dp[k];
        dp[k + w[j]] %= 1000000007;
      }
    }
    for (int j = W - w[i] + 1; j <= W; j++) {
      sum += dp[j];
      sum %= 1000000007;
    }
    cnt += w[i];
  }
  if (cnt <= W)
    sum++;
  cout << sum << endl;
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long n, w;
long long p = 1000000007;
long long memo[201][10001];
vector<int> fre, sum;
long long solve(int unuse, int leftw) {
  if (leftw < 0)
    return 0;
  if (sum[unuse] <= leftw)
    return 1;
  if (memo[unuse][leftw] != -1)
    return memo[unuse][leftw];
  memo[unuse][leftw] =
      (solve(unuse - 1, leftw) + solve(unuse - 1, leftw - fre[unuse - 1])) % p;
  return memo[unuse][leftw];
}
int main() {
  cin >> n >> w;
  for (int i = 0; i < 201; ++i)
    for (int j = 0; j < 10001; ++j)
      memo[i][j] = -1;
  fre.assign(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> fre[i];
  sort(fre.begin(), fre.end());
  sum.assign(n + 1, 0);
  for (int i = 0; i < n; ++i)
    sum[i + 1] = sum[i] + fre[i];
  cout << solve(n, w) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, w;
long long p = 1000000007;
long long memo[101][10001];
vector<int> fre, sum;
long long solve(int unuse, int leftw) {
  if (leftw < 0)
    return 0;
  if (sum[unuse] <= leftw || unuse == 0)
    return 1;
  if (memo[unuse][leftw] != -1)
    return memo[unuse][leftw];
  memo[unuse][leftw] =
      (solve(unuse - 1, leftw) + solve(unuse - 1, leftw - fre[unuse - 1])) % p;
  return memo[unuse][leftw];
}
int main() {
  cin >> n >> w;
  for (int i = 0; i < 101; ++i)
    for (int j = 0; j < 10001; ++j)
      memo[i][j] = -1;
  fre.assign(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> fre[i];
  sort(fre.begin(), fre.end());
  sum.assign(n + 1, 0);
  for (int i = 0; i < n; ++i)
    sum[i + 1] = sum[i] + fre[i];
  cout << solve(n, w) << endl;
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define All(A) A.begin(), A.end()
#define RAll(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<double, double> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const LL INF = 1 << 30;

int dp[100001][201];

int main() {
  int N;
  int W;
  cin >> N >> W;
  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(All(w));
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k <= i; k++) {
        if (dp[j][k] == 0)
          continue;
        if (k == 0) {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
          dp[j][i + 1] = (dp[j][i + 1] + dp[j][k]) % mod;
          dp[j][k] = 0;
        } else {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
        }
      }
    }
  }
  LL ans = 0;
  for (int i = 0; i <= W; i++) {
    for (int j = 0; j <= N; j++) {
      if (j == 0) {
        ans = (ans + dp[i][j]) % mod;
      } else {
        if (w[j - 1] > W - i) {
          ans = (ans + dp[i][j]) % mod;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define All(A) A.begin(), A.end()
#define RAll(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<double, double> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const LL INF = 1 << 30;

LL dp[100001][201];

int main() {
  int N;
  int W;
  cin >> N >> W;
  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(All(w));
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k <= i; k++) {
        if (dp[j][k] == 0)
          continue;
        if (k == 0) {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
          dp[j][i + 1] = (dp[j][i + 1] + dp[j][k]) % mod;
          dp[j][k] = 0;
        } else {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
        }
      }
    }
  }
  LL ans = 0;
  for (int i = 0; i <= W; i++) {
    for (int j = 0; j <= N; j++) {
      if (j == 0) {
        ans = (ans + dp[i][j]) % mod;
      } else {
        if (w[j - 1] > W - i) {
          ans = (ans + dp[i][j]) % mod;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
ll dp[202][20011] = {1}, ans;
int n, W, w[201], sum[201];

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n, greater<int>());
  sum[n - 1] = w[n - 1];
  for (int i = n - 2; i >= 0; i--)
    sum[i] = w[i] + sum[i + 1];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W && sum[i + 1] <= W; j++)
      if (sum[i + 1] + j <= W && sum[i + 1] + w[i] + j > W)
        ans = (ans + dp[i][j]) % mod;

    for (int j = 0; j <= W; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + w[i]] = (dp[i + 1][j + w[i]] + dp[i][j]) % mod;
    }
  }

  cout << max(1LL, ans) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int n, W, w[201];
map<PP, ll> mem;

ll dfs(int idx, int sum, int m) {
  ll res = 0;
  if (sum + w[m] > W || (m == n && sum + w[idx] > W))
    return 1;
  if (mem.count(PP(P(idx, sum), m)))
    return mem[PP(P(idx, sum), m)];

  for (int i = idx; i < n; i++) {
    if (sum + w[i] > W)
      continue;
    if (i == idx)
      res += dfs(i + 1, sum + w[i], m);
    else
      res += dfs(i + 1, sum + w[i], min(m, idx));
    res %= mod;
  }
  return mem[PP(P(idx, sum), m)] = res;
}

int main() {

  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  cout << max(1LL, dfs(0, 0, n)) << endl;
  return 0;
}