Language: C++

#include<bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout<<(v)<<endl
#define CIN(n)  int(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define ALL(obj) (obj).begin(),(obj).end()

#define P pair<int,int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)),a.erase(unique(ALL(a)), a.end())
class Edge{
public:
    int from,to,value;
    Edge(int a,int b,int c){
        from = a;
        to = b;
        value = c;
    }
    Edge(int a,int b){
        from = a;
        to = b;
    }
};
int main(){
    CIN(N);CIN(W);
    V<pair<LL,LL>> vec1,vec2,vec3,vec4;
    CIN(B);CIN(C);
    vec1.pb(make_pair(C,B));
    LL ans = 0;
    REP(a,N-1){
        CIN(D);CIN(E);
        if(D-B==0){
            vec1.pb(make_pair(E,D));
        }else if(D-B==1){
            vec2.pb(make_pair(E,D));
        }else if(D-B==2){
            vec3.pb(make_pair(E,D));
        }else{
            vec4.pb(make_pair(E,D));
        }
    }
    if(vec1.empty())vec1.pb(make_pair(0,0));
    if(vec2.empty())vec2.pb(make_pair(0,0));
    if(vec3.empty())vec3.pb(make_pair(0,0));
    if(vec4.empty())vec4.pb(make_pair(0,0));
    sort(ALL(vec1));
    sort(ALL(vec2));
    sort(ALL(vec3));
    sort(ALL(vec4));
    reverse(ALL(vec1));
    reverse(ALL(vec2));
    reverse(ALL(vec3));
    reverse(ALL(vec4));
    //COUT(vec1.size());
    //COUT(vec2.size());
    //COUT(vec3.size());
    //COUT(vec4.size());
    REP(a,vec1.size()+1){
        REP(b,vec2.size()+1){
            REP(c,vec3.size()+1){
                REP(d,vec4.size()+1){
                    LL ins = 0;
                    //COUT(a*vec1.front().second+b*vec2.front().second+c*vec3.front().second+d*vec4.front().second);
                    if(a*vec1.front().second+b*vec2.front().second+c*vec3.front().second+d*vec4.front().second > W)continue;
                    REP(e,a){
                        ins+=vec1.at(e).first;
                    }
                    REP(f,b){
                        ins+=vec2.at(f).first;
                    }
                    REP(g,c){
                        ins+=vec3.at(g).first;
                    }
                    REP(h,d){
                        ins+=vec4.at(h).first;
                    }
                    ans = max(ins,ans,0LL);
                    //cout<<a*vec1.front().second+b*vec2.front().second+c*vec3.front().second+d*vec4.front().second<<" "<<ins<<endl;
                }
            }
        }
    }
    COUT(ans);

    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare) [with _Tp = long long int; _Compare = long long int]':
a.cc:95:30:   required from here
   95 |                     ans = max(ins,ans,0LL);
      |                           ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:306:17: error: '__comp' cannot be used as a function
  306 |       if (__comp(__a, __b))
      |           ~~~~~~^~~~~~~~~~