alexjercan/bugnet huggingface dataset:
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int ll;

const int MAX_N = 205;
const int MAX_W = 10005;
const ll MOD = 1000000007;
int N, W;

ll dp[MAX_N][MAX_W];

ll func(vector<ll> &vec, int sum, int id) {
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= W; j++)
      dp[i][j] = 0;
  dp[id + 1][sum] = 1;

  ll ret = 0;
  for (int i = id + 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
      if (j + vec[i] > W)
        continue;
      dp[i + 1][j + vec[i]] = (dp[i + 1][j + vec[i]] + dp[i][j]) % MOD;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (W - i < vec[id]) {
      ret = (ret + dp[N][i]) % MOD;
    }
  }
  return ret;
}

int main() {
  cin >> N >> W;
  vector<ll> vec(N);

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

  sort(vec.begin(), vec.end());
  int sum = 0;
  ll res = 0;

  for (int i = 0; i < N; i++) {
    if (sum > W)
      continue;
    res = (res + func(vec, sum, i)) % MOD;
    sum += vec[i];
  }
  if (sum <= W)
    res = (res + 1LL) % MOD;
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;

const int MAX_N = 205;
const int MAX_W = 10005;
const ll MOD = 1000000007;
int N, W;

ll dp[MAX_N][MAX_W];

ll func(vector<ll> &vec, int sum, int id) {
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= W; j++)
      dp[i][j] = 0;
  dp[id + 1][sum] = 1;

  ll ret = 0;
  for (int i = id + 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
      if (j + vec[i] > W)
        continue;
      dp[i + 1][j + vec[i]] = (dp[i + 1][j + vec[i]] + dp[i][j]) % MOD;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (W - i < vec[id]) {
      ret = (ret + dp[N][i]) % MOD;
    }
  }
  return ret;
}

int main() {
  cin >> N >> W;
  vector<ll> vec(N);

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

  sort(vec.begin(), vec.end());
  int sum = 0;
  ll res = 0;

  for (int i = 0; i < N; i++) {
    res = (res + func(vec, sum, i)) % MOD;
    sum += vec[i];
  }
  if (sum <= W)
    res = (res + 1LL) % MOD;
  cout << res << endl;
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;

const ll mod = 1000000007;

int w[300];
ll dp[2][201][10010];

int main() {
  int N, W;
  while (cin >> N >> W) {
    memset(dp, 0, sizeof(dp));
    ll res = 0;
    rep(i, N) { cin >> w[i]; }
    sort(w, w + N, greater<int>());
    w[N] = INF;
    dp[0][N][0] = 1;
    rep(i, N) {
      rep(j, N + 1) rep(k, W + 1) {
        if (k + w[i] <= W) {
          dp[1][j][k + w[i]] = (dp[1][j][k + w[i]] + dp[0][j][k]);
        }
        dp[1][i][k] = (dp[1][i][k] + dp[0][j][k]);
      }
      rep(j, N + 1) rep(k, W + 1) {
        dp[0][j][k] = dp[1][j][k];
        dp[1][j][k] = 0;
      }
    }
    rep(i, N + 1) rep(j, W + 1) {
      if (j > W - w[i])
        res = (res + dp[0][i][j]) % mod;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;

const ll mod = 1000000007;

int w[300];
ll dp[2][201][10010];

int main() {
  int N, W;
  while (cin >> N >> W) {
    memset(dp, 0, sizeof(dp));
    ll res = 0;
    rep(i, N) { cin >> w[i]; }
    sort(w, w + N, greater<int>());
    w[N] = INF;
    dp[0][N][0] = 1;
    rep(i, N) {
      fill(&dp[1][0][0], &dp[1][0][0] + 201 * 10010, 0);
      rep(j, N + 1) rep(k, W + 1) {
        if (k + w[i] <= W) {
          dp[1][j][k + w[i]] = (dp[1][j][k + w[i]] + dp[0][j][k]) % mod;
        }
        dp[1][i][k] = (dp[1][i][k] + dp[0][j][k]) % mod;
      }
      rep(j, N + 1) rep(k, W + 1) { dp[0][j][k] = dp[1][j][k]; }
    }
    rep(i, N + 1) rep(j, W + 1) {
      if (j > W - w[i])
        res = (res + dp[0][i][j]) % mod;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int mod = 1000000007;
map<int, int> DP
    [2]
    [10001]; //[i][w][e]??????w??§???????????§???????????£????????????????°???????e

int main() {
  int N, W;
  cin >> N >> W;
  DP[0][0][W + 1] = 1;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> w;
    for (int j = 0; j <= W; j++)
      DP[i & 1 ^ 1][j].clear();
    for (int j = 0; j <= W; j++) {
      for (auto p : DP[i & 1][j]) {
        int e, cnt;
        tie(e, cnt) = p;
        if (j + w <= W)
          DP[i & 1 ^ 1][j + w][e] = (DP[i & 1 ^ 1][j + w][e] + cnt) % mod;
        e = min(w, e);
        DP[i & 1 ^ 1][j][e] = (DP[i & 1 ^ 1][j][e] + cnt) % mod;
      }
    }
  }
  int ans = 0;
  for (int j = 0; j <= W; j++) {
    for (auto p : DP[N & 1][j]) {
      int e, cnt;
      tie(e, cnt) = p;
      if (e + j > W)
        ans = (ans + cnt) % mod;
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

const int mod = 1000000007;
map<int, int> DP
    [201]
    [10001]; //[i][w][e]??????w??§???????????§???????????£????????????????°???????e

int main() {
  int N, W;
  cin >> N >> W;
  DP[0][0][W + 1] = 1;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> w;
    for (int j = 0; j <= W; j++) {
      for (auto p : DP[i][j]) {
        int e, cnt;
        tie(e, cnt) = p;
        if (j + w <= W)
          DP[i + 1][j + w][e] = (DP[i + 1][j + w][e] + cnt) % mod;
        e = min(w, e);
        DP[i + 1][j][e] = (DP[i + 1][j][e] + cnt) % mod;
      }
    }
  }
  int ans = 0;
  for (int j = 0; j <= W; j++) {
    for (auto p : DP[N][j]) {
      int e, cnt;
      tie(e, cnt) = p;
      if (e + j > W)
        ans = (ans + cnt) % mod;
    }
  }
  cout << ans << endl;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const ll mod = 1000000007;

int main() {
  int n, w;
  cin >> n >> w;
  vector<ll> v(n), s(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v.begin(), v.end(), greater<int>());
  v.push_back(0);
  ll ans = 0, sum = 0;
  for (int i = n; i >= 0; --i) {
    sum += v[i];
    s[i] = sum;
  }
  //????°?????±???????
  for (int i = n; i >= 0; --i) {
    ll w1 = w - s[i] + v[i];
    if (w1 < 0)
      break;
    vector<vector<ll>> dp(i + 1, vector<ll>(w1 + 1, 0));
    //????????????id
    for (int j = i; j >= 0; --j) {
      //???????????????
      for (int k = 0; k <= w1; ++k) {
        if (j == i) {
          if (k < v[i])
            dp[j][k] = 1;
        } else if (k - v[j] < 0)
          dp[j][k] = dp[j + 1][k];
        else
          dp[j][k] = dp[j + 1][k - v[j]] + dp[j + 1][k];
        dp[j][k] %= mod;
      }
    }
    ans += dp[0][w1];
    ans %= mod;
  }
  if (!ans)
    ans = 1;
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  int n, w;
  cin >> n >> w;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v.begin(), v.end(), greater<int>());
  v.push_back(100000000);
  vector<vector<vector<ll>>> dp(
      n + 1, vector<vector<ll>>(w + 1, vector<ll>(n + 1, 0)));
  for (int j = 0; j <= w; ++j) {
    for (int k = 0; k <= n; ++k) {
      if (j < v[k])
        dp[n][j][k] = 1;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j <= w; ++j) {
      for (int k = 0; k <= n; ++k) {
        if (j - v[i] < 0)
          dp[i][j][k] = dp[i + 1][j][i];
        else
          dp[i][j][k] = dp[i + 1][j][i] + dp[i + 1][j - v[i]][k];
        dp[i][j][k] %= 1000000007;
      }
    }
  }
  cout << dp[0][w][n] << endl;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {

  int n, w;
  cin >> n >> w;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(c.begin(), c.end());
  int now = 0;
  int next = 1;

  // dp[i][j][k] :=
  // i番目まで見て、未だとってない友達の中で重さが最小のものがc[j]で,合計の重さがkである通り数
  vector<vector<vector<int>>> dp(2,
                                 vector<vector<int>>(n, vector<int>(w + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {

    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= w; k++) {
        dp[next][j][k] = 0;
      }
    }

    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= w; k++) {

        // とる
        if (k + c[i] <= w) {
          if (j == i && j + 1 < n)
            (dp[next][j + 1][k + c[i]] += dp[now][j][k]) %= MOD;
          else if (j <= i)
            (dp[next][j][k + c[i]] += dp[now][j][k]) %= MOD;
        }
        // とらない
        (dp[next][j][k] += dp[now][j][k]) %= MOD;
      }
    }

    swap(now, next);
  }

  int ans = 0;
  for (int j = 0; j < n; j++) {
    for (int k = 0; k <= w; k++) {
      if (c[j] + k > w)
        (ans += dp[now][j][k]) %= MOD;
    }
  }

  /*for(int i = 0; i < n + 1; i++){
      for(int j = 0; j < n; j++){
          for(int k = 0; k < w + 1; k++){
              cout << dp[i][j][k] << " ";
          }
          cout << endl;
      }
      cout << endl;
  }*/
  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

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

int main() {

  int n, w;
  cin >> n >> w;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(c.begin(), c.end());

  // dp[i][j][k] :=
  // i番目まで見て、未だとってない友達の中で重さが最小のものがc[j]で,合計の重さがkである通り数
  vector<vector<vector<int>>> dp(n + 1,
                                 vector<vector<int>>(n, vector<int>(w + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= w; k++) {

        // とる
        if (k + c[i] <= w) {
          if (j == i && j + 1 < n)
            (dp[i + 1][j + 1][k + c[i]] += dp[i][j][k]) %= MOD;
          else if (j <= i)
            (dp[i + 1][j][k + c[i]] += dp[i][j][k]) %= MOD;
        }
        // とらない
        (dp[i + 1][j][k] += dp[i][j][k]) %= MOD;
      }
    }
  }

  int ans = 0;
  for (int j = 0; j < n; j++) {
    for (int k = 0; k <= w; k++) {
      if (c[j] + k > w)
        (ans += dp[n][j][k]) %= MOD;
    }
  }

  /*for(int i = 0; i < n + 1; i++){
      for(int j = 0; j < n; j++){
          for(int k = 0; k < w + 1; k++){
              cout << dp[i][j][k] << " ";
          }
          cout << endl;
      }
      cout << endl;
  }*/
  cout << ans << endl;

  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

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

#define MOD 1000000007
#define INF 1000000007
typedef /*long long*/ int LLI;

int n;
int W;
int myws[201];
int imos[201];
LLI dp[2][11451][201];

int main() {
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &myws[i]);
  }
  sort(myws + 1, myws + n + 1);

  int src = 0;
  int dst = 1;
  dp[src][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k < i; k++) {
      for (int j = 0; j <= W; j++) {
        dp[dst][j][k] = dp[src][j][k];
      }
    }

    for (int j = 0; j <= W; j++) {
      dp[dst][j][i] = dp[src][j][0];
    }

    int w = myws[i];
    for (int j = 0; j <= W - w; j++) {
      for (int k = 0; k < i; k++) {
        dp[dst][j + w][k] = (dp[dst][j + w][k] + dp[src][j][k]) % MOD;
      }
    }

    swap(src, dst);
    fill(dp[dst][0], dp[dst][W + 1], 0);
  }

  /*for (int i=0; i<=n; i++) {
    printf("%d:", i);
    for (int j=0; j<=W; j++) {
      int sum = 0;
      for (int k=0; k<=n; k++) sum += dp[i][j][k];
      printf(" %d", sum);
    }
    puts("");
  }*/

  LLI ans = 0;
  for (int k = 0; k <= n; k++) {
    int st = 0;
    if (k != 0)
      st = max(W - myws[k] + 1, 0);
    for (int j = st; j <= W; j++) {
      ans = (ans + dp[src][j][k]) % MOD;
    }
  }
  printf("%d\\n", ans % MOD);
}

Error: 0

Fixed submission:

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

#define MOD 1000000007
#define INF 1000000007
typedef /*long long*/ int LLI;

int n;
int W;
int myws[201];
int imos[201];
LLI dp[2][1145][201];

int main() {
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &myws[i]);
  }
  sort(myws + 1, myws + n + 1);

  int src = 0;
  int dst = 1;
  dp[src][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k < i; k++) {
      for (int j = 0; j <= W; j++) {
        dp[dst][j][k] = dp[src][j][k];
      }
    }

    for (int j = 0; j <= W; j++) {
      dp[dst][j][i] = dp[src][j][0];
    }

    int w = myws[i];
    for (int j = 0; j <= W - w; j++) {
      for (int k = 0; k < i; k++) {
        dp[dst][j + w][k] = (dp[dst][j + w][k] + dp[src][j][k]) % MOD;
      }
    }

    swap(src, dst);
    fill(dp[dst][0], dp[dst][W + 1], 0);
  }

  /*for (int i=0; i<=n; i++) {
    printf("%d:", i);
    for (int j=0; j<=W; j++) {
      int sum = 0;
      for (int k=0; k<=n; k++) sum += dp[i][j][k];
      printf(" %d", sum);
    }
    puts("");
  }*/

  LLI ans = 0;
  for (int k = 0; k <= n; k++) {
    int st = 0;
    if (k != 0)
      st = max(W - myws[k] + 1, 0);
    for (int j = st; j <= W; j++) {
      ans = (ans + dp[src][j][k]) % MOD;
    }
  }
  printf("%d\\n", ans % MOD);
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028841971
#define INF 1000000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
typedef pair<double, P> PQ;

int n, w;
int a[200];
ll dp[10001];
ll ans;

int main() {
  cin >> n >> w;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  if (a[0] > w) {
    cout << 1 << endl;
    return 0;
  }
  int sum = 0;
  rep(i, n) sum += a[i];
  if (sum <= w)
    ans++;
  rep(i, n) {
    memset(dp, 0, sizeof(dp));
    int ini = 0;
    int mini = a[i];
    for (int j = 0; j < i; j++) {
      ini += a[j];
    }
    if (ini > w)
      break;
    dp[ini] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = w - a[j]; k >= 0; k--) {
        dp[k + a[j]] += dp[k];
      }
    }
    for (int j = 0; j <= w; j++)
      dp[j] %= MOD;
    for (int j = w - mini + 1; j <= w; j++)
      ans += dp[j];
    ans %= MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028841971
#define INF 1000000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
typedef pair<double, P> PQ;

int n, w;
int a[200];
ll dp[10001];
ll ans;

int main() {
  cin >> n >> w;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  if (a[0] > w) {
    cout << 1 << endl;
    return 0;
  }
  int sum = 0;
  rep(i, n) sum += a[i];
  if (sum <= w)
    ans++;
  rep(i, n) {
    memset(dp, 0, sizeof(dp));
    int ini = 0;
    int mini = a[i];
    for (int j = 0; j < i; j++) {
      ini += a[j];
    }
    dp[ini] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = w - a[j]; k >= 0; k--) {
        dp[k + a[j]] += dp[k];
      }
    }
    for (int j = 0; j <= w; j++)
      dp[j] %= MOD;
    for (int j = w - mini + 1; j <= w; j++)
      ans += dp[j];
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;
int dp[205][10005];

signed main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(n + 1, -1);
  rep(i, n) cin >> a[i + 1];
  sort(all(a));

  vector<int> sum(all(a));
  sum[0] = 0;
  range(i, 1, n + 1) { sum[i] += sum[i - 1]; }

  dp[0][0] = 1;
  range(i, 1, n + 1) { //???????????¨?????????????????\\????????¨???
    if (sum[i] > w)
      break;
    dp[0][sum[i]] = 1;
  }

  range(i, 1, n + 1) { // i - 1????????????????????¨??¨??\\??????i
                       // ????????????????????\\???????????¨???
    if (sum[i] > w)
      break;
    dp[i][sum[i - 1]] = 1;
    range(j, i + 1, n + 1) {
      for (int k = w; k >= sum[i - 1]; k--) {
        if (k + a[j] > w)
          continue;
        // show(k)
        (dp[i][k + a[j]] += dp[i][k]) %= M;
      }
    }
  }

  // rep(i,n + 1){ rep(j,w + 1){ cout << dp[i][j] <<' '; } cout << endl; }

  int ans = 0;
  range(i, 1, n + 1) {
    range(j, sum[i], w + 1) {
      if (j + a[i] <= w)
        continue;
      // show(dp[i][j])
      // cout << i << ' ' << j << endl;
      (ans += dp[i][j]) %= M;
    }
  }

  cout << ans + 1 << endl; //????????????????????\\????????¨??????1??????
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;
int dp[205][10005];

signed main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(n + 1, -1);
  rep(i, n) cin >> a[i + 1];
  sort(all(a));

  vector<int> sum(all(a));
  sum[0] = 0;
  range(i, 1, n + 1) { sum[i] += sum[i - 1]; }

  dp[0][0] = 1;
  range(i, 1, n + 1) { //???????????¨?????????????????\\????????¨???
    if (sum[i] > w)
      break;
    dp[0][sum[i]] = 1;
  }

  range(i, 1, n + 1) { // i - 1????????????????????¨??¨??\\??????i
                       // ????????????????????\\???????????¨???
    dp[i][sum[i - 1]] = 1;
    range(j, i + 1, n + 1) {
      for (int k = w; k >= sum[i - 1]; k--) {
        if (k + a[j] > w)
          continue;
        // show(k)
        (dp[i][k + a[j]] += dp[i][k]) %= M;
      }
    }
  }

  // rep(i,n + 1){ rep(j,w + 1){ cout << dp[i][j] <<' '; } cout << endl; }

  int ans = 0;
  range(i, 1, n + 1) {
    range(j, sum[i], w + 1) {
      if (j + a[i] <= w)
        continue;
      // show(dp[i][j])
      // cout << i << ' ' << j << endl;
      (ans += dp[i][j]) %= M;
    }
  }

  cout << ans + 1 << endl; //????????????????????\\????????¨??????1??????
}
Problem p01450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;

const int mod = 1000000007;
int N, W;
int w[201];
ll dp[2][10001][202];

int main() {

  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i];
  for (int j = 0; j < 10001; j++) {
    for (int k = 0; k <= N; k++) {
      if (j <= W && (k == N || w[k] + j > W))
        dp[N % 2][j][k] = 1;
      else
        dp[N % 2][j][k] = 0;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    int cur = (i + 1) % 2;
    int nxt = (i) % 2;
    for (int j = 0; j < 10001; j++) {
      for (int k = 0; k <= N; k++) {
        dp[nxt][j][k] = 0;
        int nmwidx = k;
        if (k == N || w[k] > w[i])
          nmwidx = i;
        dp[nxt][j][k] += dp[cur][j][nmwidx];
        if (j + w[i] <= W)
          dp[nxt][j][k] += dp[cur][j + w[i]][k];
        dp[nxt][j][k] %= mod;
      }
    }
  }
  cout << dp[0][0][N] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;

const int mod = 1000000007;
int N, W;
int w[201];
ll dp[2][10001][202];
//
// ll dfs(int pos,int wl,int mwidx){
//	if(pos==N){
//		if(wl<=W&&(mwidx==-1||w[mwidx]+wl>W))return 1;
//		return 0;
//	}
//	if(dp[pos][wl]!=-1)return dp[pos][wl];
//	ll res=0;
//	int nmwidx=mwidx;
//	if(nmwidx==-1||w[nmwidx]>w[pos])nmwidx=pos;
//	res+=dfs(pos+1,wl,mwidx);
//	if(wl+w[pos]<=W)res+=dfs(pos+1,wl+w[pos],mwidx);
//	return dp[pos][wl]=res%mod;
//}

int main() {

  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i];
  // for(int i=0;i<201;i++)for(int j=0;j<10001;j++)dp[i][j]=-1;
  // ll res=dfs(0,0,-1);
  for (int j = 0; j < 10001; j++) {
    for (int k = 0; k <= N; k++) {
      if (j <= W && (k == N || w[k] + j > W))
        dp[N % 2][j][k] = 1;
      else
        dp[N % 2][j][k] = 0;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    int cur = (i + 1) % 2;
    int nxt = (i) % 2;
    for (int j = 0; j < 10001; j++)
      for (int k = 0; k <= N; k++)
        dp[nxt][j][k] = 0;
    for (int j = 0; j < 10001; j++) {
      for (int k = 0; k <= N; k++) {
        int nmwidx = k;
        if (k == N || w[k] > w[i])
          nmwidx = i;
        dp[nxt][j][k] += dp[cur][j][nmwidx];
        if (j + w[i] <= W)
          dp[nxt][j][k] += dp[cur][j + w[i]][k];
        dp[cur][j][k] %= mod;
      }
    }
  }
  cout << dp[0][0][N] << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

const int mod = 1000000007;
int a[10000 + 10];
int dp[200][10000 + 10];
int w, n;

int memo;
int solve(int pos, int rest) {
  if (pos == n)
    return rest < a[memo] ? 1 : 0;

  if (dp[pos][rest] != -1)
    return dp[pos][rest];

  int ret = 0;
  if (pos >= memo)
    ret = (ret + solve(pos + 1, rest)) % mod;
  if (pos != memo && rest >= a[pos])
    ret = (ret + solve(pos + 1, rest - a[pos])) % mod;

  return dp[pos][rest] = ret;
}

int main() {
  int ans = 0;
  n = getInt();
  w = getInt();

  REP(i, n) a[i] = getInt();

  sort(a, a + n);

  int sum = 0;
  REP(i, n) {
    memset(dp, -1, sizeof(dp));
    memo = i;
    ans = (ans + solve(0, w)) % mod;
    sum += a[i];
    if (sum > w)
      break;
  }
  if (sum <= w)
    ans++;
  if (ans == 0)
    ans = 1;

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

const int mod = 1000000007;
int a[100000 + 10];
int dp[200][100000 + 10];
int w, n;

int memo;
int solve(int pos, int rest) {
  if (pos == n)
    return rest < a[memo] ? 1 : 0;

  if (dp[pos][rest] != -1)
    return dp[pos][rest];

  int ret = 0;
  if (pos >= memo)
    ret = (ret + solve(pos + 1, rest)) % mod;
  if (pos != memo && rest >= a[pos])
    ret = (ret + solve(pos + 1, rest - a[pos])) % mod;

  return dp[pos][rest] = ret;
}

int main() {
  int ans = 0;
  n = getInt();
  w = getInt();

  REP(i, n) a[i] = getInt();

  sort(a, a + n);

  int sum = 0;
  REP(i, n) {
    memset(dp, -1, sizeof(dp));
    memo = i;
    ans = (ans + solve(0, w)) % mod;
    sum += a[i];
    if (sum > w)
      break;
  }
  if (sum <= w)
    ans++;
  if (ans == 0)
    ans = 1;

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

  return 0;
}