結果

問題 No.2617 容量3のナップザック
ユーザー ゆにぽけゆにぽけ
提出日時 2024-01-26 22:42:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,630 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 136,636 KB
実行使用メモリ 33,312 KB
最終ジャッジ日時 2024-01-26 22:42:10
合計ジャッジ時間 5,648 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 74 ms
15,976 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 77 ms
20,952 KB
testcase_26 AC 79 ms
16,664 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 48 ms
14,288 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 141 ms
28,832 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 138 ms
28,968 KB
testcase_40 WA -
testcase_41 AC 63 ms
28,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
void Main()
{
	int N,K;
	long long s,a,b,m;
	cin >> N >> K >> s >> a >> b >> m;
	vector<long long> f(2 * N);
	f[0] = s;
	for(int i = 0;i + 1 < 2 * N;i++)
	{
		f[i + 1] = (a * f[i] + b) % m;
	}
	vector<vector<long long>> G(4);
	for(int i = 0;i < N;i++)
	{
		int w = f[i] % 3 + 1;
		long long v = w * f[N + i];
		G[w].push_back(v);
	}
	for(int i = 1;i <= 3;i++)
	{
		sort(G[i].begin(),G[i].end());
	}
	long long ans = 0;
	for(int i = 0;i < K;i++)
	{
		long long cur = -1;
		if(G[3].size() >= 1)
		{
			cur = max(cur,G[3].back());
		}
		if(G[2].size() >= 1 && G[1].size() >= 1)
		{
			cur = max(cur,G[2].back() + G[1].back());
		}
		if(G[1].size() >= 3)
		{
			cur = max(cur,G[1][G[1].size() - 1] + G[1][G[1].size() - 2] + G[1][G[1].size() - 3]);
		}
		if(cur != -1)
		{
			ans += cur;
			bool fn = false;
			if(G[3].size() >= 1)
			{
				if(cur == G[3].back())
				{
					G[3].pop_back();
					fn = true;
				}
			}
			if(G[2].size() >= 1 && G[1].size() >= 1)
			{
				if(!fn && cur == G[2].back() + G[1].back())
				{
					G[2].pop_back();
					G[1].pop_back();
					fn = true;
				}
			}
			if(G[1].size() >= 3)
			{
				if(!fn && cur == G[1][G[1].size() - 1] + G[1][G[1].size() - 2] + G[1][G[1].size() - 3])
				{
					for(int j = 0;j < 3;j++)
					{
						G[1].pop_back();
					}
				}
				
			}
		}
		else
		{
			if(G[2].size() >= 1)
			{
				cur = max(cur,G[2].back());
			}
			if(G[1].size() >= 2)
			{
				cur = max(cur,G[1][G[1].size() - 1] + G[1][G[1].size() - 2]);
			}
			if(cur != -1)
			{
				ans += cur;
				bool fn = false;
				if(G[2].size() >= 1)
				{
					if(cur == G[2].back())
					{
						G[2].pop_back();
						fn = true;
					}
				}
				if(G[1].size() >= 2)
				{
					if(!fn && cur == G[1][G[1].size() - 1] + G[1][G[1].size() - 2])
					{
						for(int i = 0;i < 2;i++)
						{
							G[1].pop_back();
						}
					}
				}
			}
			else
			{
				if(G[1].size() >= 1)
				{
					cur = max(cur,G[1].back());
				}
				if(cur != -1)
				{
					ans += cur;
					G[1].pop_back();
				}
			}
		}
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0