Takahashi has prepared N N dishes for Snuke. The dishes are numbered from 1 1 to N N,…
- User
Takahashi has prepared N N dishes for Snuke. The dishes are numbered from 1 1 to N N, and dish i i has a sweetness of A i A i and a saltiness of B i B i .
Takahashi can arrange these dishes in any order he likes. Snuke will eat the dishes in the order they are arranged, but if at any point the total sweetness of the dishes he has eaten so far exceeds X X or the total saltiness exceeds Y Y, he will not eat any further dishes.
Takahashi wants Snuke to eat as many dishes as possible. Find the maximum number of dishes Snuke will eat if Takahashi arranges the dishes optimally.
Constraints 1 ≤ N ≤ 80 1≤N≤80 1 ≤ A i , B i ≤ 10000 1≤A i ,B i ≤10000 1 ≤ X , Y ≤ 10000 1≤X,Y≤10000 All input values are integers. Input The input is given from Standard Input in the following format:
N N X X Y Y A 1 A 1
B 1 B 1
A 2 A 2
B 2 B 2
⋮ ⋮ A N A N
B N B N
Output Print the answer as an integer.
Sample 1 Inputcopy Outputcopy 4 8 4 1 5 3 2 4 1 5 3 3 Consider the scenario where Takahashi arranges the dishes in the order 2 , 3 , 1 , 4 2,3,1,4.
First, Snuke eats dish 2 2. The total sweetness so far is 3 3, and the total saltiness is 2 2. Next, Snuke eats dish 3 3. The total sweetness so far is 7 7, and the total saltiness is 3 3. Next, Snuke eats dish 1
- The total sweetness so far is 8 8, and the total saltiness is 8
The total saltiness has exceeded Y
4 Y=4, so Snuke will not eat any further dishes. Thus, in this arrangement, Snuke will eat three dishes.
No matter how Takahashi arranges the dishes, Snuke will not eat all four dishes, so the answer is 3 3.
Sample 2 Inputcopy Outputcopy 2 1 1 3 2 3 2 1 Sample 3 Inputcopy Outputcopy 2 100 100 3 2 3 2 2 Sample 4 Inputcopy Outputcopy 6 364 463 230 381 154 200 328 407 339 94 193 10 115 309 3 #include <bits/stdc++.h>
using namespace std;
#define ll long long #define ld long double #define endl '\n' #define pb push_back #define speedyboy ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) x.begin() , x.end() #define F first #define S second #define pll pair<ll , ll> #define YES cout << "YES" << endl; #define NO cout << "NO" << endl;
ll i , j , k , ans; const ll sz1 = 85 , sz2 = 1e4 + 5 , INF = 1e18 , MOD = 1e9 + 7; ll dp[sz1][sz1][sz2]; pll a[sz1];
void solve(){ ll n , x , y , mx = 0; cin >> n >> x >> y; for(i = 1 ; i <= n ; i++){ cin >> a[i].F >> a[i].S; } for(i = 0 ; i <= n ; i++){ for(j = 0 ; j <= n ; j++){ for(k = 0 ; k <= x ; k++){ dp[i][j][k] = INF; } } } for(i = 0 ; i <= n ; i++){ dp[i][0][0] = 0; } for(i = 1 ; i <= n ; i++){ for(j = 1 ; j <= i ; j++){ for(k = 0 ; k <= x ; k++){ dp[i][j][k] = dp[i - 1][j][k]; if(k >= a[i].F){ dp[i][j][k] = min(dp[i][j][k] , dp[i - 1][j - 1][k - a[i].F] + a[i].S); } } } } for(i = 0 ; i <= n ; i++){ for(k = 0 ; k <= x ; k++){ if(dp[n][i][k] <= y){ mx = max(mx , i + 1); } } } cout << mx << endl; }
- Assistant