結果

問題 No.324 落ちてた閉路グラフ
ユーザー parukiparuki
提出日時 2016-06-24 17:55:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 1,600 bytes
コンパイル時間 1,765 ms
コンパイル使用メモリ 172,472 KB
実行使用メモリ 144,272 KB
最終ジャッジ日時 2024-04-27 16:10:50
合計ジャッジ時間 3,510 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 80 ms
144,192 KB
testcase_05 AC 125 ms
144,180 KB
testcase_06 AC 78 ms
144,272 KB
testcase_07 AC 120 ms
144,032 KB
testcase_08 AC 80 ms
144,192 KB
testcase_09 AC 55 ms
144,168 KB
testcase_10 AC 77 ms
144,020 KB
testcase_11 AC 56 ms
144,184 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 16 ms
41,012 KB
testcase_15 AC 25 ms
56,932 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,948 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 122 ms
144,164 KB
testcase_33 AC 115 ms
144,020 KB
testcase_34 AC 67 ms
119,720 KB
testcase_35 AC 4 ms
9,988 KB
testcase_36 AC 32 ms
98,948 KB
testcase_37 AC 3 ms
7,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int N, M;
vi w;
// dp[i番目まで見た][j個使った][直前の頂点を使ったか][頂点1を使ったか]
int dp[3001][3001][2][2];
const int NG = -900000;

int main(){
    while(cin >> N >> M){
        w.resize(N);
        rep(i, N)cin >> w[i];

        // 1個も辺を入れない。
        if(M < 2){
            puts("0");
            continue;
        }
        rep(i, N + 1)rep(j, N + 1)rep(k, 2)rep(l, 2)dp[i][j][k][l] = NG;
        dp[1][0][0][0] = dp[1][1][1][1] = 0;

        FOR(i, 1, N)rep(j, M + 1)rep(preUse, 2)rep(fiUse, 2){
            int val = dp[i][j][preUse][fiUse];
            if(val <= NG)continue;
            smax(dp[i + 1][j][0][fiUse], val);
            if(j < M){
                int ad = 0;
                if(preUse)ad += w[i - 1];
                // 最初と最後をつなぐ
                if(i == N - 1 && fiUse)
                    ad += w.back();
                smax(dp[i + 1][j + 1][1][fiUse], val + ad);
            }
        }
        int ans = NG;
        rep(k, 2)rep(l, 2)smax(ans, dp[N][M][k][l]);
        cout << ans << endl;
    }
}
0