結果

問題 No.2617 容量3のナップザック
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-01-26 22:45:30
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 348 ms / 2,000 ms
コード長 3,792 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 114,176 KB
実行使用メモリ 60,604 KB
最終ジャッジ日時 2024-01-26 22:45:40
合計ジャッジ時間 9,393 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 259 ms
46,336 KB
testcase_13 AC 235 ms
48,936 KB
testcase_14 AC 220 ms
42,608 KB
testcase_15 AC 158 ms
33,844 KB
testcase_16 AC 180 ms
31,592 KB
testcase_17 AC 203 ms
41,064 KB
testcase_18 AC 222 ms
44,624 KB
testcase_19 AC 33 ms
9,900 KB
testcase_20 AC 214 ms
49,992 KB
testcase_21 AC 113 ms
22,944 KB
testcase_22 AC 242 ms
49,020 KB
testcase_23 AC 261 ms
51,212 KB
testcase_24 AC 12 ms
6,676 KB
testcase_25 AC 155 ms
36,984 KB
testcase_26 AC 194 ms
36,220 KB
testcase_27 AC 90 ms
21,224 KB
testcase_28 AC 9 ms
6,676 KB
testcase_29 AC 87 ms
20,052 KB
testcase_30 AC 225 ms
50,432 KB
testcase_31 AC 211 ms
41,868 KB
testcase_32 AC 253 ms
59,480 KB
testcase_33 AC 292 ms
59,472 KB
testcase_34 AC 295 ms
59,476 KB
testcase_35 AC 279 ms
57,244 KB
testcase_36 AC 348 ms
59,472 KB
testcase_37 AC 297 ms
59,480 KB
testcase_38 AC 266 ms
59,476 KB
testcase_39 AC 286 ms
60,604 KB
testcase_40 AC 290 ms
59,480 KB
testcase_41 AC 129 ms
60,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum INF = 10L^^18;

void main() {
  try {
    for (; ; ) {
      const N = readInt;
      const K = readInt;
      
      auto W = new long[N + 1];
      auto V = new long[N + 1];
      {
        const Seed = readLong;
        const A = readLong;
        const B = readLong;
        const M = readLong;
        auto F = new long[2 * N + 1];
        F[1] = Seed;
        foreach (i; 1 .. 2 * N) {
          F[i + 1] = (A * F[i] + B) % M;
        }
        foreach (i; 1 .. N + 1) {
          W[i] = F[i] % 3 + 1;
          V[i] = W[i] * F[N + i];
        }
      }
      debug {
        writeln("W = ", W);
        writeln("V = ", V);
      }
      
      auto vss = new long[][4];
      foreach (i; 1 .. N + 1) {
        vss[W[i]] ~= V[i];
      }
      foreach_reverse (w; 1 .. 3 + 1) {
        vss[w].sort!"a > b";
        debug {
          writeln(w, ": ", vss[w]);
        }
      }
      int[4] lens;
      auto vsSums = new long[][4];
      foreach (w; 1 .. 3 + 1) {
        lens[w] = cast(int)(vss[w].length);
        vsSums[w] = new long[lens[w] + 1];
        foreach (i; 0 .. lens[w]) {
          vsSums[w][i + 1] = vsSums[w][i] + vss[w][i];
        }
      }
      
      long ans;
      foreach (k; 0 .. K + 1) {
        long get3(int i) {
          if (i >= lens[3]) return -INF;
          return vss[3][i];
        }
        long get1(int i) {
          if (k + 3 * i + 3 > lens[1]) return -INF;
          return (vsSums[1][k + 3 * i + 3] - vsSums[1][k + 3 * i]);
        }
        int lo = 0, hi = (K - k) + 1;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          ((get3(mid - 1) < get1((K - k) - mid)) ? hi : lo) = mid;
        }
        {
          const i = lo;
          long score;
          score += vsSums[3][min(i, lens[3])];
          score += vsSums[2][min(k, lens[2])];
          score += vsSums[1][min(k + 3 * ((K - k) - i), lens[1])];
          chmax(ans, score);
        }
      }
      writeln(ans);
      
      debug {
        bool check(int i3, int i2, int i1) {
          if (i3 + i2 > K) return false;
          const k = K - i3 - i2;
          i1 = max(i1 - i2, 0);
          return (i1 <= 3 * k);
        }
        long brt;
        foreach (i3; 0 .. cast(int)(vss[3].length) + 1)
        foreach (i2; 0 .. cast(int)(vss[2].length) + 1)
        foreach (i1; 0 .. cast(int)(vss[1].length) + 1)
        {
          if (check(i3, i2, i1)) {
            chmax(brt, vsSums[3][i3] + vsSums[2][i2] + vsSums[1][i1]);
          }
        }
        assert(brt == ans, format("%s: %s %s", vss, brt, ans));
      }
    }
  } catch (EOFException e) {
  }
}
0