#include <bits/stdc++.h>
#define range(i, l, r) for(long long int (i) = (l); (i) < (r); (i)++)
#define reversed_range(i, l, r) for (long long int (i) = (r) - 1; (i) >= l; (i)--)
using namespace std;
template <typename T>
using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;

template <typename S, typename T>
ostream& operator <<(ostream& os, pair<S, T> p) {
    os << "(";
    os << p.first << ", " << p.second;
    return os << ")";
}

template <typename T>
ostream& operator <<(ostream& os, vec<T> v) {
    os << "[";
    if (v.size() == 0) return os << "]";
    for (int i = 0; i < v.size() - 1; i++) {
        os << v.at(i) << ", ";
    }
    return os << v.at(v.size() - 1) << "]";
}

template <typename T>
ostream& operator <<(ostream& os, set<T>& s) {
    os << "{";
    if (s.begin() == s.end()) return os << "}";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename T>
ostream& operator <<(ostream& os, unordered_set<T>& s) {
    os << "{";
    if (s.begin() == s.end()) return os << "}";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, map<K, V> m) {
    os << "{";
    if (m.begin() == m.end()) return os << "}";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, unordered_map<K, V> m) {
    os << "{";
    if (m.begin() == m.end()) return os << "}";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

lint pow(lint num, lint e, lint MOD) {
    lint res = 1;
    lint cur_num = num;
    while (e) {
        if (e & 1) {
            res *= cur_num;
            res %= MOD;
        }
        cur_num *= cur_num;
        cur_num %= MOD;
        e >>= 1;
    }
    return res;
}

int main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    lint N, T; cin >> N >> T;
    vec<plint> dishes(N);
    range(i, 0, N) {
        lint time, point;
        cin >> time >> point;
        dishes.at(i) = {time, point};
    }
    // cout << dishes << "\n";
    vec<vec<lint>> prefix_dp(N, vec<lint>(T + 1, 0));
    vec<vec<lint>> suffix_dp(N, vec<lint>(T + 1, 0));

    range(j, dishes.at(0).first, T + 1) prefix_dp.at(0).at(j) = dishes.at(0).second;
    range(i, 1, N) {
        lint t, p;
        tie(t, p) = dishes.at(i);
        // cout << t << " " << p << '\n';
        range(j, 0, T + 1) {
            prefix_dp.at(i).at(j) = prefix_dp.at(i - 1).at(j);
            if (j >= t) prefix_dp.at(i).at(j) = max(prefix_dp.at(i).at(j), prefix_dp.at(i - 1).at(j - t) + p);
        }
        // cout << prefix_dp.at(i) << "\n";
    }

    range(j, dishes.at(N - 1).first, T + 1) suffix_dp.at(N - 1).at(j) = dishes.at(N - 1).second;
    reversed_range(i, 0, N - 1) {
        lint t, p;
        tie(t, p) = dishes.at(i);
        range(j, 0, T + 1) {
            suffix_dp.at(i).at(j) = suffix_dp.at(i + 1).at(j);
            if (j >= t) suffix_dp.at(i).at(j) = max(suffix_dp.at(i).at(j), suffix_dp.at(i + 1).at(j - t) + p);
        }
    }
    lint max_profit = 0;
    range(j, 0, T + 1) max_profit = max(max_profit, prefix_dp.at(N - 1).at(j));
    // cout << prefix_dp << "\n";
    // cout << suffix_dp << "\n";
    // cout << max_profit << "\n";
    // ignore first, last
    max_profit = max(max_profit, suffix_dp.at(1).at(T - 1) + dishes.at(0).second);
    max_profit = max(max_profit, prefix_dp.at(N - 2).at(T - 1) + dishes.at(N - 1).second);
    range(i, 1, N - 1) {
        lint max_profit_without_this = 0;
        range(t, 0, T) max_profit_without_this = max(
            prefix_dp.at(i - 1).at(t) + suffix_dp.at(i + 1).at(T - 1 - t),
            max_profit_without_this
        );
        max_profit = max(max_profit_without_this + dishes.at(i).second, max_profit);
    }
    cout << max_profit << "\n";
}

Measured memory_bytes: 141184