# Treats for the Cows – Dynamic Programming Solutions

«
»

This is a C++ Program that Solves Treats for the cows Problem using Dynamic Programming technique.

Problem Description

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons: The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats. Like fine wines and delicious cheeses, the treats improve with age and command greater prices. The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000). Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a. Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally? The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

Problem Solution

In this problem, the value of treats becomes as high as its age. So, our approach will be to select treats of lower values in earlier days and preserve the treats of high values for later to fetch greater values. The matrix dp calculates the result of the result of subproblems to avoid recomputation.

Expected Input and Output

Case-1:

```Number of treats=5
Values of treats=1 3 1 5 2
The maximum revenue that can be obtained by selling the treats is= 43```
Program/Source Code For Recursive Solution

Here is source code of the C++ Program to Solve Treats for Cows using Recursion. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

Note: Join free Sanfoundry classes at Telegram or Youtube
1. `#include<bits/stdc++.h>`
2. `using namespace std;`
3. ` `
4. `typedef vector<int> vi;`
5. `typedef vector<vi> vvi;`
6. ` `
7. ` `
8. `int calc(vvi &dp,vi &a,int i, int j, int k)`
9. `{`
10. `    if(i>j)`
11. `        return 0;`
12. ` `
13. `    if(dp[i][j]!=-1)`
14. `        return dp[i][j];`
15. ` `
16. `    //now, calculate`
17. `    return dp[i][j]=max(calc(dp,a,i+1,j,k+1)+a[i]*k,calc(dp,a,i,j-1,k+1)+a[j]*k);`
18. ` `
19. `}`
20. ` `
21. `int main()`
22. `{`
23. `    int n,i;`
24. `    cout<<"Enter the number of treats ";`
25. `    cin>>n;`
26. `    vector<int> a(n);`
27. ` `
28. `    cout<<"Enter the values of treats "<<endl;`
29. `    for(i=0;i<n;i++)`
30. `        cin>>a[i];`
31. ` `
32. `    vector<vector<int> > dp(n,vector<int>(n,-1));`
33. `    cout<<"The maximum revenue that can be obtained by selling the treats is "<<endl;`
34. `    cout<<calc(dp,a,0,n-1,1)<<endl;`
35. ` `
36. `    return 0;`
37. `}`
Program/Source Code for DP solution

Here is source code of the C++ Program to solve Treats for the cows Problem using Dynamic Programming. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. ` `
2. `#include<bits/stdc++.h>`
3. `using namespace std;`
4. ` `
5. `int solve(int n, vector<int> a)`
6. `{`
7. `    vector<vector<int> > dp(n, vector<int>(n));`
8. `    int i,j;`
9. `    int day;`
10. ` `
11. `    //initialization`
12. `    for(i=0;i<n;i++)`
13. `    {`
14. `        dp[i][i]=n*a[i];`
15. `    }`
16. ` `
17. `    for(int len=2;len<=n;len++)`
18. `    {`
19. `        day=n-len+1;`
20. ` `
21. `        for(i=0;i<(n-len+1);i++)`
22. `        {`
23. `            j=i+len-1;`
24. ` `
25. `            dp[i][j]=max(day*a[i]+dp[i+1][j],dp[i][j-1]+day*a[j]);`
26. `        }`
27. `    }`
28. ` `
29. `    return dp[n-1];`
30. `}`
31. ` `
32. `int main()`
33. `{`
34. `    int n,i;`
35. `    cout<<"Enter the number of treats ";`
36. `    cin>>n;`
37. `    vector<int> a(n);`
38. ` `
39. `    cout<<"Enter the values of treats "<<endl;`
40. `    for(i=0;i<n;i++)`
41. `        cin>>a[i];`
42. ` `
43. `    cout<<"The maximum revenue that can be obtained by selling the treats is "<<endl;`
44. `    cout<<solve(n,a)<<endl;`
45. ` `
46. `    return 0;`
47. `}`
Program Explanation

In the main function, we take input for the number of treats and their values. This input is passed as arguments to the function solve. In the solve function, we created a matrix to memoize the result of subproblems. The final answer is returned and displayed.

Runtime Test Cases
```
Case-1:
\$ g++ treats.cpp
\$ ./a.out
Enter the number of treats 5
Enter the values of treats
1 3 1 5 2
The maximum revenue that can be obtained by selling the treats is
43```

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions. 