#55194: cpp_answer


yp11451202@yphs.tp.edu.tw (705-38黃鈺潤)


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

struct Girl {
    int s, v;
    double cp;
};

// 用於第一類女生的 CP 值排序
bool compareGirls(const Girl& a, const Girl& b) {
    return a.cp > b.cp;
}

void solve() {
    int n, T;
    if (!(cin >> n >> T)) return;

    vector<Girl> type1;
    vector<pair<int, int>> type2;

    for (int i = 0; i < n; ++i) {
        int s, v, p;
        cin >> s >> v >> p;
        if (p == 1) {
            type1.push_back({s, v, (double)v / s});
        } else {
            type2.push_back({s, v});
        }
    }

    // 1. 處理第二類女生 (0/1 背包 DP)
    // dp[j] 表示花費 j 時間在第二類女生身上能獲得的最大好感度
    vector<double> dp(T + 1, 0.0);
    for (auto& girl : type2) {
        for (int j = T; j >= girl.first; --j) {
            dp[j] = max(dp[j], dp[j - girl.first] + girl.second);
        }
    }

    // 2. 依照 CP 值排序第一類女生
    sort(type1.begin(), type1.end(), compareGirls);

    double max_total_v = 0;

    // 3. 枚舉分配給第二類女生的時間 j,剩下的時間給第一類女生
    for (int j = 0; j <= T; ++j) {
        double current_v = dp[j];
        int remaining_t = T - j;

        // 貪婪演算法填滿剩餘時間
        for (auto& g : type1) {
            if (remaining_t <= 0) break;
            if (remaining_t >= g.s) {
                current_v += g.v;
                remaining_t -= g.s;
            } else {
                current_v += (double)remaining_t * g.cp;
                remaining_t = 0;
            }
        }
        max_total_v = max(max_total_v, current_v);
    }

    // 四捨五入輸出
    cout << (long long)round(max_total_v) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}