結果

問題 No.2617 容量3のナップザック
ユーザー 👑 emthrmemthrm
提出日時 2024-01-26 21:46:45
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,947 bytes
コンパイル時間 3,577 ms
コンパイル使用メモリ 272,328 KB
実行使用メモリ 42,116 KB
最終ジャッジ日時 2024-01-26 21:46:59
合計ジャッジ時間 11,934 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,548 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 280 ms
27,992 KB
testcase_13 WA -
testcase_14 AC 301 ms
26,272 KB
testcase_15 WA -
testcase_16 AC 88 ms
19,160 KB
testcase_17 AC 290 ms
27,464 KB
testcase_18 AC 260 ms
26,980 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 136 ms
15,156 KB
testcase_22 WA -
testcase_23 AC 367 ms
37,524 KB
testcase_24 AC 9 ms
6,548 KB
testcase_25 AC 87 ms
22,592 KB
testcase_26 AC 96 ms
19,960 KB
testcase_27 AC 127 ms
13,280 KB
testcase_28 WA -
testcase_29 AC 65 ms
14,004 KB
testcase_30 WA -
testcase_31 AC 278 ms
23,772 KB
testcase_32 WA -
testcase_33 AC 425 ms
35,460 KB
testcase_34 AC 433 ms
36,064 KB
testcase_35 WA -
testcase_36 AC 192 ms
34,132 KB
testcase_37 AC 453 ms
36,064 KB
testcase_38 WA -
testcase_39 AC 156 ms
33,588 KB
testcase_40 AC 427 ms
35,420 KB
testcase_41 AC 140 ms
42,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

// https://ei1333.github.io/library/structure/others/priority-sum-structure.hpp
template< typename T, typename Compare = less< T >, typename RCompare = greater< T > >
struct PrioritySumStructure {

  size_t k;
  T sum;

  priority_queue< T, vector< T >, Compare > in, d_in;
  priority_queue< T, vector< T >, RCompare > out, d_out;

  PrioritySumStructure(int k) : k(k), sum(0) {}

  void modify() {
    while(in.size() - d_in.size() < k && !out.empty()) {
      auto p = out.top();
      out.pop();
      if(!d_out.empty() && p == d_out.top()) {
        d_out.pop();
      } else {
        sum += p;
        in.emplace(p);
      }
    }
    while(in.size() - d_in.size() > k) {
      auto p = in.top();
      in.pop();
      if(!d_in.empty() && p == d_in.top()) {
        d_in.pop();
      } else {
        sum -= p;
        out.emplace(p);
      }
    }
    while(!d_in.empty() && in.top() == d_in.top()) {
      in.pop();
      d_in.pop();
    }
  }

  T query() const {
    return sum;
  }

  void insert(T x) {
    in.emplace(x);
    sum += x;
    modify();
  }

  void erase(T x) {
    assert(size());
    if(!in.empty() && in.top() == x) {
      sum -= x;
      in.pop();
    } else if(!in.empty() && RCompare()(in.top(), x)) {
      sum -= x;
      d_in.emplace(x);
    } else {
      d_out.emplace(x);
    }
    modify();
  }

  void set_k(size_t kk) {
    k = kk;
    modify();
  }

  size_t get_k() const {
    return k;
  }

  size_t size() const {
    return in.size() + out.size() - d_in.size() - d_out.size();
  }
};

template< typename T >
using MaximumSum = PrioritySumStructure< T, greater< T >, less< T > >;

template< typename T >
using MinimumSum = PrioritySumStructure< T, less< T >, greater< T > >;

int main() {
  int n, k; cin >> n >> k;
  vector<int> w(n);
  vector<ll> v(n);
  {
    vector<ll> f(n * 2);
    int a, b, m; cin >> f.front() >> a >> b >> m;
    FOR(i, 1, n * 2) f[i] = (a * f[i - 1] + b) % m;
    REP(i, n) w[i] = f[i] % 3 + 1;
    REP(i, n) v[i] = w[i] * f[i + n];
  }
  array<vector<ll>, 4> item{};
  REP(i, n) item[w[i]].emplace_back(v[i]);
  FOR(i, 1, 3) ranges::sort(item[i], greater<ll>());
  ll ans = 0;
  REP(md, 3) {
    if (md > k || item[2].size() < md) break;
    ll cur = 0;
    REP(i, md) cur += item[2][i] + (i < item[1].size() ? item[1][i] : 0);
    vector<ll> tmp;
    tmp.reserve((item[1].size() + 2) / 3);
    for (int i = md; i < item[1].size(); i += 3) {
      tmp.emplace_back(0);
      REP(j, 3) {
        if (i + j < item[1].size()) tmp.back() += item[1][i + j];
      }
    }
    ranges::reverse(tmp);
    assert(ranges::is_sorted(tmp));
    MaximumSum<ll> ms(k - md);
    for (const ll v_i : tmp) ms.insert(v_i);
    for (const ll v_i : item[3]) ms.insert(v_i);
    for (int i = md; ;) {
      ms.set_k(max(k - i, 0));
      chmax(ans, cur + ms.query());
      if (i >= item[2].size()) break;
      REP(_, 3) {
        if (i < item[2].size()) cur += item[2][i];
        ++i;
      }
      if (!tmp.empty()) {
        cur += tmp.back();
        ms.erase(tmp.back());
        tmp.pop_back();
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
0