結果

問題 No.2617 容量3のナップザック
ユーザー Rubikun
提出日時 2024-01-26 21:41:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 2,055 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 202,408 KB
最終ジャッジ日時 2025-02-18 23:04:55
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,K,seed,AA,BB,M;cin>>N>>K>>seed>>AA>>BB>>M;
    vector<ll> F(2*N+1);
    F[1]=seed;
    for(int i=1;i<2*N;i++) F[i+1]=(AA*F[i]+BB)%M;
    
    vector<ll> A,B,C;
    for(int i=1;i<=N;i++){
        ll w=F[i]%3+1;
        ll v=w*F[N+i];
        if(w==1) C.push_back(v);
        if(w==2) B.push_back(v);
        if(w==3) A.push_back(v);
    }
    while(si(A)<=K) A.push_back(0);
    while(si(B)<=K) B.push_back(0);
    while(si(C)<=3*K) C.push_back(0);
    
    sort(all(A));
    reverse(all(A));
    
    sort(all(B));
    reverse(all(B));
    
    sort(all(C));
    reverse(all(C));
    
    A.resize(K);
    B.resize(K);
    C.resize(3*K);
    
    ll ans=0;
    
    vector<ll> Arui(K+1),Brui(K+1),Crui(3*K+1);
    
    for(int i=1;i<=K;i++){
        Arui[i]=Arui[i-1]+A[i-1];
    }
    for(int i=1;i<=K;i++){
        Brui[i]=Brui[i-1]+B[i-1];
    }
    for(int i=1;i<=3*K;i++){
        Crui[i]=Crui[i-1]+C[i-1];
    }
    
    for(ll a=0;a<=K;a++){
        ll left=0,right=K-a;
        while(right-left>2){
            ll m1=(left+left+right)/3,m2=(left+right+right)/3;
            ll x=Arui[a]+Brui[m1]+Crui[3*K-3*a-2*m1];
            ll y=Arui[a]+Brui[m2]+Crui[3*K-3*a-2*m2];
            
            if(x>=y) right=m2;
            else left=m1;
        }
        
        for(ll b=left-2;b<=right+2;b++){
            if(0<=b&&b<=K-a){
                ll x=Arui[a]+Brui[b]+Crui[3*K-3*a-2*b];
                chmax(ans,x);
            }
        }
    }
    
    cout<<ans<<endl;
}

0