Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
# define mod 1000000007
# define INF (1 << 29)
/* ちゃんと考えて実装して問題なくACできた時嬉しいよ！！ */

int main(void){
    int n,t;
    vector< pair<ll,ll> > v, v_rev;
    cin >> n >> t;
    for(ll i=0; i<n; i++){
        ll a,b;
        cin >> a >> b;
        v.push_back(make_pair(a,b));
        v_rev.push_back(make_pair(a,b));
    }

    // 時間の順にsort
    sort(v.begin(), v.end());
    sort(v_rev.begin(), v_rev.end(), greater< pair<int,int> >());

    // dp[i][j] := 1番目~i番目までから選んで
    // 時間j以下で食べる時の満足度の最大値
    int dp_1[n+1][t];
    int dp_2[n+1][t];
    for(int i=0; i<=n; i++){
        for(int j=1; j<=t-1; j++){
            dp[i][j] = 0;
        }
    }

    // dp_1
    for(int i=0; i<n; i++){
        for(int j=1; j<=t-1; j++){
            if(v[i].first > j){
                dp_1[i+1][j] = dp_1[i][j];
            }
            else{
                dp_1[i+1][j] = max(dp_1[i][j], dp_1[i][j-v[i].first]+v[i].second);
            }
        }
    }

    // dp_2
    for(int i=0; i<n; i++){
        for(int j=1; j<=t-1; j++){
            if(v_rev[i].first > j){
                dp_2[i+1][j] = dp_2[i][j];
            }
            else{
                dp_2[i+1][j] = max(dp_2[i][j], dp_2[i][j-v_rev[i].first]+v_rev[i].second);
            }
        }
    }

    int ans = 0;
    // 端以外
    for(int i=1; i<n; i++){
        for(int j=1; j<=t-1; j++){
            if(ans < dp_1[i-1][j] + dp_2[n-i-2][t-1-j] + v[i-1].second){
                ans = dp_1[i-1][j] + dp_2[n-i-2][t-1-j] + v[i-1].second;
            }
        }
    }
    // 端
    for(int j=1; j<=t-1; j++){
        if(ans < dp_2[n-1][t-1-j] + v[0].second){
            ans = dp_2[n-1][t-1-j] + v[0].second
        }
        if(ans < dp_1[n-1][t-1-j] + v[n-1].second){
            ans = dp_1[n-1][t-1-j] + v[n-1].second;
        }
    }
    cout << ans;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:29:13: error: 'dp' was not declared in this scope; did you mean 'dup'?
   29 |             dp[i][j] = 0;
      |             ^~
      |             dup
a.cc:40:35: error: no matching function for call to 'max(int&, long long int)'
   40 |                 dp_1[i+1][j] = max(dp_1[i][j], dp_1[i][j-v[i].first]+v[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:40:35: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   40 |                 dp_1[i+1][j] = max(dp_1[i][j], dp_1[i][j-v[i].first]+v[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:40:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   40 |                 dp_1[i+1][j] = max(dp_1[i][j], dp_1[i][j-v[i].first]+v[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:52:35: error: no matching function for call to 'max(int&, long long int)'
   52 |                 dp_2[i+1][j] = max(dp_2[i][j], dp_2[i][j-v_rev[i].first]+v_rev[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:52:35: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   52 |                 dp_2[i+1][j] = max(dp_2[i][j], dp_2[i][j-v_rev[i].first]+v_rev[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:52:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   52 |                 dp_2[i+1][j] = max(dp_2[i][j], dp_2[i][j-v_rev[i].first]+v_rev[i].second);
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:69:49: error: expected ';' before '}' token
   69 |             ans = dp_2[n-1][t-1-j] + v[0].second
      |                                                 ^
      |                                                 ;
   70 |         }
      |         ~