結果

問題 No.561 東京と京都
ユーザー らーゆ
提出日時 2024-05-05 16:40:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 727 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 67,176 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-27 15:46:34
合計ジャッジ時間 1,517 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

using namespace std;
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long ll;
int N,D;
int T[109], K[109];
ll dp[109][2];
ll fanc(int i, int j) {
	ll res = -1;
	if (dp[i][j] != -1) {
		return dp[i][j];
	}
	if (i == 1 and j == 0) res = T[i];
	if (i == 1 and j == 1) res = K[i] - D;
	if (i >= 2 and fanc(i - 1, j) != -1 and fanc(i - 1, (j + 1) % 2) != -1) {
		if(j==0) res = max(fanc(i - 1, j) + T[i], fanc(i - 1, (j + 1) % 2) + T[i] - D);
		else res= max(fanc(i - 1, j) +K[i], fanc(i - 1, (j + 1) % 2) + K[i] - D);
	}
	return dp[i][j]=res;
}
int main() {
	cin >> N >> D;
	for (int i = 1; i <= N; i++) {
		cin >> T[i] >> K[i];
	}
	memset(dp, -1, sizeof(dp));
	cout << max(fanc(N, 0), fanc(N, 1)) << endl;;
}
0