Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define mo 1000000007
#define MAX 1000004
#define fast_io ios::sync_with_stdio(false)
ll wt[101], a[101];
map<ll, ll> m1;
ll m2[5];
vector<long long> arr[5];
vector<long long> arr2[5];
int main() {
  fast_io;
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, w;
  cin >> n >> w;
  int ct = 1;
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> a[i];
    if (m1.find(wt[i]) == m1.end()) {
      m1[wt[i]] = ct++;
      m2[m1[wt[i]]] = wt[i];
    }
  }
  for (int i = 1; i <= n; i++)
    arr[m1[wt[i]]].pb(a[i]);
  for (int i = 1; i < ct; i++) {
    sort(arr[i].begin(), arr[i].end());
    reverse(arr[i].begin(), arr[i].end());
    arr2[i].pb(0);
    long long temp = 0;
    // cout << i << " " << m2[i] << "\n";
    for (int j = 0; j < arr[i].size(); j++) {
      temp += arr[i][j];
      arr2[i].pb(temp);
      // cout << temp << "\n";
    }
  }
  // for(int i=1;i<ct;i++)
  // {
  // 	cout << "Weight - " << i << " " << m2[i] << "\n";
  // 	for(int j=0;j<arr2[i].size();j++)
  // 		cout << arr2[i][j] << " ";
  // 	cout << "\n";
  // }
  long long ans = 0;
  for (int i = 0; i <= (w / (m2[1])) && i < arr2[1].size(); i++) {
    long long temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
    temp1 = arr2[1][i];
    ans = max(ans, temp1);
    if (ct > 2) {
      for (int j = 0; j <= (w - m2[1] * i) / m2[2] && j < arr2[2].size(); j++) {
        temp2 = arr2[2][j];
        if (ct > 3) {
          for (int k = 0;
               k <= (w - 1ll * m2[1] * i - 1ll * m2[2] * j) / m2[3] &&
               k < arr2[3].size();
               k++) {
            temp3 = arr2[3][k];
            if (ct > 4) {
              for (int p = 0; p <= (w - 1ll * m2[1] * i - 1ll * m2[2] * j -
                                    1ll * m2[3] * k) /
                                       m2[4] &&
                              p < arr2[4].size();
                   p++) {
                temp4 = arr2[4][p];
                ans = max(ans, temp1 + temp2 + temp3 + temp4);
              }
            }
            ans = max(ans, temp1 + temp2 + temp3);
          }
        }
        ans = max(ans, temp1 + temp2);
      }
    }
  }
  cout << ans;

  return 0;
}

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

#define ll long long
#define pb push_back
#define mp make_pair
#define mo 1000000007
#define MAX 1000004
#define fast_io ios::sync_with_stdio(false)
ll wt[101], a[101];
map<ll, ll> m1;
ll m2[5];
vector<long long> arr[5];
vector<long long> arr2[5];
int main() {
  fast_io;
  ll n, w;
  cin >> n >> w;
  int ct = 1;
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> a[i];
    if (m1.find(wt[i]) == m1.end()) {
      m1[wt[i]] = ct++;
      m2[m1[wt[i]]] = wt[i];
    }
  }
  for (int i = 1; i <= n; i++)
    arr[m1[wt[i]]].pb(a[i]);
  for (int i = 1; i < ct; i++) {
    sort(arr[i].begin(), arr[i].end());
    reverse(arr[i].begin(), arr[i].end());
    arr2[i].pb(0);
    long long temp = 0;
    // cout << i << " " << m2[i] << "\n";
    for (int j = 0; j < arr[i].size(); j++) {
      temp += arr[i][j];
      arr2[i].pb(temp);
      // cout << temp << "\n";
    }
  }
  // for(int i=1;i<ct;i++)
  // {
  // 	cout << "Weight - " << i << " " << m2[i] << "\n";
  // 	for(int j=0;j<arr2[i].size();j++)
  // 		cout << arr2[i][j] << " ";
  // 	cout << "\n";
  // }
  long long ans = 0;
  for (int i = 0; i <= (w / (m2[1])) && i < arr2[1].size(); i++) {
    long long temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
    temp1 = arr2[1][i];
    ans = max(ans, temp1);
    if (ct > 2) {
      for (int j = 0; j <= (w - m2[1] * i) / m2[2] && j < arr2[2].size(); j++) {
        temp2 = arr2[2][j];
        if (ct > 3) {
          for (int k = 0;
               k <= (w - 1ll * m2[1] * i - 1ll * m2[2] * j) / m2[3] &&
               k < arr2[3].size();
               k++) {
            temp3 = arr2[3][k];
            if (ct > 4) {
              for (int p = 0; p <= (w - 1ll * m2[1] * i - 1ll * m2[2] * j -
                                    1ll * m2[3] * k) /
                                       m2[4] &&
                              p < arr2[4].size();
                   p++) {
                temp4 = arr2[4][p];
                ans = max(ans, temp1 + temp2 + temp3 + temp4);
              }
            }
            ans = max(ans, temp1 + temp2 + temp3);
          }
        }
        ans = max(ans, temp1 + temp2);
      }
    }
  }
  cout << ans;

  return 0;
}