結果

問題 No.31 悪のミックスジュース
ユーザー te-shte-sh
提出日時 2017-04-13 16:20:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 1,049 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 105,552 KB
実行使用メモリ 6,792 KB
最終ジャッジ日時 2023-09-03 12:46:14
合計ジャッジ時間 2,426 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 57 ms
6,716 KB
testcase_02 AC 85 ms
6,792 KB
testcase_03 AC 119 ms
6,752 KB
testcase_04 AC 42 ms
6,736 KB
testcase_05 AC 41 ms
6,736 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 3 ms
4,936 KB
testcase_09 AC 142 ms
6,792 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 38 ms
5,420 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,888 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, v = rd[1].to!long;
  auto ci = readln.split.to!(long[]);

  foreach (i, c; ci.drop(1)) ci[i + 1] = ci[i] + c;
  v -= n;

  if (v <= 0) {
    writeln(ci.back);
    return;
  }

  auto k = ci.enumerate!long
    .map!(ec => Tuple!(long, real)(ec.index, ec.value.to!real / (ec.index + 1))).array
    .sort!"a[1] < b[1]".front[0];

  auto dp = new long[]((k + 1) * n);
  dp[] = long.max;
  dp[0] = 0;

  foreach (i; 0..n) {
    auto dp2 = dp.dup;
    foreach (dpi, dpv; dp) {
      if (dpv != long.max) {
        foreach (j; 0..(k + 1)) {
          auto idx = dpi + (i + 1) * j;
          if (idx < dp.length)
            dp2[idx] = min(dp2[idx], dpv + ci[i] * j);
        }
      }
    }
    dp = dp2;
  }

  auto r = dp.enumerate!long
    .filter!(d => d.value != long.max)
    .map!(d => d.value + (v - d.index + k) / (k + 1) * ci[k]);

  writeln(r.fold!min + ci.back);
}
0