結果

問題 No.324 落ちてた閉路グラフ
ユーザー nebukuro09
提出日時 2017-04-13 17:50:17
言語 D
(dmd 2.109.1)
結果
MLE  
実行時間 -
コード長 1,438 bytes
コンパイル時間 735 ms
コンパイル使用メモリ 115,472 KB
実行使用メモリ 813,212 KB
最終ジャッジ日時 2024-06-12 18:41:23
合計ジャッジ時間 6,697 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 1 MLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable int INF = 1 << 29;
void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto W = readln.split.map!(to!int).array;

    if (M == 0) {
        writeln(0);
        return;
    }

    // dp[i][j][k][l]  頂点iまででj個の辺をとったときの最大値
    // k: 直前の頂点を取ったか?
    // l: 頂点0を取ったか?
    auto dp = new int[][][][](N+1, M+1, 2, 2);
    foreach (i; 0..N+1) foreach (j; 0..M+1) foreach(k; 0..2) fill(dp[i][j][k], -INF);
    dp[0][0][0][0] = 0;
    dp[0][1][1][1] = 0;

    foreach (i; 1..N) {
        foreach (j; 0..M+1) {
            foreach (k; 0..2) {
                foreach (l; 0..2) {
                    if (dp[i-1][j][k][l] == -INF) continue;
                    int take = 0;
                    if (k) take += W[i-1];
                    if (i == N-1 && l) take += W[N-1];

                    if (j < M) dp[i][j+1][1][l] =
                                   max(dp[i][j+1][1][l], dp[i-1][j][k][l] + take);
                    dp[i][j][0][l] = max(dp[i][j][0][l], dp[i-1][j][k][l]);
                }
            }
        }
    }


    int ans = -INF;
    foreach (k; 0..2) foreach (l; 0..2) ans = max(ans, dp[N-1][M][k][l]);
    ans.writeln;
}
0