結果

問題 No.324 落ちてた閉路グラフ
ユーザー koyumeishikoyumeishi
提出日時 2015-12-17 10:57:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,234 bytes
コンパイル時間 811 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 16:00:38
合計ジャッジ時間 2,218 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
template<class T> istream& operator >> (istream& is, vector<T>& vec){ for(T& val: vec) is >> val; return is;}

int main(){
	int n,m;
	cin >> n >> m;
	vector<int> w(n);
	cin >> w;
	if(m==0){
		cout << 0 << endl;
		return 0;
	}
	
	const int inf = 100000000;
	
	vector<vector<vector<int>>> dp (2,vector<vector<int>>(2,vector<int>(m+1, -inf)));
	vector<vector<vector<int>>> dp_(2,vector<vector<int>>(2,vector<int>(m+1, -inf)));
	
	dp[0][0][0] = 0;
	dp[1][1][1] = 0;
	
	for(int i=1; i<n; i++){
		for(int start=0; start<2; start++)
		for(int last=0; last<2; last++)
		for(int k=max(0,m-(n-i)); k<=min(i,m); k++){
			if(dp[start][last][k] <= -inf) continue;
			dp_[start][0][k] = max(dp_[start][0][k], dp[start][last][k]);
			if(k+1<=m){
				int next = dp[start][last][k];
				if(last){
					next += w[i-1];
				}
				dp_[start][1][k+1] = max(dp_[start][1][k+1], next);
			}
			dp[start][last][k] = -inf;
		}
		swap(dp, dp_);
	}
	int ans = max({dp[0][0][m], dp[0][1][m], dp[1][0][m], dp[1][1][m]+w[n-1]});
	
	cout << ans << endl;
	return 0;
}
0