Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define forr(i, k, n) for (int i = k; i < n; i++)
#define iforr(i, k, n) for (int i = n - 1; i >= k; i--)
#define rep(i, n) forr(i, 0, n)
#define irep(i, n) iforr(i, 0, n)
#define aut(i, arr) for (auto &i : arr)
#define all(arr) arr.begin(), arr.end()
#define pb push_back
#define pii pair<int, int>
#define vpii vector<pii>
#define f first
#define db double
#define s second
#define ll long long
#define ull unsigned long long
#define E '\n'
#define lmax 9223372036854775807;
#define MOD 1000000007
using namespace std;
vi v;
vvi ed;
void dfs(int i) {
  v[i] = 1;
  aut(j, ed[i]) if (!v[j]) dfs(j);
}
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
}

int main() {
  Fast_IO();
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    vi arr(n);
    vi ar(n);
    rep(i, n) {
      cin >> arr[i];
      cin >> ar[i];
    }
    vector<vector<ll>> dp(2, vector<ll>(k + 1, -1));
    dp[0][0] = 0;
    int flg = 1;
    forr(i, 1, n + 1) {
      dp[flg][0] = 0;
      forr(j, 1, k + 1) {
        dp[flg][j] = dp[flg ^ 1][j];
        if (arr[i - 1] <= j)
          if (dp[flg ^ 1][j - arr[i - 1]] >= 0)
            dp[flg][j] =
                max(dp[flg][j], ar[i - 1] + dp[flg ^ 1][j - arr[i - 1]]);
      }
      flg ^= 1;
    }
    ll ans = -1;
    rep(i, k + 1) ans = max(ans, dp[flg ^ 1][i]);
    cout << ans;
    cout << E;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define forr(i, k, n) for (int i = k; i < n; i++)
#define iforr(i, k, n) for (int i = n - 1; i >= k; i--)
#define rep(i, n) forr(i, 0, n)
#define irep(i, n) iforr(i, 0, n)
#define aut(i, arr) for (auto &i : arr)
#define all(arr) arr.begin(), arr.end()
#define pb push_back
#define pii pair<int, int>
#define vpii vector<pii>
#define f first
#define db double
#define s second
#define ll long long
#define ull unsigned long long
#define E '\n'
#define lmax 9223372036854775807;
#define MOD 1000000007
using namespace std;
vi v;
vvi ed;
void dfs(int i) {
  v[i] = 1;
  aut(j, ed[i]) if (!v[j]) dfs(j);
}
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
}

int main() {
  // Fast_IO();
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    vi arr(n);
    vi ar(n);
    rep(i, n) {
      cin >> arr[i];
      cin >> ar[i];
    }
    vector<vector<ll>> dp(2, vector<ll>(k + 1, -1));
    dp[0][0] = 0;
    int flg = 1;
    forr(i, 1, n + 1) {
      dp[flg][0] = 0;
      forr(j, 1, k + 1) {
        dp[flg][j] = dp[flg ^ 1][j];
        if (arr[i - 1] <= j)
          if (dp[flg ^ 1][j - arr[i - 1]] >= 0)
            dp[flg][j] =
                max(dp[flg][j], ar[i - 1] + dp[flg ^ 1][j - arr[i - 1]]);
      }
      flg ^= 1;
    }
    ll ans = -1;
    rep(i, k + 1) ans = max(ans, dp[flg ^ 1][i]);
    cout << ans;
    cout << E;
  }
  return 0;
}