Problem p03164 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define N 505
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int, int>
#define lli pair<ll, ll>
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define fast2                                                                  \
  freopen("badhair.gir", "r", stdin);                                          \
  freopen("badhair.cik", "w", stdout);
#define mod 1000000007
#define fs(x, y)                                                               \
  for (ll i = 1; i <= y; i++)                                                  \
  cin >> x[i]
#define fo(i, x, y) for (ll i = x; i <= y; i++)
#define INF 1000000000005
using namespace std;

ll n, m, ar[N], sum, t, W, w[N], v[N], flag;

ll dp[N][500005];

ll f(int ind, int val) {
  if (dp[ind][val] != -1)
    return dp[ind][val];
  if (val == 0)
    return dp[ind][val] = 0;
  if (ind == n + 1) {
    return dp[ind][val] = 1e12;
  }
  return dp[ind][val] = min(f(ind + 1, val), f(ind + 1, val - v[ind]) + w[ind]);
}

int main() {
  fast;
  cin >> n >> W;
  fo(i, 1, n) cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 1e5 + 100; i++) {
    f(1, i);
    fo(j, 1, n) if (dp[j][i] <= W && dp[j][i] != -1) sum = i;
  }
  cout << sum;
}

/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/

Fixed submission:
#include <bits/stdc++.h>
#define N 505
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int, int>
#define lli pair<ll, ll>
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define fast2                                                                  \
  freopen("badhair.gir", "r", stdin);                                          \
  freopen("badhair.cik", "w", stdout);
#define mod 1000000007
#define fs(x, y)                                                               \
  for (ll i = 1; i <= y; i++)                                                  \
  cin >> x[i]
#define fo(i, x, y) for (ll i = x; i <= y; i++)
#define INF 1000000000005
using namespace std;

ll n, m, ar[N], sum, t, W, w[N], v[N], flag;

ll dp[N][200005];

ll f(int ind, int val) {
  if (dp[ind][val] != -1)
    return dp[ind][val];
  if (val == 0)
    return dp[ind][val] = 0;
  if (ind == n + 1) {
    return dp[ind][val] = 1e12;
  }
  return dp[ind][val] = min(f(ind + 1, val), f(ind + 1, val - v[ind]) + w[ind]);
}

int main() {
  fast;
  cin >> n >> W;
  fo(i, 1, n) cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 1e5 + 100; i++) {
    f(1, i);
    fo(j, 1, n) if (dp[j][i] <= W && dp[j][i] != -1) sum = i;
  }
  cout << sum;
}

/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/