結果

問題 No.324 落ちてた閉路グラフ
ユーザー FF256grhyFF256grhy
提出日時 2015-12-17 04:46:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 987 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 26,780 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-09 21:36:02
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 12 ms
4,384 KB
testcase_05 AC 12 ms
4,376 KB
testcase_06 AC 13 ms
4,380 KB
testcase_07 AC 13 ms
4,380 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 13 ms
4,376 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 12 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,384 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,388 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 13 ms
4,384 KB
testcase_33 AC 12 ms
4,376 KB
testcase_34 AC 9 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 6 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int n, m, w[3000];
int dp[3001][4];

int max(int a, int b) { return (a > b ? a : b); }

int main(void) {
	scanf("%d%d", &n, &m);
	
	if(m < 2) { printf("0\n"); return 0; }
	
	int i, j;
	for(i = 0; i < n; i++) {
		scanf("%d", &w[i]);
	}
	
	dp[0][3] = w[0];
	for(i = 2; i < n; i++) {
	for(j = i - 1; 0 <= j; j--) {
		int a = dp[j - 1][3], b = dp[j - 1][1];
		int w1 = w[i - 1], w2 = (i == n - 1 ? w[i] : 0);
		if(j == i - 1) {
			dp[j][3] = a + w1 + w2;
			dp[j][2] = a;
			dp[j][1] = b + w1;
			dp[j][0] = b;
		} else if(j == 0) {
			dp[0][3] = w2;
		} else {
			dp[j][3] = max(dp[j][2], a + w1) + w2;
			dp[j][2] = max(dp[j][2], a);
			dp[j][1] = max(dp[j][0], b + w1);
			dp[j][0] = max(dp[j][0], b);
		}
	}
	}
	
	int ans;
	if(m == n) { ans = dp[m - 2][3]; }
	else if(m == n - 1) { ans = max( max(dp[m - 1][1], dp[m - 1][2]), dp[m - 2][3] ); }
	else { ans =  max( max(dp[m][0], dp[m - 1][1]), max(dp[m - 1][2], dp[m - 2][3]) ); }
	printf("%d\n", ans);
	
	return 0;
}
0