#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ int n, k; cin >> n >> k; ll seed; cin >> seed; ll a, b, m; cin >> a >> b >> m; vector v(n), w(n); vector f(n * 2); f[0] = seed; for (int i=1; i w1; vector w2; vector w3; rep(i,0,n){ if (w[i] == 1){ w1.push_back(v[i]); }else if(w[i] == 2){ w2.push_back(v[i]); }else if(w[i] == 3){ w3.push_back(v[i]); } } sort(w1.begin(), w1.end()); sort(w2.begin(), w2.end()); sort(w3.begin(), w3.end()); reverse(w1.begin(), w1.end()); reverse(w2.begin(), w2.end()); reverse(w3.begin(), w3.end()); int n1 = w1.size(); int n2 = w2.size(); int n3 = w3.size(); vector rui1(n1 + 1); vector rui2(n2 + 1); vector rui3(n3 + 1); rep(i,0,n1) rui1[i+1] = w1[i]; rep(i,0,n2) rui2[i+1] = w2[i]; rep(i,0,n3) rui3[i+1] = w3[i]; rep(i,0,n1) rui1[i+1] += rui1[i]; rep(i,0,n2) rui2[i+1] += rui2[i]; rep(i,0,n3) rui3[i+1] += rui3[i]; vector threes(3); vector ones(3); vector var(3); ll ans = 0; rep(l,0,k+1){ ll tmp = 0; tmp += rui2[min(l, n2)]; while (ones[l % 3] <= min(l, n1)){ var[l % 3] += w1[ones[l % 3]]; ones[l % 3]++; } while((ones[l % 3] - min(l, n1)) % 3 != 0){ ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; } if(ones[l % 3] > l){ ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; } if(ones[l % 3] > l){ ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; ones[l % 3]--; var[l % 3] -= w1[ones[l % 3]]; } if (threes[l % 3] >= 1){ threes[l % 3]--; var[l % 3] -= w3[threes[l % 3]]; } if (threes[l % 3] >= 1){ threes[l % 3]--; var[l % 3] -= w3[threes[l % 3]]; } while ((ones[l % 3] < n1 || threes[l%3] < n3) && (ones[l % 3] - min(l, n1) + 2) / 3 + threes[l % 3] < k - l){ if (rui1[min(ones[l%3] + 3, n1)] - rui1[min(ones[l%3], n1)] <= rui3[min(threes[l%3] + 1, n3)] - rui3[min(threes[l%3], n3)]){ var[l%3] += rui3[min(threes[l%3] + 1, n3)] - rui3[min(threes[l%3], n3)]; threes[l%3]++; chmin(threes[l%3], n3); }else{ var[l%3] += rui1[min(ones[l%3] + 3, n1)] - rui1[min(ones[l%3], n1)]; ones[l%3] += 3; chmin(ones[l%3], n1); } } chmax(ans, var[l%3] + tmp); } cout << ans << '\n'; }