結果

問題 No.2617 容量3のナップザック
ユーザー HaaHaa
提出日時 2024-01-26 23:45:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,627 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 177,504 KB
実行使用メモリ 43,052 KB
最終ジャッジ日時 2024-01-26 23:45:10
合計ジャッジ時間 6,346 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 112 ms
30,008 KB
testcase_13 AC 131 ms
33,028 KB
testcase_14 AC 110 ms
28,640 KB
testcase_15 AC 85 ms
22,196 KB
testcase_16 AC 83 ms
24,372 KB
testcase_17 AC 104 ms
26,908 KB
testcase_18 AC 102 ms
26,504 KB
testcase_19 AC 19 ms
7,460 KB
testcase_20 AC 118 ms
31,692 KB
testcase_21 AC 55 ms
16,460 KB
testcase_22 AC 132 ms
32,676 KB
testcase_23 AC 133 ms
33,952 KB
testcase_24 AC 8 ms
6,676 KB
testcase_25 AC 85 ms
22,768 KB
testcase_26 AC 87 ms
25,584 KB
testcase_27 AC 46 ms
14,048 KB
testcase_28 AC 6 ms
6,676 KB
testcase_29 AC 52 ms
15,500 KB
testcase_30 AC 127 ms
34,844 KB
testcase_31 AC 103 ms
26,804 KB
testcase_32 AC 141 ms
39,524 KB
testcase_33 AC 150 ms
40,800 KB
testcase_34 AC 151 ms
38,708 KB
testcase_35 AC 148 ms
39,196 KB
testcase_36 AC 160 ms
43,052 KB
testcase_37 AC 154 ms
40,932 KB
testcase_38 AC 147 ms
39,264 KB
testcase_39 AC 150 ms
37,436 KB
testcase_40 AC 152 ms
40,360 KB
testcase_41 AC 77 ms
35,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y){return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y){return (x>y)?(x=y,true):false;};
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

int main(){
    ll n, k, seed, a, b, m;
    cin >> n >> k >> seed >> a >> b >> m;
    VI f(2*n);
    f[0]=seed;
    REP(i,2*n-1) f[i+1]=(f[i]*a+b)%m;
    VVI v(3);
    REP(i,n){
        ll w=f[i]%3+1;
        v[w-1].push_back(w*f[n+i]);
    }
    VVI s(3);
    REP(i,3){
        sort(ALL(v[i]),greater<ll>());
        s[i].resize(v[i].size()+1);
        s[i][0]=0;
        REP(j,(int)v[i].size()){
            s[i][j+1]=s[i][j]+v[i][j];
        }
    }
    VI x(k+1,0);
    int pi=0, pj=0;
    for(int i=1;i<=k;i++){
        pi+=3;
        x[i]=s[0][min(max(0,pi),(int)s[0].size()-1)]+s[1][min(max(0,pj),(int)s[1].size()-1)];
        while(1){
            ll l=s[0][min(max(0,pi-2),(int)s[0].size()-1)]+s[1][min(max(0,pj+1),(int)s[1].size()-1)];
            ll r=s[0][min(max(0,pi+2),(int)s[0].size()-1)]+s[1][min(max(0,pj-1),(int)s[1].size()-1)];
            if(pi-2>=pj&&chmax(x[i],l))
                pi-=2, pj+=1;
            else if(pj>=1&&chmax(x[i],r))
                pi+=2, pj-=1;
            else
                break;
            
        }
    }
    ll ans=0;
    REP(i,k+1){
        chmax(ans,s[2][min(i,(int)s[2].size()-1)]+x[k-i]);
    }
    cout << ans << endl;
    return 0;
}
0