Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cstring>
- #include <vector>
- using ll=long long;
- using namespace std;
- int n,m,k;
- int cost[25];
- int cw[25][25];
- int vw[25][25];
- int dp[1006][22][3];
- int rec(int money_left, int at, bool locked) {
- if(at == -1 or money_left == 0) {
- return 0;
- }
- if(dp[money_left][at][locked] != -1) {
- return dp[money_left][at][locked];
- }
- int result = 0;
- result = max(result, rec(money_left, at - 1, true));
- if(locked) {
- int spend = money_left - cost[at];
- if(spend >= 0) {
- for(int j = 0; j < m; j++) {
- int tmp_spend = spend - cw[at][j];
- if(tmp_spend >= 0) {
- result = max(result, rec(tmp_spend, at, false) + vw[at][j]);
- result = max(result, rec(tmp_spend, at - 1, true) + vw[at][j]);
- }
- }
- }
- }
- else {
- for(int j = 0; j < m; j++) {
- int spend = money_left - cw[at][j];
- if(spend >= 0) {
- result = max(result, rec(spend, at, locked) + vw[at][j]);
- result = max(result, rec(spend, at - 1, true) + vw[at][j]);
- }
- }
- }
- return dp[money_left][at][locked] = result;
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- memset(dp,-1,sizeof dp);
- cin>>n>>m>>k;
- for(int i=0;i<n;i++){
- cin>>cost[i];
- }
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- cin >> cw[i][j];
- }
- }
- for(int i = 0; i < n; i++) {
- for(int j =0 ; j < m; j++) {
- cin >> vw[i][j];
- }
- }
- cout<<rec(k, n- 1,true)<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement