結果

問題 No.2617 容量3のナップザック
ユーザー inksamuraiinksamurai
提出日時 2024-01-27 04:27:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,088 bytes
コンパイル時間 4,107 ms
コンパイル使用メモリ 212,476 KB
実行使用メモリ 42,700 KB
最終ジャッジ日時 2024-01-27 04:28:00
合計ジャッジ時間 10,355 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 RE -
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 243 ms
26,872 KB
testcase_13 AC 139 ms
35,456 KB
testcase_14 AC 224 ms
26,336 KB
testcase_15 AC 110 ms
22,712 KB
testcase_16 AC 159 ms
21,080 KB
testcase_17 AC 207 ms
25,752 KB
testcase_18 AC 170 ms
25,728 KB
testcase_19 AC 21 ms
7,720 KB
testcase_20 AC 132 ms
33,944 KB
testcase_21 AC 110 ms
15,568 KB
testcase_22 AC 149 ms
35,760 KB
testcase_23 AC 253 ms
40,616 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 163 ms
22,016 KB
testcase_27 AC 89 ms
13,152 KB
testcase_28 AC 8 ms
6,676 KB
testcase_29 AC 57 ms
17,928 KB
testcase_30 AC 149 ms
34,648 KB
testcase_31 AC 209 ms
25,136 KB
testcase_32 AC 211 ms
37,636 KB
testcase_33 AC 262 ms
37,664 KB
testcase_34 AC 315 ms
37,632 KB
testcase_35 AC 225 ms
37,632 KB
testcase_36 AC 306 ms
37,708 KB
testcase_37 AC 312 ms
37,636 KB
testcase_38 AC 163 ms
37,756 KB
testcase_39 RE -
testcase_40 AC 255 ms
37,640 KB
testcase_41 AC 84 ms
35,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int) a.size())
#define vec(...) vector<__VA_ARGS__>
#define _3PawRSC ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using vi=vector<int>;
using pii=pair<int,int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

void slv(){
	int n,k;
	cin>>n>>k;
	int seed,a,b,m;
	cin>>seed>>a>>b>>m;
	vi w(n),c(n);
	{
		vi f(2*n);
		f[0]=seed;
		rng(i,1,2*n){
			f[i]=(f[i-1]*a+b)%m;
		}
		rep(i,n){
			w[i]=f[i]%3+1;
		}
		rep(i,n){
			c[i]=w[i]*f[i+n];
		}
	}
	vec(vi) rbts(3);
	rep(i,n){
		rbts[w[i]-1].pb(c[i]);
		// print(w[i],c[i]);
	}
	rep(v,3){
		sort(rbts[v].begin(), rbts[v].end());
	}
	int si=sz(rbts[0]),si1=sz(rbts[1]),si2=sz(rbts[2]);
	vi ps;
	rep(i,si){
		ps.pb((!sz(ps)?0:ps.back())+rbts[0][i]);
	}
	vi ps1;
	rep(i,si1){
		ps1.pb((!sz(ps1)?0:ps1.back())+rbts[1][i]);
	}
	auto range_sum=[&](vi&ps,int l,int r)->int{
		if(l>r) return 0;
		// print(l,r);
		if(l<=0) l=0;
		if(r>=sz(ps)){
			cout<<"whu\n";
			exit(0);
		}
		return ps[r]-(!l?0:ps[l-1]);
	};
	// range_sum(ps,si-1)
	auto af=[&](int x,int take)->int{
		int now=range_sum(ps,si-1-take+1,si-1)+range_sum(ps1,si1-1-take+1,si1-1);
		int leftover=(x-take)*3;
		if(si-1-take>=0){
			now+=range_sum(ps,si-1-take-leftover+1,si-1-take);
		}
		return now;
	};
	// print(af(1,1));
	auto ask=[&](int x)->int{
		int l=0,r=min(x,max(si,si1));
		while(r-l>3){
			int ml=(2*l+r)/3;
			int mr=(l+2*r)/3;
			int fl=af(x,ml);
			int fr=af(x,mr);
			if(fl>fr){
				r=mr;
			}else{
				l=ml;
			}
		}
		int ret=0;
		rng(i,l,r+1){
			// print(x,i,af(x,i));
			ret=max(ret,af(x,i));
		}
		return ret;
	};
	// print(ask(1));
	int ans=0,ad=0;
	rep(i,min(k,si2)+1){
		ans=max(ans,ask(k-i)+ad);
		if(i<si2){
			ad+=rbts[2][si2-1-i];
		}
	}
	print(ans);
}

signed main(){
_3PawRSC;
	slv();
}
0