結果

問題 No.324 落ちてた閉路グラフ
ユーザー furafura
提出日時 2020-07-12 03:02:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 1,042 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 197,224 KB
実行使用メモリ 144,512 KB
最終ジャッジ日時 2024-04-21 21:08:25
合計ジャッジ時間 4,730 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 67 ms
81,920 KB
testcase_05 AC 127 ms
144,384 KB
testcase_06 AC 58 ms
71,680 KB
testcase_07 AC 124 ms
144,384 KB
testcase_08 AC 60 ms
75,136 KB
testcase_09 AC 36 ms
47,360 KB
testcase_10 AC 60 ms
75,008 KB
testcase_11 AC 36 ms
47,232 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 13 ms
17,408 KB
testcase_14 AC 14 ms
20,864 KB
testcase_15 AC 23 ms
32,768 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 126 ms
144,512 KB
testcase_33 AC 115 ms
138,752 KB
testcase_34 AC 56 ms
71,168 KB
testcase_35 AC 4 ms
5,504 KB
testcase_36 AC 23 ms
30,592 KB
testcase_37 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

int main(){
	int n,m; scanf("%d%d",&n,&m);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	if(m==0) return puts("0"),0;

	// 頂点 0 を選ばない
	static int dp1[3000][3001][2];
	rep(i,n) rep(j,m+1) rep(k,2) dp1[i][j][k]=-INF;
	dp1[0][0][0]=0;
	for(int i=1;i<n;i++){
		rep(j,m+1){
			// 頂点 i を選ばない
			dp1[i][j][0]=max(dp1[i-1][j][0],dp1[i-1][j][1]);
			// 頂点 i を選ぶ
			if(j<m) dp1[i][j+1][1]=max(dp1[i-1][j][0],dp1[i-1][j][1]+a[i-1]);
		}
	}

	// 頂点 0 を選ぶ
	static int dp2[3000][3001][2];
	rep(i,n) rep(j,m+1) rep(k,2) dp2[i][j][k]=-INF;
	dp2[0][1][1]=0;
	for(int i=1;i<n;i++){
		rep(j,m+1){
			// 頂点 i を選ばない
			dp2[i][j][0]=max(dp2[i-1][j][0],dp2[i-1][j][1]);
			// 頂点 i を選ぶ
			if(j<m) dp2[i][j+1][1]=max(dp2[i-1][j][0],dp2[i-1][j][1]+a[i-1])+(i==n-1?a[n-1]:0);
		}
	}

	printf("%d\n",max({dp1[n-1][m][0],dp1[n-1][m][1],dp2[n-1][m][0],dp2[n-1][m][1]}));

	return 0;
}
0