#include <iostream>
#include <type_traits>
#include <vector>
#include <cstdint>
#include <cassert>
#include <set>
#include <limits>
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }

using i32   = int32_t;
using i64   = int64_t;
using u32   = uint32_t;
using u64   = uint64_t;
using uint  = unsigned int;
using usize = std::size_t;
using ll    = long long;
using ull   = unsigned long long;
using ld    = long double;
template<typename T, usize n>
using arr = T (&)[n];
template<typename T, usize n>
using c_arr = const T (&)[n];
template<typename T>
T in()
{
    T v;
    return std::cin >> v, v;
}
template<typename T, typename Uint, usize n, usize i>
T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) { return in<T>(); }
template<typename T, typename Uint, usize n, usize i>
auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type& szs)
{
    const usize s = (usize)szs[i];
    std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s);
    for (usize j = 0; j < s; j++) { ans[j] = in_v<T, Uint, n, i + 1>(szs); }
    return ans;
}
template<typename T, typename Uint, usize n>
auto in_v(c_arr<Uint, n> szs) { return in_v<T, Uint, n, 0>(szs); }
template<typename... Types>
auto in_t() { return std::tuple<std::decay_t<Types>...>{in<Types>()...}; }

template<typename T>
void out(const T& v) { std::cout << v; }
template<typename T>
void out(const std::vector<T>& v)
{
    for (usize i = 0; i < v.size(); i++) {
        if (i > 0) { std::cout << ' '; }
        out(v[i]);
    }
    std::cout << "\n";
}
template<typename T1, typename T2>
void out(const std::pair<T1, T2>& v) { out(v.first), std::cout << ' ', out(v.second); }
template<typename T, typename... Args>
void out(const T& v, const Args... args) { out(v), std::cout << ' ', out(args...); }
template<typename... Args>
void outln(const Args... args) { out(args...), std::cout << '\n'; }
template<typename... Args>
void outel(const Args... args) { out(args...), std::cout << std::endl; }
constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }

template<typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type, const T& v = T{}) { return v; }
template<typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs, const T& v = T{})
{
    const usize s = (usize)szs[i];
    return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(s, make_v<T, Uint, n, i + 1>(szs, v));
}
template<typename T, typename Uint, usize n>
auto make_v(c_arr<Uint, n> szs, const T& t = T{}) { return make_v<T, Uint, n, 0>(szs, t); }

constexpr unsigned int mod                  = 1000000007;
template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename Elem, typename Val = Elem>
class dynamic_cht
{
private:
    struct line
    {
        Elem a, b;
        bool query;
        mutable const line* next = nullptr;
        bool operator<(const line& l) const
        {
            if (not l.query) { return a > l.a; }
            if (not next) { return false; }
            return static_cast<Val>(next->b - b) < static_cast<Val>(a - next->a) * static_cast<Val>(l.a);
        }
        friend std::ostream& operator<<(std::ostream& os, const line& l) { return (os << l.a << "x+" << l.b); }
    };
    bool needless(const typename std::multiset<line>::iterator y) const
    {
        const auto z = std::next(y);
        if (y == lines.begin()) { return z != lines.end() and y->a == z->a and y->b >= z->b; }
        const auto x = std::prev(y);
        return z == lines.end() ? y->a == x->a and y->b >= x->b : static_cast<Val>(x->b - y->b) * static_cast<Val>(z->a - y->a) >= static_cast<Val>(y->b - z->b) * static_cast<Val>(y->a - x->a);
    }
    std::multiset<line> lines;

public:
    void add(const Elem a, const Elem b)
    {
        auto y = lines.insert({a, b, false});
        if (needless(y)) { return lines.erase(y), static_cast<void>(0); }
        auto z = std::next(y);
        while (z != lines.end() and needless(z)) { z = lines.erase(z); }
        if (z != lines.end()) { y->next = &*z; }
        while (y != lines.begin() and needless(z = std::prev(y))) { lines.erase(z); }
        if (y != lines.begin()) { z->next = &*y; }
    }
    Val query(const Elem x) const
    {
        if (lines.empty()) { return inf_v<Val>; }
        const auto l = *lines.lower_bound(line{x, 0, true});
        return static_cast<Val>(l.a) * static_cast<Val>(x) + static_cast<Val>(l.b);
    }
    bool empty() const { return lines.empty(); }
    friend std::ostream& operator<<(std::ostream& os, const dynamic_cht& cht)
    {
        os << "[";
        usize i = 0;
        for (const auto& l : cht.lines) { os << l << (i + 1 == cht.lines.size() ? "" : ","), i++; }
        return (os << "]\n");
    }
};
int main()
{
    const auto N      = in<usize>();
    const auto T      = in<usize>();
    constexpr usize F = TEN(4);
    std::vector<std::vector<usize>> ts(F + 1);
    std::vector<std::vector<ll>> ps(F + 1);
    for (usize i = 0; i < N; i++) {
        const auto t = in<usize>();
        const auto p = in<ll>();
        const auto f = in<usize>();
        ts[f].push_back(t), ps[f].push_back(p);
    }
    auto dp  = make_v({F + 1, T + 1}, inf_v<ll>);
    dp[0][0] = 0;
    std::vector<dynamic_cht<ll, __int128_t>> chts(T + 1);  // dp[f][t]からの直線 y=-2fx+(dp[f][t]+f*f)
    for (usize f = 1; f <= F; f++) {
        if (ts[f].empty()) { continue; }
        auto Q  = make_v({T + 1, 2UL}, inf_v<ll>);
        Q[0][0] = 0;
        for (usize i = 0; i < ts[f].size(); i++) {
            const usize t = ts[f][i];
            const ll p    = ps[f][i];
            for (usize j = T - t; (int)j >= 0; j--) {
                for (int k = 0; k < 2; k++) { chmin(Q[j + t][1], Q[j][k] - p); }
            }
        }
        for (usize t = 0; t <= T; t++) { chmin(dp[f][t], Q[t][1]); }
        auto P = make_v({T + 1, 2UL}, inf_v<ll>);
        for (usize t = 0; t <= T; t++) {
            if (chts[t].empty()) { continue; }
            P[t][0] = chts[t].query(f);
        }
        for (usize i = 0; i < ts[f].size(); i++) {
            const usize t = ts[f][i];
            const ll p    = ps[f][i];
            for (usize j = T - t; (int)j >= 0; j--) {
                for (int k = 0; k < 2; k++) { chmin(P[j + t][1], P[j][k] - p); }
            }
        }
        for (usize t = 0; t <= T; t++) { chmin(dp[f][t], P[t][1] + (ll)f * (ll)f); }
        for (usize t = 0; t <= T; t++) {
            if (dp[f][t] >= inf_v<ll>) { continue; }
            chts[t].add(-2LL * f, dp[f][t] + f * f);
        }
    }
    ll ans = -inf_v<ll>;
    for (usize f = 0; f <= F; f++) {
        for (usize t = 0; t <= T; t++) { chmax(ans, -dp[f][t]); }
    }
    outln(ans);

    return 0;
}



Measured memory_bytes: 319368