Language: C++

//#pragma GCC optimize("O3")
//#pragma GCC target("avx")

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// Types
using ll = long long int;
using str = string;
template <class T> using u_set = unordered_set<T>;
template <class K, class V> using u_map = unordered_map<K, V>;
template <class V> using graph = u_map<V, u_set<V>>;

namespace jumpaku::io {
// Input
using IS = istream;
IS &in(IS &);
IS &in(IS &i) { return i; }
template <class T, class... Ts> IS &in(IS &i, T &a, Ts &... as) {
  return in(i >> a, as...);
}
template <class V> IS &operator>>(IS &i, vector<V> &a) {
  for_each(a.begin(), a.end(), [&i](auto &e) { i >> e; });
  return i;
}
template <class F, class S> IS &operator>>(IS &i, pair<F, S> &p) {
  return in(i, p.first, p.second);
}
template <class... Ts, size_t... I>
IS &tuple_in(IS &i, tuple<Ts...> &t, index_sequence<I...>) {
  return in(i, get<I>(t)...);
}
template <class... Ts> IS &operator>>(IS &i, tuple<Ts...> &t) {
  return tuple_in(i, t, make_index_sequence<sizeof...(Ts)>());
}
// Output
using OS = ostream;
OS &out(OS &o) { return o << endl; }
template <class T> OS &out(OS &o, T const &a) { return o << a << endl; }
template <class T, class... Ts> OS &out(OS &o, T const &a, Ts const &... as) {
  return out(o << a << " ", as...);
}
template <class F, class S> OS &operator<<(OS &o, pair<F, S> const &p) {
  return o << p.first << ":" << p.second;
}
template <class... Ts> OS &args_out(OS &o, Ts const &... ts);
OS &args_out(OS &o) { return o; }
template <class T> OS &args_out(OS &o, T const &t) { return o << t; }
template <class T0, class T1, class... Ts>
OS &args_out(OS &o, T0 const &t0, T1 const &t1, Ts const &... ts) {
  return args_out(o << t0 << ",", t1, ts...);
}
template <class... Ts, size_t... I>
OS &tuple_out(OS &o, tuple<Ts...> const &t, index_sequence<I...>) {
  return args_out(o, get<I>(t)...);
}
template <class... Ts> OS &operator<<(OS &o, tuple<Ts...> const &t) {
  return tuple_out(o << "(", t, make_index_sequence<sizeof...(Ts)>()) << ")";
}
template <
    class C,
    class T = typename iterator_traits<typename C::iterator>::value_type,
    typename enable_if<!is_same<C, str>::value, nullptr_t>::type = nullptr>
OS &operator<<(OS &o, C const &a) {
  return a.empty() ? (o << "[]") : ([&o, &a]() -> OS & {
    o << "[" << *a.begin();
    for_each(next(a.begin()), a.end(), [&o](auto const &e) { o << "," << e; });
    return o << "]";
  }());
}
} // namespace jumpaku::io
auto input = [](auto &... a) { jumpaku::io::in(cin, a...); };
auto print = [](auto const &... a) { jumpaku::io::out(cout, a...); };
#ifdef JUMPAKU_DEBUG
auto dump = [](auto const &... a) { jumpaku::io::out(cerr, a...); };
#else
auto dump = [](auto const &...) {};
#endif

// Hash
namespace jumpaku::hashcode {
template <class... Ts> size_t hash_args(size_t h, Ts const &... ts);
constexpr size_t hash_args(size_t h) { return h; }
template <class T, class... Ts>
constexpr size_t hash_args(size_t h, T const &t, Ts const &... ts) {
  return hash_args(((h << 10) - h + hash<T>{}(t)), ts...);
}
template <class... Ts, size_t... I>
constexpr size_t hash_tuple(tuple<Ts...> const &t, index_sequence<I...>) {
  return hash_args(17, get<I>(t)...);
}
} // namespace jumpaku::hashcode
namespace std {
template <class... Ts> struct hash<tuple<Ts...>> {
  constexpr hash<tuple<Ts...>>() = default;
  constexpr size_t operator()(tuple<Ts...> const &t) const {
    return jumpaku::hashcode::hash_tuple(t, index_sequence_for<Ts...>());
  }
};
} // namespace std

// Range
namespace jumpaku::ranges {
template <class T> struct seq {
  struct iterator {
    using difference_type = ll;
    using value_type = T;
    using pointer = T *;
    using reference = T &;
    using iterator_category = random_access_iterator_tag;
    seq<T> const *s;
    ll i;
    iterator(seq const *s, ll const &i = 0) : s(s), i(i) {}
    bool operator==(iterator const &itr) const { return i == itr.i; }
    bool operator!=(iterator const &itr) const { return i != itr.i; }
    iterator &operator++() { return *this += 1; }
    iterator operator++(int) { return iterator(s, i++); }
    iterator &operator--() { return *this -= 1; }
    iterator operator--(int) { return iterator(s, i--); }
    iterator operator+(ll const &n) const { return iterator(s, i + n); }
    iterator operator-(ll const &n) const { return iterator(s, i - n); }
    ll operator-(iterator const &itr) const { return i - itr.i; }
    iterator &operator+=(ll const &n) {
      i += n;
      return *this;
    }
    iterator &operator-=(ll const &n) { return *this += (-n); }
    T const operator*() const { return (*s)[i]; }
  };
  function<T(ll)> const f;
  ll const l;
  seq(function<T(ll)> const &f, ll const &l) : f(f), l(l) {}
  iterator begin() const { return iterator(this, 0); }
  iterator end() const { return iterator(this, l); }
  ll size() const { return l; }
  bool empty() const { return size() == 0; }
  template <class F> auto map(F const &g) const {
    return seq<decltype(g(f(declval<ll>())))>(
        [g, f = f](auto i) { return g(f(i)); }, l);
  }
  virtual T operator[](ll i) const { return f(i); }
};
} // namespace jumpaku::ranges

struct range : jumpaku::ranges::seq<ll> {
  ll const begin_;
  range(ll end) : range(0LL, end) {}
  range(ll begin, ll end)
      : seq<ll>([begin](auto i) { return i + begin; }, end - begin),
        begin_(begin) {}
  ll operator[](ll i) const override { return i + begin_; }
};
namespace jumpaku::ranges {
range endless = range(-1LL);
}
range::iterator begin(ll i = 0LL) {
  return range::iterator(&jumpaku::ranges::endless, i);
}
range::iterator end(ll i) { return begin(i); }
template <class T = ll> vector<T> vec(size_t n, T &&init = T()) {
  return vector<T>(n, init);
}
ll gcd(ll p, ll q) { return (q == 0) ? p : gcd(q, p % q); }
ll lcm(ll p, ll q) { return p / gcd(q, p) * q; }

// MOD
ll pow(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  if (n & 1)
    return ((a % m) * pow(a, n - 1, m)) % m;
  auto b = pow(a, n / 2, m);
  return (b * b) % m;
}
ll inv(ll a, ll p) { return pow(a, p - 2, p); }

template <ll N, ll M> struct Factrial {
  Factrial() {
    copy(begin(0), end(N + 1), fact.begin());
    fact.front() = 1;
    partial_sum(fact.begin(), fact.end(), fact.begin(),
                [&](auto acc, auto i) { return (acc * i) % M; });
    copy(begin(0), end(N + 1), finv.begin());
    finv.back() = inv(fact[N], M);
    partial_sum(finv.rbegin(), finv.rend(), finv.rbegin(),
                [](auto acc, auto i) { return (acc * (i + 1)) % M; });
  }
  array<ll, N + 1> fact;
  array<ll, N + 1> finv;
  ll comb(ll n, ll m) const {
    if (m < 0 || n < m)
      return 0LL;
    return (fact[n] * ((finv[m] * finv[n - m]) % M)) % M;
  }
};

constexpr ll MOD = 1e9 + 7;

using Key = tuple<ll, ll>;
ll f(Key const &key, vector<tuple<ll, ll>> const &AB, u_map<Key, ll> &dp) {
  ll t = get<0>(key);
  ll i = get<1>(key);
  if (t <= 0)
    return 0;
  auto Ai = get<0>(AB[i]);
  auto Bi = get<1>(AB[i]);
  if (i == 0)
    return Bi;
  // if (dp.contains(key))
  if (dp.find(key) != dp.end())
    return dp[key];
  return dp[key] =
             max(f(Key{t - Ai, i - 1}, AB, dp) + Bi, f(Key{t, i - 1}, AB, dp));
}
int main() {
  ll N, T;
  input(N, T);
  auto AB = vec<tuple<ll, ll>>(N);
  input(AB);
  sort(AB.begin(), AB.end(),
       [](auto const &x0, auto const &x1) { return get<0>(x0) > get<0>(x1); });
  // dump(N, T, AB);
  auto dp = u_map<Key, ll>{};
  dp.reserve(1e7);
  print(f(Key{T, N - 1}, AB, dp));
}

Compiler output:
a.cc: In instantiation of 'jumpaku::io::IS& jumpaku::io::in(IS&, T&, Ts& ...) [with T = std::vector<std::tuple<long long int, long long int> >; Ts = {}; IS = std::basic_istream<char>]':
a.cc:91:47:   required from '<lambda(auto:3& ...)> [with auto:3 = {std::vector<std::tuple<long long int, long long int>, std::allocator<std::tuple<long long int, long long int> > >}]'
   91 | auto input = [](auto &... a) { jumpaku::io::in(cin, a...); };
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~
a.cc:238:8:   required from here
  238 |   input(AB);
      |   ~~~~~^~~~
a.cc:39:15: error: no match for 'operator>>' (operand types are 'jumpaku::io::IS' {aka 'std::basic_istream<char>'} and 'std::vector<std::tuple<long long int, long long int> >')
   39 |   return in(i >> a, as...);
      |             ~~^~~~
In file included from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/bits/quoted_string.h:38,
                 from /usr/include/c++/14/iomanip:50,
                 from a.cc:7:
/usr/include/c++/14/istream:170:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  170 |       operator>>(bool& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:170:24: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'bool&'
  170 |       operator>>(bool& __n)
      |                  ~~~~~~^~~
/usr/include/c++/14/istream:174:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]'
  174 |       operator>>(short& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:174:25: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'short int&'
  174 |       operator>>(short& __n);
      |                  ~~~~~~~^~~
/usr/include/c++/14/istream:177:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  177 |       operator>>(unsigned short& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:177:34: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'short unsigned int&'
  177 |       operator>>(unsigned short& __n)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/istream:181:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]'
  181 |       operator>>(int& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:181:23: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'int&'
  181 |       operator>>(int& __n);
      |                  ~~~~~^~~
/usr/include/c++/14/istream:184:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  184 |       operator>>(unsigned int& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:184:32: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'unsigned int&'
  184 |       operator>>(unsigned int& __n)
      |                  ~~~~~~~~~~~~~~^~~
/usr/include/c++/14/istream:188:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  188 |       operator>>(long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:188:24: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'long int&'
  188 |       operator>>(long& __n)
      |                  ~~~~~~^~~
/usr/include/c++/14/istream:192:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  192 |       operator>>(unsigned long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:192:33: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'long unsigned int&'
  192 |       operator>>(unsigned long& __n)
      |                  ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/istream:199:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  199 |       operator>>(long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:199:29: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'long long int&'
  199 |       operator>>(long long& __n)
      |                  ~~~~~~~~~~~^~~
/usr/include/c++/14/istream:203:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  203 |       operator>>(unsigned long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:203:38: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'long long unsigned int&'
  203 |       operator>>(unsigned long long& __n)
      |                  ~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/istream:219:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  219 |       operator>>(float& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:219:25: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'float&'
  219 |       operator>>(float& __f)
      |                  ~~~~~~~^~~
/usr/include/c++/14/istream:223:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  223 |       operator>>(double& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:223:26: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'double&'
  223 |       operator>>(double& __f)
      |                  ~~~~~~~~^~~
/usr/include/c++/14/istream:227:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  227 |       operator>>(long double& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:227:31: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'long double&'
  227 |       operator>>(long double& __f)
      |                  ~~~~~~~~~~~~~^~~
/usr/include/c++/14/istream:328:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  328 |       operator>>(void*& __p)
      |       ^~~~~~~~
/usr/include/c++/14/istream:328:25: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'void*&'
  328 |       operator>>(void*& __p)
      |                  ~~~~~~~^~~
/usr/include/c++/14/istream:122:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(__istream_type& (*)(__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  122 |       operator>>(__istream_type& (*__pf)(__istream_type&))
      |       ^~~~~~~~
/usr/include/c++/14/istream:122:36: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)' {aka 'std::basic_istream<char>& (*)(std::basic_istream<char>&)'}
  122 |       operator>>(__istream_type& (*__pf)(__istream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/istream:126:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(__ios_type& (*)(__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>; __ios_type = std::basic_ios<char>]'
  126 |       operator>>(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/include/c++/14/istream:126:32: note:   no known conversion for argument 1 from 'std::vector<std::tuple<long long int, long long int> >' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&)' {aka 'std::basic_ios<char>& (*)(std::basic_ios<char>&)'}
  126 |       operator>>(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/istream:133:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  133 |       operator>>(ios_base& (*__pf)(ios_