結果

問題 No.409 ダイエット
ユーザー lmxyue
提出日時 2025-08-25 11:02:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 697 ms / 2,000 ms
コード長 931 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 198,140 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-08-25 11:03:16
合計ジャッジ時間 22,823 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:15:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |         for (int i = 0; i < N; i++) scanf("%lld", &D[i]);
      |                                     ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

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

const long long inf = 1e16;

template<class T1, class T2>
void chmin(T1 &a, T2 b) {
        if (b < a) a = b;
}

int main() {
        long long N, A, B, W;
        cin >> N >> A >> B >> W;
        vector<long long> D(N);
        for (int i = 0; i < N; i++) scanf("%lld", &D[i]);

        if (B == 0) {
                cout << W - N * A << endl;
                return 0;
        }

        static long long dp0[1010], dp1[1010];
        fill_n(dp0, 1010, inf);
        dp0[0] = 0;

        for (int i = 0; i < N; i++) {
                fill_n(dp1, 1010, inf);
                for (int j = 0; j < 1007; j++) {
                        chmin(dp1[0], dp0[j] + D[i]);
                        chmin(dp1[j + 1], dp0[j] + B * (j + 1) - A);
                }
                swap(dp0, dp1);
        }

        long long mini = *min_element(dp0, dp0 + 1010);
        cout << W + mini << endl;
}
0