#pragma GCC optimize("O2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair; using pll = pair; //#define double ldb template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(const T &X : arr) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(const T &X : vec) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const set &s) { for(const T &x : s) os << x << ' '; return os; } int sum(vector &v, int l, int r) { if (v.empty() or l > r) return 0ll; return v[min((int)ssize(v) - 1, r)] - (l <= 0 ? 0ll : v[min((int)ssize(v) - 1, l - 1)]); } signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n, k, seed, a, b, m; cin >> n >> k >> seed >> a >> b >> m; vector w(n), v(n); { vector f(2 * n, seed); for(int i = 1; i < 2 * n; i++) f[i] = (a * f[i - 1] + b) % m; for(int i = 0; i < n; i++) w[i] = f[i] % 3 + 1, v[i] = w[i] * f[i + n]; } dbg(w); dbg(v); array, 4> item; for(int i = 0; i < n; i++) item[w[i]].emplace_back(v[i]); for(auto &v : item) R::sort(v | V::reverse); array, 3> suf, comp; for(int i = 0; i < 3; i++) { for(int j = i; j < ssize(item[1]); j += 3) { int tmp = 0; for(int k = 0; k < 3 and j + k < ssize(item[1]); k++) tmp += item[1][j + k]; comp[i].emplace_back(tmp); } suf[i] = comp[i]; partial_sum(suf[i].begin(), suf[i].end(), suf[i].begin()); } array, 4> pre; for(int i = 1; i < 4; i++) { pre[i] = item[i]; partial_sum(pre[i].begin(), pre[i].end(), pre[i].begin()); } int ans = 0; for(int i = 0; i <= ssize(item[2]) and i <= k; i++) { int cost = (i == 0 ? 0ll : pre[2][i - 1]) + (i == 0 ? 0ll : pre[1][min((int)ssize(item[1]), i) - 1]); auto calc = [&](int x) { int value = 0, cnt = 0; { int l = 0, r = ssize(item[3]); while(l < r) { int mid = (l + r) / 2; if (item[3][mid] < x) r = mid; else l = mid + 1; } cnt += l, value += sum(pre[3], 0, l - 1); } if (i / 3 < ssize(comp[i % 3])) { int l = i / 3, r = ssize(comp[i % 3]); while(l < r) { int mid = (l + r) / 2; if (comp[i % 3][mid] < x) r = mid; else l = mid + 1; } cnt += l - i / 3, value += sum(suf[i % 3], i / 3, l - 1); } return array{value, cnt}; }; int l = 0, r = 1ll << 32; while(l < r) { int mid = (l + r + 1) / 2; auto [value, cnt] = calc(mid); dbg(mid); dbg(calc(mid)); if (i + cnt >= k) l = mid; else r = mid - 1; } auto [value, cnt] = calc(l); dbg(cnt); dbg(cost); if (i + cnt > k) value -= l * (i + cnt - k); //cout << "i = " << i << ", " << cost + value << ' ' << i + cnt << '\n'; ans = max(ans, cost + value); } dbg(comp[0]); dbg(suf[0]); cout << ans << '\n'; return 0; }