結果

問題 No.324 落ちてた閉路グラフ
ユーザー mokomoko
提出日時 2018-04-04 16:58:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 71 ms / 5,000 ms
コード長 1,921 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 145,336 KB
実行使用メモリ 74,416 KB
最終ジャッジ日時 2023-09-08 15:50:40
合計ジャッジ時間 4,336 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
74,248 KB
testcase_01 AC 36 ms
74,204 KB
testcase_02 AC 36 ms
74,144 KB
testcase_03 AC 36 ms
74,140 KB
testcase_04 AC 70 ms
74,152 KB
testcase_05 AC 70 ms
74,160 KB
testcase_06 AC 70 ms
74,156 KB
testcase_07 AC 70 ms
74,216 KB
testcase_08 AC 70 ms
74,312 KB
testcase_09 AC 69 ms
74,124 KB
testcase_10 AC 70 ms
74,172 KB
testcase_11 AC 71 ms
74,156 KB
testcase_12 AC 68 ms
74,156 KB
testcase_13 AC 56 ms
74,212 KB
testcase_14 AC 46 ms
74,148 KB
testcase_15 AC 49 ms
74,152 KB
testcase_16 AC 36 ms
74,184 KB
testcase_17 AC 35 ms
74,184 KB
testcase_18 AC 36 ms
74,116 KB
testcase_19 AC 35 ms
74,180 KB
testcase_20 AC 36 ms
74,128 KB
testcase_21 AC 36 ms
74,172 KB
testcase_22 AC 36 ms
74,128 KB
testcase_23 AC 35 ms
74,144 KB
testcase_24 AC 36 ms
74,300 KB
testcase_25 AC 35 ms
74,140 KB
testcase_26 AC 35 ms
74,164 KB
testcase_27 AC 35 ms
74,140 KB
testcase_28 AC 35 ms
74,168 KB
testcase_29 AC 36 ms
74,272 KB
testcase_30 AC 36 ms
74,152 KB
testcase_31 AC 36 ms
74,144 KB
testcase_32 AC 70 ms
74,124 KB
testcase_33 AC 70 ms
74,176 KB
testcase_34 AC 64 ms
74,416 KB
testcase_35 AC 37 ms
74,116 KB
testcase_36 AC 58 ms
74,324 KB
testcase_37 AC 37 ms
74,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int n,m,w[3010];
int dp[3010][3010][2]; //頂点iまででj個の頂点を使ったときの最大値。kは頂点iを使ったかどうか。
int ans=-INF;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	REP(i,3010) FOR(j,0,3010) dp[i][j][0]=dp[i][j][1]=-INF;

	cin>>n>>m;
	REP(i,n) cin>>w[i+1];

	// 頂点1を使わない
	dp[1][0][0]=0;
	FOR(i,1,n+1) {
		FOR(j,0,3001) {
			// 頂点i+1を使う
			dp[i+1][j+1][1]=max(dp[i+1][j+1][1],max(dp[i][j][0],dp[i][j][1]+w[i]));
			// 頂点i+1を使わない
			dp[i+1][j][0]=max(dp[i+1][j][0],max(dp[i][j][0],dp[i][j][1]));
		}
		ans=max(ans,dp[i][m][0]);
	}
	ans=max(ans,dp[n+1][m][0]);

	/*FOR(i,1,n+2) {
		REP(j,n+1) cout<<(dp[i][j][0]>=0?dp[i][j][0]:-1)<<' ';
		cout<<'|';
		REP(j,n+1) cout<<(dp[i][j][1]>=0?dp[i][j][1]:-1)<<' ';
		cout<<endl;
	}
	cout<<endl;*/

	REP(i,3010) FOR(j,0,3010) dp[i][j][0]=dp[i][j][1]=-INF;

	// 頂点1を使う。
	dp[1][1][1]=0;
	FOR(i,1,n+1) {
		FOR(j,1,3001) {
			// 頂点iを使う
			dp[i+1][j+1][1]=max(dp[i+1][j+1][1],max(dp[i][j][0],dp[i][j][1]+w[i]));
			// 頂点iを使わない
			dp[i+1][j][0]=max(dp[i+1][j][0],max(dp[i][j][0],dp[i][j][1]));
		}
		if(i==n) ans=max(ans,dp[n][m][1]+w[n]);
		else ans=max(ans,dp[i][m][1]);
	}

	/*FOR(i,1,n+2) {
		REP(j,n+1) cout<<(dp[i][j][0]>=0?dp[i][j][0]:-1)<<' ';
		cout<<'|';
		REP(j,n+1) cout<<(dp[i][j][1]>=0?dp[i][j][1]:-1)<<' ';
		cout<<endl;
	}*/

	cout<<ans<<endl;

	return 0;
}
0