結果

問題 No.2701 A cans -> B cans
ユーザー kokoseikokosei
提出日時 2024-03-29 22:42:47
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 145 ms / 1,000 ms
コード長 1,208 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 79,744 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-09-30 16:33:55
合計ジャッジ時間 7,834 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 73
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
using ll = long long;
ll inf = 1e18;

int N, M;
int A[5050], B[5050]; ll C[5050];
ll dp[5050][2], ndp[5050][2];
template<typename A, int N, typename T>
void Fill(A (&arr)[N], const T &val){
    fill((T*)arr, (T*)(arr + N), val);
}
template<typename T>
void chmax(T &a, T b){
    a = max(a, b);
}
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> N >> M;
    for(int i = 0;i < N;i++)cin >> A[i] >> B[i] >> C[i];
    Fill(dp, -inf);
    Fill(ndp, -inf);
    dp[0][0] = 0;
    for(int i = 0;i < N;i++){
        for(int j = 0;j <= M;j++){
            for(int k = 0;k < 2;k++){
                int nm = j + A[i] - k * B[i];
                chmax(ndp[j][k], dp[j][k]);
                if(nm <= M){
                    chmax(ndp[nm][1], ndp[j][k] + B[i] * C[i]);
                }
            }
        }
        for(int j = 0;j <= M;j++){
            chmax(ndp[j][0], ndp[j][1]);
            ndp[j][1] = -inf;
        }
        swap(dp, ndp);
        Fill(ndp, -inf);
    }
    for(int i = 1;i <= M;i++){
        chmax(dp[i][0], dp[i - 1][0]);
    }
    for(int i = 1;i <= M;i++){
        cout << dp[i][0] << "\n";
    }
    return 0;
}
0