A KETO DIET


A KETO DIET



Tokita Ohma plans to start a keto diet. His automated kitchen can prepare NN types of food items. The ii-th food item has pipi grams of protein, fifi grams of fat, and cici grams of carbohydrates. Ohma wants to consume at least PP grams of protein and FF grams of fat each day while minimizing the carbohydrates he consumes. He also won't eat the same food item twice in the same day.


What food items should Ohma eat in a day so that he minimizes the carbohydrates he consumes and gets at least PP grams of protein and FF grams of fat. If it is not possible, output −1−1.

Input


Two integers P,FP,F on the first line.


One integer NN on the second line.


NN lines follow. Three integers pi,fi,cipi,fi,ci on the (i+2)(i+2)-th line.

Constraints


1≤N≤10001≤N≤1000


1≤P,F≤2501≤P,F≤250


1≤pi,fi,ci≤2501≤pi,fi,ci≤250

Output


One integer --- the minimum carbohydrates Ohma will end up having to consume in order to get at least PP grams of protein and FF grams of fat. If it is impossible to meet that requirement, output −1−1.

Example 1


Input
50 50
5
8 43 27
32 2 21
8 34 48
31 29 36
23 2 11



Output
74


#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
  int x = 1;
  while (b)
  {
    if (b & 1) x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr (names + 1, ',');
  cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 200005;

void solve() {

int n,p,f;
cin>>p>>f>>n;
vector<vector<int>>food;
int sump=0,sumf=0;
for (int i = 0; i < n; i++)
{
  int x,y,z;
  cin>>x>>y>>z;
  food.push_back({x,y,z});
  sump+=x;
  sumf+=y;
}
if(sump<|| sumf<f){
  cout<<-1<<endl;
  return;
}

vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(p+1,vector<int>(f+1,INT_MAX)));
dp[n][0][0]=0;

for (int i = n - 1; i >= 0; i--)
{
  for (int j = 0; j <=p; j++)
  {
    for (int k = 0; k <=f; k++)
    {
      int temp=dp[i+1][max(0LL,j-food[i][0])][max(0LL,k-food[i][1])]+food[i][2];
      dp[i][j][k]=min(dp[i+1][j][k],temp);
    }
    
  }
  
}
cout<<dp[0][p][f]<<endl;




}

int32_t main()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);


  clock_t z = clock();

  int t = 1;
  // cin >> t; 
  while (t--) solve();



  return 0;
}



Post a Comment

0 Comments