結果

問題 No.2686 商品券の使い道
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-20 22:18:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 3,000 ms
コード長 7,573 bytes
コンパイル時間 1,882 ms
コンパイル使用メモリ 148,316 KB
実行使用メモリ 8,020 KB
最終ジャッジ日時 2024-09-30 08:15:13
合計ジャッジ時間 3,586 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 10 ms
5,248 KB
testcase_03 AC 10 ms
5,248 KB
testcase_04 AC 10 ms
5,248 KB
testcase_05 AC 9 ms
5,248 KB
testcase_06 AC 10 ms
5,248 KB
testcase_07 AC 9 ms
5,248 KB
testcase_08 AC 10 ms
5,248 KB
testcase_09 AC 9 ms
5,248 KB
testcase_10 AC 9 ms
5,248 KB
testcase_11 AC 11 ms
5,248 KB
testcase_12 AC 10 ms
5,248 KB
testcase_13 AC 10 ms
5,248 KB
testcase_14 AC 11 ms
5,248 KB
testcase_15 AC 11 ms
5,248 KB
testcase_16 AC 11 ms
5,248 KB
testcase_17 AC 11 ms
5,248 KB
testcase_18 AC 11 ms
5,248 KB
testcase_19 AC 11 ms
5,248 KB
testcase_20 AC 11 ms
5,248 KB
testcase_21 AC 11 ms
5,248 KB
testcase_22 AC 11 ms
5,248 KB
testcase_23 AC 11 ms
5,248 KB
testcase_24 AC 11 ms
5,248 KB
testcase_25 AC 10 ms
5,248 KB
testcase_26 AC 10 ms
5,248 KB
testcase_27 AC 11 ms
5,248 KB
testcase_28 AC 10 ms
5,248 KB
testcase_29 AC 31 ms
7,892 KB
testcase_30 AC 30 ms
7,888 KB
testcase_31 AC 30 ms
8,016 KB
testcase_32 AC 31 ms
8,016 KB
testcase_33 AC 30 ms
8,016 KB
testcase_34 AC 30 ms
8,012 KB
testcase_35 AC 31 ms
8,012 KB
testcase_36 AC 30 ms
7,888 KB
testcase_37 AC 31 ms
8,008 KB
testcase_38 AC 30 ms
8,020 KB
testcase_39 AC 30 ms
8,012 KB
testcase_40 AC 30 ms
8,016 KB
testcase_41 AC 28 ms
7,988 KB
testcase_42 AC 31 ms
8,012 KB
testcase_43 AC 31 ms
8,012 KB
testcase_44 AC 30 ms
8,012 KB
testcase_45 AC 31 ms
8,016 KB
testcase_46 AC 31 ms
8,012 KB
evil_random20_1.txt AC 27 ms
8,016 KB
evil_random20_2.txt AC 26 ms
8,012 KB
evil_random20_3.txt AC 27 ms
8,008 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;
template<int m> struct modint
{
	private:
	unsigned int value;
	static constexpr int mod() {return m;}

	public:
	constexpr modint(const long long x = 0) noexcept
	{
		long long y = x;
		if(y < 0 || y >= mod())
		{
			y %= mod();
			if(y < 0) y += mod();
		}
		value = (unsigned int)y;
	}
	static constexpr int get_mod() noexcept {return m;}
	static constexpr int primitive_root() noexcept
	{
		assert(m == 998244353);
		return 3;
	}
	constexpr unsigned int val() noexcept {return value;}
	constexpr modint &operator+=(const modint &other) noexcept
	{
		value += other.value;
		if(value >= mod()) value -= mod();
		return *this;
	}
	constexpr modint &operator-=(const modint &other) noexcept
	{
		unsigned int x = value;
		if(x < other.value) x += mod();
		x -= other.value;
		value = x;
		return *this;
	}
	constexpr modint &operator*=(const modint &other) noexcept
	{
		unsigned long long x = value;
		x *= other.value;
		value = (unsigned int) (x % mod());
		return *this;
	}
	constexpr modint &operator/=(const modint &other) noexcept
	{
		return *this *= other.inverse();
	}
	constexpr modint inverse() const noexcept
	{
		assert(value);
		long long a = value,b = mod(),x = 1,y = 0;
		while(b)
		{
			long long q = a/b;
			a -= q*b; swap(a,b);
			x -= q*y; swap(x,y);
		}
		return modint(x);
	}
	constexpr modint power(long long N) const noexcept
	{
		assert(N >= 0);
		modint p = *this,ret = 1;
		while(N)
		{
			if(N & 1) ret *= p;
			p *= p;
			N >>= 1;
		}
		return ret;
	}
	constexpr modint operator+() {return *this;}
	constexpr modint operator-() {return modint() - *this;}
	constexpr modint &operator++(int) noexcept {return *this += 1;}
	constexpr modint &operator--(int) noexcept {return *this -= 1;}
	friend modint operator+(const modint& lhs, const modint& rhs) {return modint(lhs) += rhs;}
	friend modint operator-(const modint& lhs, const modint& rhs) {return modint(lhs) -= rhs;}
	friend modint operator*(const modint& lhs, const modint& rhs) {return modint(lhs) *= rhs;}
	friend modint operator/(const modint& lhs, const modint& rhs) {return modint(lhs) /= rhs;}
	friend ostream &operator<<(ostream &os,const modint &x) {return os << x.value;}
};
using mint = modint<998244353>;
/* using mint = modint<1000000007>; */
template<class T,T (*op)(T,T),T (*e)()> struct segtree
{
	private:
	int size,N;
	vector<T> node;

	public:
	segtree(int N = 0) : segtree(vector<T>(N,e())) {}

	segtree(const vector<T> &V)
	{
		N = (int)V.size();
		size = 1;
		while(size < N)
		{
			size <<= 1;
		}
		node.assign(2 * size,e());
		for(int i = 0;i < N;i++)
		{
			node[i + size] = V[i];
		}
		for(int i = size - 1;i >= 1;i--)
		{
			node[i] = op(node[2 * i],node[2 * i + 1]);
		}
	}

	void replace(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u = v;});
	}

	void add(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u + v;});
	}

	void func_update(int pos,T x)
	{
		func_update(pos,x,op);
	}

	void func_update(int pos,T x,T (*func)(T,T))
	{
		assert(0 <= pos && pos < N);
		pos += size;
		node[pos] = func(node[pos],x);
		while(pos >> 1)
		{
			pos >>= 1;
			node[pos] = op(node[2 * pos],node[2 * pos + 1]);
		}
	}

	T operator[](int pos)
	{
		assert(0 <= pos && pos < N);
		return node[pos + size];
	}

	int get_log()
	{
		int res = 0;
		while(1 << res < size)
		{
			res++;
		}
		assert((1 << res) == size);
		return res;
	}

	vector<vector<T>> tree_list()
	{
		vector<vector<T>> res;
		int log = get_log();
		res.reserve(log + 1);
		for(int i = 0;i <= log;i++)
		{
			int L = 1 << log,R = L << 1;
			vector<T> cur(node.begin() + L,node.begin() + R);
			res.push_back(cur);
		}
		return res;
	}

	//suppose that class T has ostream &operator<<
	void print(int k)
	{
		assert(0 <= k && k <= get_log());
		int L = 1 << k,R = L << 1;
		for(int i = L;i < R;i++)
		{
			cerr << node[i] << (i + 1 == R ? "\n":" ");
		}
	}

	void print()
	{
		int log = get_log();
		for(int k = 0;k <= log;k++)
		{
			int L = 1 << k,R = L << 1;
			const string unit((1 << (log - k)) - 1,' ');
			for(int i = L;i < R;i++)
			{
				if(i == L)
				{
					cerr << unit;
				}
				cerr << node[i];
				if(i + 1 < R)
				{
					cerr << unit << unit << ' ';
				}
				else
				{
					cerr << unit;
				}
			}
			cerr << "\n";
		}
	}

	T prod(int l,int r)
	{
		assert(0 <= l && l <= r && r <= N);
		T L = e(),R = e();
		l += size,r += size;
		while(l < r)
		{
			if(l & 1)
			{
				L = op(L,node[l++]);
			}
			if(r & 1)
			{
				R = op(node[--r],R);
			}
			l >>= 1,r >>= 1;
		}
		return op(L,R);
	}
	T prod()
	{
		return node[1];
	}

	template<class F> int max_right(int l,F f)
	{
		assert(0 <= l && l <= N);
		assert(f(e()));
		if(l == N)
		{
			return N;
		}
		l += size;
		T val = e();
		do
		{
			while(!(l & 1))
			{
				l >>= 1;
			}
			if(!f(op(val,node[l])))
			{
				while(l < size)
				{
					l <<= 1;
					if(f(op(val,node[l])))
					{
						val = op(val,node[l++]);
					}
				}
				return l - size;
			}
			val = op(val,node[l++]);
		} while((l & -l) != l);
		return N;
	}

	template<class F> int min_left(int r,F f)
	{
		assert(0 <= r && r <= N);
		assert(f(e()));
		if(r == 0)
		{
			return 0;
		}
		r += size;
		T val = e();
		do
		{
			r--;
			while(r > 1 && (r & 1))
			{
				r >>= 1;
			}
			if(!f(op(node[r],val)))
			{
				while(r < size)
				{
					r <<= 1;
					r++;
					if(f(op(node[r],val)))
					{
						val = op(node[r--],val);
					}
				}
				return r + 1 - size;
			}
			val = op(node[r],val);
		} while((r & -r) != r);
		return 0;
	}
};

long long op(long long a,long long b)
{
	return max(a,b);
}
long long e()
{
	return -(long long)1e18;
}
void Main()
{
	int N,M,Q;
	cin >> N >> M >> Q;
	vector<int> A(N),B(N);
	for(int i = 0;i < N;i++)
	{
		cin >> A[i] >> B[i];
	}

	auto dfs = [&](auto dfs,int id,const int t,long long m,long long q,long long val,vector<pair<pair<long long,long long>,long long>> &V) -> void
	{
		if(id == t)
		{
			V.push_back(make_pair(make_pair(m,q),val));
			return;
		}
		dfs(dfs,id + 1,t,m,q,val,V);
		dfs(dfs,id + 1,t,m + A[id],q,val + B[id],V);
		dfs(dfs,id + 1,t,m,q + A[id],val + B[id],V);
	};
	vector<pair<pair<long long,long long>,long long>> S,T;
	dfs(dfs,0,N / 2,0,0,0,S);
	dfs(dfs,N / 2,N,0,0,0,T);
	sort(S.begin(),S.end());
	sort(T.begin(),T.end());
	vector<long long> comp;
	for(int i = 0;i < (int)S.size();i++)
	{
		comp.push_back(Q - S[i].first.second);
	}
	for(int i = 0;i < (int)T.size();i++)
	{
		comp.push_back(T[i].first.second);
	}
	sort(comp.begin(),comp.end());
	comp.erase(unique(comp.begin(),comp.end()),comp.end());
	int j = 0;
	segtree<long long,op,e> seg(comp.size());
	long long ans = e();
	for(int i = (int)S.size() - 1;i >= 0;i--)
	{
		while(j < (int)T.size() && T[j].first.first + S[i].first.first <= M)
		{
			int id = lower_bound(comp.begin(),comp.end(),T[j].first.second) - comp.begin();
			seg.func_update(id,T[j].second);
			j++;
		}
		int id = lower_bound(comp.begin(),comp.end(),Q - S[i].first.second) - comp.begin();
		ans = max(ans,S[i].second + seg.prod(0,id + 1));
	}
	cout << ans << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0