結果

問題 No.974 最後の日までに
ユーザー betrue12
提出日時 2020-01-17 23:41:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,685 bytes
コンパイル時間 3,387 ms
コンパイル使用メモリ 204,432 KB
最終ジャッジ日時 2025-01-08 19:26:33
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int W;
    cin >> W;
    vector<int64_t> A(W), B(W), C(W);
    for(int i=0; i<W; i++) cin >> A[i] >> B[i] >> C[i];

    auto calculate_cand = [&](int H, int offset){
        vector<pair<int64_t, int64_t>> res;
        res.emplace_back(0, 0);
        for(int d=0; 2*d<=H; d++){
            vector<int> order;
            for(int i=0; i<H-2*d; i++) order.push_back(0);
            for(int i=0; i<d; i++) order.push_back(1);
            do{
                int64_t money = 0, love = 0;
                int pt = offset;
                for(int t : order){
                    if(t){
                        money -= C[pt+1];
                        love += B[pt+1];
                        pt += 2;
                    }else{
                        money += A[pt];
                        pt += 1;
                    }
                }
                res.emplace_back(money, love);
            }while(next_permutation(order.begin(), order.end()));
        }
        sort(res.begin(), res.end());
        return res;
    };

    int H = W/2;
    int H2 = W-H;
    int64_t ans = 0;
    for(int k=0; k<2; k++){
        auto res1 = calculate_cand(H, 0);
        auto res2 = calculate_cand(H2, H);
        int sz = res2.size();
        int pt = sz-1;
        int64_t mx = -1e18;
        for(auto& p : res1){
            int64_t money = p.first, love = p.second;
            while(pt >= 0 && money + res2[pt].first >= 0){
                mx = max(mx, res2[pt].second);
                pt--;
            }
            ans = max(ans, love + mx);
        }
        H++;
        H2--;
    }
    cout << ans << endl;
    return 0;
}
0