結果

問題 No.409 ダイエット
ユーザー pekempey
提出日時 2016-08-05 23:12:09
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 807 ms / 2,000 ms
コード長 707 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 161,280 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-26 09:40:55
合計ジャッジ時間 23,816 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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