結果

問題 No.324 落ちてた閉路グラフ
ユーザー koyumeishikoyumeishi
提出日時 2015-12-17 10:53:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 1,198 bytes
コンパイル時間 809 ms
コンパイル使用メモリ 81,212 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-09 21:36:40
合計ジャッジ時間 2,713 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 45 ms
4,380 KB
testcase_05 AC 75 ms
4,380 KB
testcase_06 AC 39 ms
4,380 KB
testcase_07 AC 73 ms
4,380 KB
testcase_08 AC 41 ms
4,376 KB
testcase_09 AC 21 ms
4,376 KB
testcase_10 AC 41 ms
4,376 KB
testcase_11 AC 21 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 13 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 75 ms
4,380 KB
testcase_33 AC 71 ms
4,376 KB
testcase_34 AC 37 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 13 ms
4,380 KB
testcase_37 AC 2 ms
4,376 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)));
	
	dp[0][0][0] = 0;
	dp[1][1][1] = 0;
	
	for(int i=1; i<n; i++){
		vector<vector<vector<int>>> dp_(2,vector<vector<int>>(2,vector<int>(m+1, -inf)));
		
		for(int start=0; start<2; start++)
		for(int  last=0;  last<2;  last++)
		for(int k=0; 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);
			}
		}
		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